天天看點

帶屬性的自定義标簽

帶屬性的自定義标簽

1)控制标簽體循環輸出指定次數

在标簽處理類中添加屬性變量及其setter方法

private int times;

public void doTag() throws JspException, IOException {

  JspFragment jf = this.getJspBody();

  for(int i=0; i<times; i++){

    jf.invoke(null);

  }

}

public void setTimes(int times) {

  this.times = times;

}

在tld檔案中增加屬性描述<attribute></attribute>

<attribute>

  <name>times</name>

  <required>true</required>

  <rtexprvalue>true</rtexprvalue>

</attribute>

示範<trexprvalue>true</rtexprvalue>

<class3g:mySimpleTag5 times="<%=3+1 %>">

  aaaaaaaaa<br>

</class3g:mySimpleTag5>

2)解釋傳參時類型轉換問題,并以Data資料的傳遞示範非基本資料類型的參數傳遞

a、         在标簽處理類中添加Data類型成員及其setter方法

private Date date;

public void setDate(Date date) {

  this.date = date;

}

b、        在tld中添加屬性描述

<attribute>

  <name>date</name>

  <required>true</required>

  <rtexprvalue>true</rtexprvalue>

<type>java.util.Date</type>

</attribute>

c、         在jsp中調用

<class3g:mySimpleTag5 times="3" date="<%=new Date() %>">

xxxxxxxx

</class3g:mySimpleTag5>