這篇文章主要介紹了jsp簡單自定義標(biāo)簽的forEach遍歷及轉(zhuǎn)義字符,需要的朋友可以參考下
接著昨天的,如果<forEach>中的items類型是map或者Collection類型的,怎樣使用增強(qiáng)for循環(huán);
首先還是創(chuàng)建一個(gè)標(biāo)簽處理器類,定義兩個(gè)屬性,String var; Object items;
因?yàn)閕tems要迭代各種集合,所以要使用Object;
然后重寫setter方法;
聲明一個(gè)成員變量,集合類型的, 和上面兩個(gè)屬性是不相同的,這個(gè)是用在類里的,
在items的setter方法中,判斷items的類型
然后繼承他的doTag方法;
代碼如下:
public class ForEachTag2 extends SimpleTagSupport {
private String var;
private Object items;
private Collection collection;
public void setVar(String var){
this.var=var;
}
public void setItems(Object items){
this.items=items;
if(items instanceof Map){
Map map = (Map) items;
collection = map.entrySet();
}
if(items instanceof Collection){//set list
collection =(Collection) items;
}
if(items.getClass().isArray()){
collection = new ArrayList();
int len = Array.getLength(items);
for(int i=0;i<len;i++){
Object obj= Array.get(items, i);
collection.add(obj);
}
}
}
@Override
public void doTag() throws JspException, IOException {
Iterator iterator = collection.iterator();
while(iterator.hasNext()){
Object obj = iterator.next();
this.getJspContext().setAttribute(var, obj);
this.getJspBody().invoke(null);
}
}
}
然后,寫tld描述標(biāo)簽
代碼如下:
<tag>
<name>forEach2</name>
<tag-class>com.csdn.items.ForEachTag2</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>var</name>
<required>true</required>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
最后在jsp文件中寫items的各種類型
代碼如下:
<%
Map map = new HashMap();
map.put("aa","aaaa");
map.put("bb","bbbb");
map.put("cc","cccc");
map.put("dd","dddd");
map.put("ee","eeee");
request.setAttribute("map",map);
%>
<c:forEach2 var="str" items="${map}">
${str.key }-----${str.value }<br />
</c:forEach2>
<%
String[] strs ={"aa","bb","cc"} ;
request.setAttribute("strs",strs);
%>
<c:forEach2 var="str" items="${strs}">
${str}<br>
</c:forEach2>
接下里是一個(gè)轉(zhuǎn)義的自定義標(biāo)簽:
步驟都一樣:
代碼如下:
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody();//獲取jsp文件中的內(nèi)容
StringWriter sw = new StringWriter();//獲取一個(gè)流對象
jf.invoke(sw);//吧內(nèi)容放到流對象中
String s =sw.toString();//把jsp內(nèi)容轉(zhuǎn)成字符串
s= filter(s);//獲取進(jìn)行轉(zhuǎn)義之后的字符
this.getJspContext().getOut().write(s);//寫入瀏覽器
}
public String filter(String message) {//對字符串進(jìn)行轉(zhuǎn)義的方法
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
接下來就一樣了,
代碼如下:
<tag>
<name>htmlFilter</name>
<tag-class>com.csdn.items.HTMLFilter</tag-class>
<body-content>scriptless</body-content>
</tag>
<c:htmlFilter>
<a href=""> aaa</a>
</c:htmlFilter>
Jsp標(biāo)簽文件的內(nèi)容原樣輸出;
更多信息請查看IT技術(shù)專欄