自定义书签-带属性的标签

要想让一个自定义标签具有属性,通常需要完成两个任务:
   * 在标签处理器中编写每个属性对应的setter方法
   * 在TLD文件中描术标签的属性
为自定义标签定义属性时,每个属性都必须按照JavaBean的属性命名方式, 标签处理器中定义属性名对应的setter方法,用来接收JSP页面调用自定义标签时传递进来的属性值。
   例如属性url, 在标签处理器类中就要定义相应的setUrl(String url)方法。 在标签处理器中定义相应的set方法后,JSP引擎在解析执行开始标签前,也就是调用doStartTag方法前,会调用set属性方法,为标签设置属性(
注意:只支持八种基本类型数据的自动转换
<tag>元素的<attribute>子元素用于描述自定义
标签的一个属性,自定义标签所具有的每个属性
都要对应一个<attribute>元素 。

<attribute>
    <description>description</description>
    <name>aaaa</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    <type>ObjectType</type>

</attribute>

用自定义标签定义属性输出一个书签实体count次:
jspd代码:
<html> <head> </head> <body> <simpleTag:SimpleDemo5 count="5"> 啊说呱唧呱唧<Br> </simpleTag:SimpleDemo5> </body> </html>
tld文件: <tag> <name>SimpleDemo5</name> <tag-class>web.simpleTag.SimpleDemo5</tag-class> <body-content>scriptless</body-content> <attribute> <name>count</name> <required>yes</required> <rtexprvalue>yes</rtexprvalue> </attribute> </tag> Java代码:

      public class SimpleDemo5 extends SimpleTagSupport{
        private int count;
        public void setCount(int count) {
          this.count=count;
        }
        @Override
        public void doTag() throws JspException, IOException {
          JspFragment jf=this.getJspBody();
          for(int i=0;i<count;i++){
          jf.invoke(this.getJspContext().getOut());
        }

      }


 

posted @ 2016-04-28 17:54  戒。  阅读(331)  评论(0编辑  收藏  举报