国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 信宜市| 庆阳市| 黄大仙区| 长白| 如皋市| 曲沃县| 开江县| 广水市| 平江县| 黄浦区| 藁城市| 青河县| 南华县| 中山市| 金堂县| 津南区| 涟源市| 吕梁市| 聊城市| 林芝县| 文化| 绥中县| 霍城县| 山阴县| 德州市| 临夏县| 收藏| 九寨沟县| 花垣县| 古交市| 门源| 简阳市| 平泉县| 德令哈市| 长丰县| 兴海县| 鄂尔多斯市| 长春市| 柳林县| 巴青县| 彭山县|