Create a JSP custom label __jsp

Source: Internet
Author: User
Tags blank page tld

In JSP programming, struts is typically used to handle form and business logic, and struts uses a number of custom tags, such as HTML, beans, and so on, but many times we still need to use statements such as "<% Java code%>" to manipulate some output. To reduce this code, you can create custom tags.

A tag is an ordinary Java class, and the only special thing is that it must inherit TagSupport or Bodytagsupport classes. These two classes provide methods that are responsible for the interaction between the JSP page and the class you write, such as input, output. These two classes are provided by the JSP container and need not be implemented by the developer themselves. In other words, you just have to inherit the class that implements the business logic TagSupport or Bodytagsupport, and do some special work, your class is a tag. And it is responsible for the interaction with the JSP page, do not worry about you.

The following describes how to create a custom label.
A Write Java class inheritance TagSupport or TagSupport
1 provides the set method of the property,
This property can then be set on the JSP page. Take the JSTL tag as an example <c:out value= ""/>, this value is the JSP data to the entrance between the tag. So tag must have a SetValue method, the specific properties can not be called value. For example SetValue (String data) {this.data = data;}
The name of this "value" is defined in the TLD. What name can be taken, just tag to provide the appropriate set method can be.
2) Handling doStartTag or Doendtag
These two methods are provided by TagSupport. Or take <c:out value= "" "/> as an example, when the JSP parsing this label, in the" "" Trigger the doStartTag event, "" "when the Doendtag event. Typically, logical operations are performed in the doStartTag and the output is controlled in the Doendtag.
The following is a simple example
Code package com.test.tag;    import java.io.ioexception;    import javax.servlet.jsp.jsptagexception;    import javax.servlet.jsp.tagext.bodycontent;    import javax.servlet.jsp.tagext.bodytagsupport;   /**   * permissionbodytag  show body content    */   Public class based on permissions  permissionbodytag extends bodytagsupport {       boolean  permission;        public boolean ispermission ()  {            return permission;       &nbsp}        public void setpermission ( boolean permission)  {           this.permission  = permission;        }        pubLic int dostarttag ()  throws jsptagexception {            if  (permission)  {                return EVAL_BODY_INCLUDE;            } else {                return SKIP_BODY;           &nbsp}        }        public int doendtag ()  throws jsptagexception {           system.out.println ("Doendtag");            try {                if  (bodycontent != null)  {     &nbSp;             bodycontent.writeout ( Bodycontent.getenclosingwriter ());                }else{                }             } catch  (ioexception e)  {                throw new jsptagexception ("IO ERROR:"  +  E.getmessage ());            }             return EVAL_PAGE;       &nbsp}        public void doinitbody ()  throws jsptagexception {       }         public void setbodycontent(bodycontent bodycontent)  {            this.bodycontent = bodycontent;        }   }  

Usually you will find that most of your activities are concentrated in doStartTag or Doendtag methods. Indeed, it is easy to write taglib after familiarity with some interfaces and classes. As the author of JSP Design says: The logic inside is a little more complicated, but it's not as hard as the rockets.

Two Writing TLD files
The tag file is actually just an XML-formatted description file, and the content is simple.
Create Tagtest-html.tld file in this tag file, we name the label we created Tagtest and declare the type and parameters (permission). Save the file under/web-inf.
Code  <?xml version= "1.0"  encoding= "UTF-8"?>   <! doctype taglib public  "-//sun microsystems, inc.//dtd jsp tag library  1.1//en " " HTTP://JAVA.SUN.COM/J2EE/DTDS/WEB-JSPTAGLIBRARY_1_1.DTD ">   <taglib>        <tlibversion>1.2</tlibversion>        <jspversion>1.1</jspversion>       <shortname>tagTest< /shortname>       <uri>/taiji</uri>        <tag>           <name>permission</name >           <tagclass>com.test.tag.permissionbodytag </tagclass>           <bodycontent>jsp</ bodycontent>           <attribute>                <name>permission</name>                <required>true</required>                <rtexprvalue>true</rtexprvalue>            </attribute>       </tag>    </taglib>      

Three Add to Web.xml.
Of course, we also need to add our custom tags to the web.xml, otherwise we will not be able to use them.
Code <taglib> <taglib-uri>/tagtest </taglib-uri> <taglib-location>/web-inf/tagtest-html. Tld</taglib-location> </taglib>

Four. import TLD on JSP page
Code <%@ taglib uri= "/tagtest" prefix= "Tagtest"%>

This way, your JSP page will be able to use your own tag. You can use this tag to determine whether the JSP content of the tag package is output according to the Boolean value of the descendant.
Code <tagtest:permission permission= "<% java code%>" >//need to have permission to output, users can see the content </tagTest:permission>

Advantages
1. The modified JSP file is no longer or contains as little Java code as possible. The HTML code becomes very clean and the page designer changes the paging file without worrying about accidentally deleting the program code.
2. Program developers use a simple blank page can be developed, when the page designer submits the target page, only a simple edit to complete the replacement

Eventually make the page and code as separate as possible.

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.