国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Taglib原理和實(shí)現(xiàn)之El表達(dá)式和JSTL標(biāo)簽

2019-11-18 16:00:37
字體:
供稿:網(wǎng)友

  1、問題:你想和jstl共同工作。比如,在用自己的標(biāo)簽處理一些邏輯之后,讓jstl處理余下的工作。

  2、看這個(gè)jsp例子:

....
<%
 String name="diego";
 request.setAttribute("name",name);
%>

<c:out value="${name}"/>
......
  許多jstl標(biāo)簽支持El表達(dá)式,所以,只要你在自己的標(biāo)簽內(nèi)部把值塞進(jìn)request,其他jstl標(biāo)簽就能使用它們

  3、下面這個(gè)例子,從request里面取得對象,找到它屬性的值,塞到request里去。

package diegoyun;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.beanutils.PRopertyUtils;
import org.apache.taglibs.standard.lang.support.EXPressionEvaluatorManager;

public class SetVarTag extends TagSupport
{
 private Object value = null;
 private String property = null;
 private String var = null;
 public void setVar(String var)
 {
  this.var = var;
 }
 public void setProperty(String property)
 {
  this.property = property;
 }
 public void setValue(Object value)throws JspException{
  this.value = ExpressionEvaluatorManager.evaluate("value", value.toString(), Object.class, this, pageContext);
 }
 public int doEndTag() throws JspException{
  Object propertyValue = null;
  try{
   propertyValue = PropertyUtils.getProperty(value, property);
  }
  catch (Exception e) {
   throw new JspException(e);
  }  
  pageContext.setAttribute(var,propertyValue);
  return EVAL_PAGE;
 }
}

  編寫tld

<!--SetVarTag-->
<tag>
 <name>set</name>
 <tag-class>diegoyun.SetVarTag</tag-class>
 <body-content>empty</body-content>
 <attribute>
  <name>value</name>
  <required>true</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
 <attribute>
  <name>property</name>
  <required>false</required>
  <rtexprvalue>false</rtexprvalue>
 </attribute>
 <attribute>
  <name>var</name>
  <required>false</required>
  <rtexprvalue>false</rtexprvalue>
 </attribute>
</tag>
  編寫jsp

<%@ page language="java" %>
<%@ page import="diegoyun.vo.*"%>
<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c"%>
<Html>
<body bgcolor="#FFFFFF">
<%
 Man man = new Man();
 man.setName("diego");
 request.setAttribute("man",man);
%>
Get value from request and set it's property value into request:

<diego:set value="${man}" property="name" var="myname"/>
now use OutTag of jstl taglib to get the name:

value is : <c:out value="${myname}" />

</body>
</html>
  運(yùn)行,效果如下:

Get value from request and set it's property value into request:
now use OutTag of jstl taglib to get the name:
value is : diego

  4、結(jié)語

  和jstl交互是非常有用的技術(shù)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 布尔津县| 门源| 宜章县| 达拉特旗| 万载县| 临洮县| 屯门区| 柘城县| 明星| 三门峡市| 翼城县| 栖霞市| 东莞市| 永顺县| 璧山县| 镇赉县| 玉树县| 英山县| 庆安县| 朝阳区| 十堰市| 祁东县| 祥云县| 如东县| 黔西县| 台北县| 威宁| 兴业县| 綦江县| 洛川县| 拉萨市| 文山县| 岳池县| 双峰县| 阿坝| 龙江县| 永年县| 阳信县| 大关县| 贵德县| 山丹县|