JSP simple custom label of foreach traversal and escape character Descriptor example _jsp programming

Source: Internet
Author: User
Tags stringbuffer
Then yesterday, if the items type in <forEach> were map or collection type, how to use the enhanced for loop;
First, you create a label processor class that defines two attributes, String var; Object items;
Because items want to iterate over various collections, use object;
Then rewrite the setter method;
Declare a member variable, the collection type, and the above two attributes are not the same, this is used in the class,
In the setter method of items, determine the type of items
Then inherit his Dotag method;
Copy Code code 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 Code code 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 the various types of items in the JSP file
Copy Code code 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>

The next is an escaped custom label:
Steps are the same:
Copy Code code as follows:

public void Dotag () throws Jspexception, IOException {
Jspfragment JF = This.getjspbody ()//Get the contents of the JSP file
StringWriter SW = new StringWriter ();//Get a Stream object
Jf.invoke (SW)//bar content in stream object
string s =sw.tostring ();//Convert JSP content to string
s= filter (s);//Get the character after escaping
This.getjspcontext (). Getout (). write (s);//Writing browser
}
public string Filter (String message) {//method of escaping a string
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 ());
}
}

Then it's the same,
Copy Code code 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 contents of the JSP tag file are output as is;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.