A descriptor example of the foreach traversal and escape character of the JSP simple custom label

Source: Internet
Author: User
Tags filter foreach stringbuffer

  This article mainly introduces the JSP simple custom label's foreach traversal and the escape character, needs the friend to be possible to refer to under

Then yesterday, if the items type in <forEach> is a map or collection type, how to use the enhanced for loop;  first or create a label processor class, define two attributes, String var; Object items;  because items want to iterate over a variety of collections, you use object;  and then rewrite the setter method;  declare a member variable, the collection type, and the top two attributes are not the same, this is used in the class,   in the setter method of items, judge the type of items   then inherit his Dotag method;    Code as follows: public class ForEachTag2 extends Simpletagsupport {  Private String var;  private Object items;  private Collection collection;  c 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 in Stanceof 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++) {  objec T obj= array.get (items, i);  collection.add (obj); }  }  @Override   public void Dotag () throws jspexception ioexception {  = iterator iterator Tor ();  while (Iterator.hasnext ()) {  Object obj = Iterator.next ();  this.getjspcontext (). setattribute ( var, obj);  this.getjspbody (). Invoke (null); } } }    Then, write the TLD description tag     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 items in the JSP file the various types of   code is as follows: <%   Map map = new HashMap ();  map.put ("AA", "AAAA");  map.put ("BB", "bbbb") &nbSp 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:  code as follows: public void Dotag () throws Jspexception, IOException {  Jspfra Gment JF = This.getjspbody ()//Get content in JSP file   StringWriter SW = new StringWriter ();//Get a Stream object   Jf.invoke (SW); The content is put into the Stream object   string s =sw.tostring ();//Convert the JSP content to a string   s= filter (s);//Get the character   This.getjspcontext () after escaping. Getout (). write (s);//writing to browser  }  public string filter (String message) {///to escape a string   if (message = = NULL) & nbsp return (NULL);  char content[] = new Char[message.lengtH ()];  message.getchars (0, Message.length (), content, 0);  stringbuffer result = new StringBuffer ( Content.length +);  for (int i = 0; i < content.length i++) {  switch (content[i)) {  case ' < ': &N Bsp Result.append ("<");  break;  case ' > ':  result.append (">");  break;  case ' & ': & nbsp Result.append ("&");  break;  case ' "':  result.append (" ");  break;  Result.append (Content[i]); } }  return (result.tostring ()); } }    The next one,     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 label file are output; 
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.