(1)在标签处理器中编写每个属性对应的setter方法
(2)在TLD文件中描术标签的属性
2.为自定义标签定义属性时,每个属性都必须按照JavaBean的属性命名方式,在标签处理器中定义属性名对应的setter方法,用来接收JSP页面调用自定义标签时传递进来的属性值。 例如属性url,在标签处理器类中就要定义相应的setUrl(String url)方法。
3.在标签处理器中定义相应的set方法后,JSP引擎在解析执行开始标签前,也就是调用doStartTag方法前,会调用set属性方法,为标签设置属性。
public class SimpleTagDemo5 extends SimpleTagSupport {
private int count;
private Date date;
public void setCount(int count) {
this.count = count;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody();
for(int i=0;i<count;i++){
jf.invoke(null);
}
}
}
<name>simpledemo5</name>
<tag-class>cn.itcast.web.simpletag.SimpleTagDemo5</tag-class>
<body-content>scriptless</body-content>
<attribute>
<description>指定标签把标签体执行几次</description>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>date</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>这个值为true说明在给属性赋值的时候,可以接受el表达式或者脚本表达式;如果是false,则只能给一个固定的值
</attribute>
</tag>