在這裏要注意的問題:set方法可以在八大基本數據之間相互轉化,其他類型的數據不能進行轉化,但同樣可以直接傳遞
java代碼:
package cn.csdn.class3g.web.tag;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TagArray extends SimpleTagSupport {
/*set方法可以在八大基本數據之間相互轉化,其他類型的數據不能進行轉化,但同樣可以直接傳遞*/
private String var;
private int[] items;
public void setVar(String var) {
this.var = var;
}
public void setItems(int[] items) {
this.items = items;
}
public void doTag() throws JspException, IOException {
//數組的排序
Arrays.sort(items);
/*遍歷數組並輸出*/
for(int i=0;i<items.length;i++){
//將數組值傳給var屬性
var = ""+items[i];
this.getJspContext().setAttribute("arr",var);
//輸出
this.getJspBody().invoke(null);
}
}
}
Arrays.sort(items);的解釋:
J2eeAPI中Array中的sort(byte[] a)方法
public static void sort(byte[] a)
對指定的 byte 型數組按數字升序進行排序。該排序算法是一個經過調優的快速排序法,改編自 Jon L. Bentley 和 M. Douglas McIlroy 合著的 Engineering a Sort Function", Software-Practice and ExperienceVol. 23(11) P. 1249-1265 (November 1993)。此算法在許多數據集上提供n*log(n) 性能,這導致其他快速排序會降低二次型性能。
參數:
a - 要排序的數組
jsp文件的部分代碼:
<!-- 將數組值直接傳入屬性中 -->
<%
int arr[] = {100,40,39,86,54};
request.setAttribute("arr",arr);
%>
<!-- 獲取屬性值 -->
<tag:tag var="arrry"items="${arr}">
${arr}
</tag:tag>
tld文件的部分代碼:
<description>there are custom tags of tag</description>
<tlib-version>1.0</tlib-version>
<short-name>tag</short-name>
<uri>http://cn.csdn.web.tag</uri>
<tag>
<description>if else demo</description>
<name>tag</name>
<tag-class>cn.csdn.class3g.web.tag.TagArray</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
要注意的問題:
jsp文件中務必要導入標籤描述文件:
<%@ taglib uri="http://cn.csdn.web.tag" prefix="tag"%>