14--jsp Traverse all data labels and escape tags __js

Source: Internet
Author: User

1.  development traverses all types of data labels

Label Processing class: 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 {//whether the three judgements below the map can be in the Dotage () method can also be in the SE Titems () method if (itemsinstanceof map) {///Here the property to be passed in the JSP page is strongly converted to the map type and cannot be new HASHMAP map map = (map) item

        S      

      Collection = Map.entryset (); }//Determine if set, list if (itemsinstanceof Collection) {Collection = (Collection) items;

        ///Judge whether it is an array, various arrays 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); The JSP file <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <%@ taglib "uri=" Exa Mple "prefix=" Example "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  


3. Custom labels do not complain, but the result is not correct and processing:

situation One: Check your JSP page on whether the taglib instructions are written , if written, check your URI path is correct, if all correct, and then look at the tag tag in your TLD description file is defined correctly, it is the URI class path is correct, The name is consistent.

situation Two: Check your label processing class is correct, the main error has you have not forgotten to write output to the browser code , there are two forms: 1. This.getjspcontext (). Getout (). write (s); 2). This.getJspBody.invoke (null); These two outputs are different outputs, the latter being jspfragment

The output of the class object, direct output null is equivalent to the output of a situation; and one thing to note is: Invoke () method can be directly output to the browser can also pass a flow parameter, commonly used is the StringWriter character output stream, which is the two output to be used together, such as:

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 will forget the last line of output browser code, which is the situation two common mistakes

4. Packaging Tag Library

In general, it is OK to package directly, but sometimes the package can not be used, but also need to import your class file to need the type of file jar package, you need to go under Tomcat, such as mine is D:\java\Tomcat\apache-tomcat-6.0.18\ Lib this path to import Jsp-api.jar and Servlet-api.jar these two files.

The custom label is typed into a jar package to add the byte code of the label processing class and the tag library description file to a jar package in a certain way . The specific approach is as follows. The label processing class byte code and tag library description file are organized as shown in the following structure.

The tag library descriptor file is placed in the Meta-inf directory of the jar file (note here Meta-inf write not mistakenly written meat-inf, I love fan this low-level error, must be correct, a little bit wrong will lead to the package can not be used) The label handles the root directory of the class bytecode and the Meta-inf directory peer placement.

You can use the MyEclipse tool to reverse the package , the previous blog specifically introduced, here a simple one: is the right to export the project, select the Java jar file. As shown in the following figure

As shown in the figure above, the. Classpath and. Project are not required by the jar package, you can not enter the package, so do not check, and then click Browse to select a path output jar package, this completes the package, Note: This is to build a Java project and then package. It was a war package that was typed out of the Jar Pack Web project.

You can also use the JAR command to create a jar file , as follows: Jar CVF Mytaglib_0.9.jar meta-inf (description file) COM (class byte code) after 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.

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.