要做下面两件事就可以达到目的了:
1 在tld文件如下配置:
- <tag>
- <icon>
- </icon>
- <name>rim</name>
- <tag-class>com.xui.overall.RimTag</tag-class>
- <body-content>JSP</body-content>
- <attribute>
- <name>title</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
<tag>
<icon>
</icon>
<name>rim</name>
<tag-class>com.xui.overall.RimTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>title</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
其中关键的是 <rtexprvalue>true</rtexprvalue> ,使title属性可以接受EL表达式.
2 对接收到的EL表达式处理
title属性的 set get 方法 如下写
- private Object title = "";
- public void setTitle(final Object title) throws JspException
- {
- // 对EL表达式的支持
- this.title = ExpressionEvaluatorManager.evaluate("title", title.toString(), Object.class, this, pageContext);
- }
- public Object getTitle()
- {
- return title;
- }
private Object title = "";
public void setTitle(final Object title) throws JspException
{
// 对EL表达式的支持
this.title = ExpressionEvaluatorManager.evaluate("title", title.toString(), Object.class, this, pageContext);
}
public Object getTitle()
{
return title;
}
这里处理${}的是org.apache.taglibs.standard.lang.support下,有个叫 ExpressionEvaluatorManager类,
ExpressionEvaluatorManager.evaluate有五个参数。第一个title属性的名字,本例用"title"就行。第二个要求字符串,通常简单调用输入对象的toString方法。第三个是类,通常用Object.class。第四个用this即可,第五个是pageContext变量。
注意:当你的tag属性支持el表达式时,你必须把它声明为Object对象:本例中是 private Object title = "标题";
这样工作就做完了,自定义标签就可以处理 ${}了.