(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>