JSP custom tag

Source: Internet
Author: User
Tags tld

How to create a simple tag processor?

 

Three things need to be done:

I. Compile a tag processor (Java file)

2. Describe the tag (TLD file) in the tag library descriptor File)

3. reference the tag in the JSP file

Procedure:

Step 1: compile a class that extends simpletagsupport.

 
PackageFoo;ImportJavax. servlet. jsp. tagext. simpletagsupport;//Mort import...Public ClassSimpletagtest1 extands simpletagsupport {//Put the tag here for processingCode}

Step 2: implement the dotag () method

Public VoidDotag ()ThrowsJspexception, ioexception {//Print "this is xxxxxx" in response"Getjspcontext (). getout (). Print ("this is xxxxxx");}

Step 3: Create a TLD (Taglib description, tag library Descriptor)

 <?  XML version = "1.0" encoding = "ISO-8859-1"  ?>  <  Taglib  Xmlns  = "Http://java.sun.com/xml/ns/j2ee" Xmlns: xsi  = "Http://www.w3.org/2001/XMLSchema-instance"  Xsi: schemalocation  = "Http://java.sun.com/xml/ns/jsee/web-jsptagLibrary_2_0.xsd"  Version  = "2.0"  >      <  Tlib-version  > 1.2 </  Tlib-version  >      <  Uri > Simpletags </  Uri  >      <  Tag  >          <  Name  > Simple1 </  Name  >          <  Description  > XXXXXXXX</  Description  >          <  Tag-class  > Foo. simpletagtest1 </  Tag-class  >          <  Body-content  > Empty </  Body-content  >      </ Tag  >  </  Taglib  > 

Step 4: deploy the tag processor and TLD

Put the TLD file under the WEB-INF, and put the tag processor under the WEB-INF/classes, here of course also follow the package directory structure. In other words, the markup processor class should be placed in the same position as all other web application Java classes.

Step 5: compile a tag-based JSP

 <%  @ Taglib prefix  =  "  Mytags  " Uri  =  "  Simpletags  "   %>  <  Html  >         <  Body  >                  <  Mytags: simple1  % >         </  Body  >  </  Html  > 

The URI name must be the same as the URI name in the TLD file.

Now, a simple custom tag is created.

There are several common scenarios for custom tags, Respectively:

1. A signed tag (for example, <X: Label>... </X: Label>, "..." is the body of the tag)

2. The expression (for example, <X: Label >$ {movie} </X: Label>) is used in the TAG body, "$ {movie}" is the El expression in the label body)

Iii. Cyclic execution label body

4. Simple labels with attributes (for example, <X: Label movie = "$ {movie}"/>)

These situations are described as follows:

Case 1: Write a body tag.Such:

<Mytags: simple2>This is the body // This is the marked body</Mytags: simple2>

In this case, in order to execute the statement in the body, you need to add such a sentenceDotag ()Method:

 
Getjspbody (). Invoke (Null);

Invoke means "processing the tagged body and printing it to the response ".

Null means to output the content to the response, rather than to any other writer.

In addition,TLD"<Body-content>Empty</Body-content>"The column also needs to be changed:

<Body-content>Scriptless</Body-content>

The following describes four different body-content parameters.

Case 2: If the TAG body uses an expressionSuch:

 
<Mytags: simple3>Message is: $ {message}</Mytags: simple3>

Therefore, the value assignment of this expression must be performed in the tag processor'sDotag ()Completed, such:

Getjspcontext (). setattribute ("message", "Wear sunscreen"); Getjspbody (). Invoke (Null);//Remember to write this sentence, otherwise the TAG body will not be executed

Case 3: If you want to output data of a set cyclically, How should we implement it? For example:

    table  >    mytags: simple3  >    tr  >  TD  >$ {movie}    TD  >  tr  >    mytags: simple3  >    table  > 

Obviously, we want to generate a table by outputting different movie in a loop. Then Mark the processor'sDotag ()The method should be changed:

 
String [] movies = {"Monsoon Wedding", "saved! ","....."};Public VoidDotag ()ThrowsJspexception, ioexception {For(IntI = 0; I <movies. length; I ++) {Getjspcontext (). setattribute ("Movie", Movies [I]); getjspbody (). Invoke (Null);//Each invoke will execute a TAG body}}

Case 4: What if this simple tag has attributes? For example:

 < Table  >  <  Mytags: simple5  Movielist  = "$ {Moviecollection }"  >         <  Tr  >               <  TD  > $ {Movie. name} </  TD  >               < TD  > $ {Movie. Genre} </  TD  >          </  Tr  >  </  Mytags: simple5  >  </  Table  > 

Because attributes appear in the tag, the description in the TLD must reflect this situation. Therefore, the TLD should be adjusted:

 <? XML version = "1.0" encoding = "ISO-8859-1"  ?>  <  Taglib  Xmlns  = "Http://java.sun.com/xml/ns/j2ee"  Xmlns: xsi  = "Http://www.w3.org/2001/XMLSchema-instance"  Xsi: schemalocation  = "Http://java.sun.com/xml/ns/jsee/web-jsptagLibrary_2_0.xsd"  Version  = "2.0"  >      < Tlib-version  > 1.2 </  Tlib-version  >      <  Uri  > Simpletags </  Uri  >      <  Tag  >          <  Name  > Simple1 </  Name  >          <  Description  > XXXXXXXX </  Description  >          <  Tag-class  > Foo. simpletagtest1 </  Tag-class  >          < Body-content  > Empty </  Body-content  >          <  Attribute  >              <  Name  > Movielist </  Name  >              <  Required > True </  Required  >  <! --  This indicates that the movielist attribute is required.  -->              <  Rtexprvalue  > True </  Rtexprvalue  >  <! --  This indicates that the movielist attribute can be a runtime expression (not necessarily a constant string) -->       </  Attribute  >      </  Tag  >  </  Taglib  > 

In addition, this attribute must be reflected in the tag processor class:

Public ClassSimpletagtest5ExtendsSimpletagsupport {Private list movielist; Public void setmovielist (list movielist) {This. movielist = movielist ;}Public VoidDotag ()....}

 

Supplement:

There are four types of parameters that can be written in <body-content> </body-content>.:

① Empty

That is, the tag cannot be blank.

② Scriptless

This tag cannot contain script elements, but can contain template text, El, customization, and standard actions.

③ Tagdependent

The tag body must be plain text, so it does not calculate El or start to mark/action.

④ JSP

Anything that can be put in JSP can be put in this tag body

 

 

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.