Jsp traversal of all data labels and escape labels

Source: Internet
Author: User

1. Develop tags for traversing all types of data

[Java]
<Span style = "color: #009900; BACKGROUND-COLOR: # ffffff"> tag processing class:
</Span>
<Span style = "BACKGROUND-COLOR: # ffffff"> package com. csdn. web. example;
 

 
Import java. io. IOException;
 
Import java. lang. reflect. Array;
 
Import java. util. ArrayList;
 
Import java. util. Collection;
 
Import java. util. HashMap;
 
Import java. util. Iterator;
 
Import java. util. Map;
 

 
Import javax. servlet. jsp. JspException;
 
Import javax. servlet. jsp. tagext. SimpleTagSupport;
 

 
Publicclass ForEachAll extends SimpleTagSupport {
 

 
Private Collection collection;
 

 
Private String var;
 
Private Object items;
 

 
Publicvoid setVar (String var ){
 
This. var = var;
 
}
 

 
Publicvoid setItems (Object items ){
 
This. items = items;
 
}
 

 
@ Override
 
Publicvoid doTag () throws JspException, IOException {
 
// Determine whether the three judgments under Map can be used in the doTage () method or setItems () method.
 
If (itemsinstanceof Map ){
 
// Here, we need to convert the attributes passed in from the jsp page to the Map type rather than the new HashMap
 
Map map = (Map) items;
 
Collection = map. entrySet ();
 
}
 
// Determine whether it is set or list
 
If (itemsinstanceof Collection ){
 
Collection = (Collection) items;
 
}
 
// Determine whether it is an array or an array
 
If (items. getClass (). isArray ()){
 
Collection = new ArrayList ();
 
Int len = Array. getLength (items );
 
For (int I = 0; I <len; I ++ ){
 
Collection. add (Array. get (items, I ));
 
}
 

 
}
 
Iterator it = collection. iterator ();
 
While (it. hasNext ()){
 
Object obj = it. next ();
 
This. getJspContext (). setAttribute (var, obj );
 
This. getJspBody (). invoke (null );
 
}
 
}
 

 
}
 
Jsp file
 
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
 
<% @ Taglib uri = "example" prefix = "example" %>
 
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
 
<Html>
 
<Head>
 

 
<Title> traverse various types of data </title>
 

 
</Head>
 

 
<Body>
 
<%
 
List list = new ArrayList ();
 
List. add (1 );
 
List. add ("aa ");
 
List. add ("bb ");
 
List. add (2 );
 
Request. setAttribute ("list", list );
 
%>
 
<Example: forEachAll var = "str" items = "$ {list}">
 
$ {Str} <br>
 
</Example: forEachAll>
 
<Hr>
 

 
<%
 
Map map = new HashMap ();
 
Map. put ("1", "aa ");
 
Map. put (2, "aa ");
 
Map. put (3, "aa ");
 
Map. put (4, "aa ");
 
Request. setAttribute ("map", map );
 
%>
 
<Example: forEachAll items = "$ {map}" var = "map">
 
$ {Map. key} ------- $ {map. value} <br>
 
</Example: forEachAll>
 
<Hr>
 

 
<%
 
String [] strs = {"asd", "fff", "v", "tt "};
 
Request. setAttribute ("strs", strs );
 
%>
 
<Example: forEachAll items = "$ {strs}" var = "str" >$ {str} <br> </example: forEachAll>
 
<Hr>
 

 
<%
 
Int [] I = {1, 2, 3, 4 };
 
Request. setAttribute ("I", I );
 
%>
 
<Example: forEachAll items = "$ {I}" var = "num" >$ {num} <br>
 
</Example: forEachAll>
 
</Body>
 
</Html>
 
Note: The description file is the same as the forEach tag defined in the previous blog. I will not list it here. If you do not know it, you can go to the previous blog.
 
2. Case study of html Escape tags:
 
Tag processing class:
 
Package com. csdn. web. example;
 

 
Import java. io. IOException;
 
Import java. io. StringWriter;
 

 
Import javax. servlet. jsp. JspException;
 
