首页 > 学院 > 开发设计 > 正文

Taglib原理和实现:再论El和JSTL标签

2019-11-18 15:47:36
字体:
来源:转载
供稿:网友

问题:你想和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);



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表