注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
不用杀毒软件如何清除hta或wmf木
信息安全的隐患-GoogleHacking原
Microsoft AntiSpyware微软出品的
Windows Vista系统防火墙初探
微软官方解读Vista安全性: 全面的
使用netfilter/iptables构建防火
一段使用 iptable 设置防火墙的设
iptables使用实例
用iptables实现NAT
限制单个IP并发TCP连接的iptable
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Java >> 文章正文
Taglib原理和实现:再论El和JSTL标签
作者:佚名  来源:不详  点击:  更新:2006-12-19
简介:

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

看这个JSP例子:

<%
String name="diego";
request.setAttribute("name",name);
%>
<c:out value="${name}"/>
......

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

下面这个例子,从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

结束语

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






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务