Import javax. servlet. jsp. tagext. JspFragment;
 
Import javax. servlet. jsp. tagext. SimpleTagSupport;
 

 
Publicclass HtmlFilter extends SimpleTagSupport {
 

 
@ Override
 
Publicvoid doTag () throws JspException, IOException {
 
JspFragment jf = this. getJspBody ();
 
StringWriter sw = new StringWriter ();
 
Jf. invoke (sw );
 
String s = sw. toString ();
 
S = filter (s );
 
This. getJspContext (). getOut (). write (s );
 
}
 
// This template file is also available in tomcat. You can refer to it. Transfer tag template file:
 
D: \ java \ Tomcat \ apache-tomcat-6.0.18 \ webapps \ examples \ WEB-INF \ classes \ util
 

 
Public String filter (String message ){
 

 
If (message = null)
 
Return (null );
 

 
Char content [] = newchar [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 ());
 

 
}
 

 
}
 
JSP file
 
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
 
<% @ Taglib uri = "example" prefix = "example" %>
 
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
 
<Html>
 
<Head>
 

 
<Title> Custom escape tag </title>
 

 
</Head>
 

 
<Body>
 
<Example: HtmlFilter>
 
<A href = ""> Custom escape tag </a>
 
<Span style = "BACKGROUND-COLOR: # ffffff"> </span> <span style = "BACKGROUND-COLOR: # ffffff"> </span> </example: HtmlFilter>
 
</Body>
 
</Html>
</Span>

 

3. No error is reported for custom tags, but the cause and handling of incorrect results are as follows:

Case 1: Check whether the taglib command on your jsp page has been written. If so, check whether your uri path is correct. If both are correct, check whether the tag in your tld description file is correctly defined, whether the uri class path in the tag is correct, and whether the names are consistent.

Case 2: Check whether your tag processing class is correct. The main error is whether you forget to write the code output to the browser. There are two forms: 1 ). this. getJspContext (). getOut (). write (s); 2 ). this. getJspBody. invoke (null); the two outputs are different, respectively. The latter is JspFragment www.2cto.com

The output of class objects directly outputs null, which is equivalent to the output of scenario 1. Note that the invoke () method can be directly output to the browser or pass a stream parameter to it, the StringWriter character output stream is commonly used. These two types of output must be used together, for example:

SpFragment jf = this. getJspBody ();

StringWriter sw = new StringWriter ();

Jf. invoke (sw );

String s = sw. toString ();

S = filter (s );

This. getJspContext (). getOut (). write (s );

But most people forget to output the browser code in the last sentence. This is a common mistake.

4. Package the tag Library

Generally, you can directly package the package, but sometimes the package cannot be used. You also need to import the jar package of the class file required by your class file. At this time, you need to go to tomcat, for example, I am importing the jsp-api.jar and servlet-api.jar files under the path D: \ java \ Tomcat \ apache-tomcat-6.0.18 \ lib.

To add a custom tag to a jar package, you need to add the bytecode of the tag processing class and the description file of the tag library to a jar package according to a certain storage method. The procedure is as follows. Organize the tag processing class bytecode and tag library description files in the following structure.

 

The label library descriptor file should be placed in the META-INF directory of the jar file (here pay attention to the META-INF writing do not mistakenly write MEAT-INF, I love fan this low-level error, must be correct, A slight write error will cause the packaged package to be unavailable); the label handles the class bytecode root directory and the META-INF directory are placed at the same level.

You can use the myeclipse tool to package it in reverse mode. The previous blog has introduced it in detail. Here I will simply say: Right-click the export project and select the jar file under java. As shown in

 

 


As shown in. classpath and. the project is not required for the jar package. You do not need to check the project, and then click Browse to select a path to output the jar package. This completes the packaging. Note: create a java project and package it. The jar package is a war package from a web project.

You can also use the jar command to create a jar file, the procedure is as follows: jar cvf mytaglib_0.9.jar META-INF (description file) com (class bytecode) after completing these two steps, A custom tag library jar package is ready, you can add it to any WEB-INF/lib directory of Web applications that want to use this tag library used.

 

 

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.