当前位置: 首页 > 图文教程 > 网络编程 > JSP > Taglib原理和实现 第五章:再论支持El表达式和jstl标签

JSP
GET 方式提交的含有特殊字符的参数
java big5到gb2312的编码转换
java Lucene 中自定义排序的实现
hibernate中的增删改查实现代码
jsp 定制标签(Custom Tag)
jsp基础速成精华讲解
IE cache缓存 所带来的问题收藏
关于JSP的一点疑问小结
JSP 多条SQL语句同时执行的方法
jsp include文件时的一个乱码解决方法
在JSTL EL中处理java.util.Map,及嵌套List的情况
jsp 页面显示的一些用法
根据Hibernte的cfg文件生成sql文件
五种 JSP页面跳转方法详解
JSP 防范SQL注入攻击分析
JSP 连接MySQL配置与使用
java eclipse 启动参数
jsp 页面上图片分行输出小技巧
解决jsp开发中不支持EL问题
JSP 页面中使用FCKeditor控件(js用法)

JSP 中的 Taglib原理和实现 第五章:再论支持El表达式和jstl标签


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 87 ::
收藏到网摘: n/a

1。问题:你想和jstl共同工作。比如,在用自己的标签处理一些逻辑之后,让jstl处理余下的工作。

2。看这个jsp例子:
....
<%
String name="diego";
request.setAttribute("name",name);
%>

<c:out value="${name}"/>
......

  许多jstl标签支持El表达式,所以,只要你在自己的标签内部把值塞进request,其他jstl标签就能使用它们

3。下面这个例子,从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:<br>
<diego:set value="${man}" property="name" var="myname"/>
now use OutTag of jstl taglib to get the name:<br>
value is : <c:out value="${myname}" />

</body>
</html>

运行,效果如下:

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。结语。和jstl交互是非常有用的技术。在jstl里提供了许多完成基本功能的标签,如输出,循环,条件选择等。仅在处理自己特定逻辑的时候才实现自己的标签,并提供和jstl交互,能大大提高重用性和减少工作量