Example of foreach traversal and escape character of JSP simple custom tag

Time:2020-10-20

Then yesterday, if the items type in < foreach > is of map or collection type, how to use the enhanced for loop;
First, create a tag handler class and define two attributes, string VaR; object items;
Because items want to iterate over various sets, object is used;
Then the setter method is rewritten;
Declaring a member variable of collection type is different from the above two properties. This is used in the class,
In the setter method of items, determine the type of items
Then it inherits his dotag method;

Copy codeThe code is as follows:
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);
}
}
}

Then, write the TLD description tag

Copy codeThe code is as follows:
<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>

Finally, write various types of items in JSP file

Copy codeThe code is as follows:
<%
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>

Next is an escape custom tag:
The steps are the same:

Copy codeThe code is as follows:
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody (); / / get the content of JSP file
Stringwriter SW = new stringwriter(); / / get a stream object
jf.invoke (SW); / / put the content into the stream object
String s = sw.toString (); / / convert JSP content into string
S = filter (s); / / get the character after escape
this.getJspContext (). Getout(). Write (s); / / write to browser
}
Public string filter (string message) {/ / method for escaping strings
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());
}
}

And then it’s the same,

Copy codeThe code is as follows:
<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>

The content of JSP tag file is output as is;