ForEach traversal and escape character examples of simple custom jsp labels

Source: Internet
Author: User

Next, if the items type in <forEach> is map or Collection, how can we use the enhanced for loop;
First, create a tag processor class and define two attributes: String var; Object items;
Because items needs to iterate various sets, objects must be used;
Then rewrite the setter method;
Declare a member variable of the set type, which is different from the preceding two attributes. This is used in the class,
Determine the items type in the setter method of items
Then inherit 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 items types in the 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 (); // obtain the content in the jsp file
StringWriter sw = new StringWriter (); // gets a stream object
Jf. invoke (sw); // put the content in the Stream Object
String s = sw. toString (); // convert jsp content into a String
S = filter (s); // get the characters after escaping
This. getJspContext (). getOut (). write (s); // write to the browser
}
Public String filter (String message) {// escape the 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 ());
}
}

The next step is 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 the Jsp Tag file is 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.