JSP simple Custom Label's foreach traversal and escape characters

Source: Internet
Author: User
Tags foreach stringbuffer

The items type in <forEach> is a map or collection type, and how to use the enhanced for loop;


first 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; www.2cto.com


declares a member variable, the collection type, and the above two attributes are not the same, this is used in the class,


determines the type of items in the setter method of the items


then inherits his Dotag method;


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


<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


<%


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>


followed by an escaped custom label:


steps are the same:


public void Dotag () throws Jspexception, IOException {


jspfragment JF = this.getjspbody ()//Get the content in the JSP file


StringWriter sw = new StringWriter ();//Get a Stream object


Jf.invoke (SW)//bar content to flow 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 to escape 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 ());


}


}


Next,
.

<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 tag file content is exported 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.