自定义标签库(Tag library)
通过标签实现if功能
- 功能要求:
- 标签形式
<a:if test=””>
标签体
</a:if>
- 标签名:if
- 参数:test,接收一个boolean值
- 标签体:网页代码或EL表达式
- 逻辑
- 如果test的值为true,则显示标签体的内容
- 如果test的值为false,则不显示标签体的内容
- 实现:
- 处理器类
public class IfTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { if(test){ getJspBody().invoke(null); } } } |
- tld文件
<tag> <name>if</name> <tag-class>com.atguigu.web.tag.IfTag</tag-class> <body-content>tagdependent</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> |