JSP Custom Labels

Source: Internet
Author: User
Tags tld

JSP Custom Labels

How to build a simple tag processor?

There are three things to do:

First, write the label processor (Java file)

Second, describe the tag (TLD file) in the tag library descriptor file.

Third, reference the tag in the JSP file

Specific steps:

Step1: Writing a class that extends Simpletagsupport

Package foo;  Import javax.servlet.jsp.tagext.SimpleTagSupport;  Mort Import ... class//place Tag processing code here}       

Step2: Implementing the Dotag () method

Throws Jspexception, IOException {         // Print In response "This is xxxxxx" Getjspcontext (). Getout (). Print (" This is xxxxxx ");}   

Step3: Create a TLD for the tag (taglib description, Tag Library descriptor )

<?XML version= "1.0" encoding= "Iso-8859-1"?><Taglibxmlns= "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> <>foo. Simpletagtest1</tag-class > <body-content >empty</ Body-content> </ tag></taglib>          

STEP4: Deploying the Tag processor and TLD

Place the TLD file under Web-inf and place the tag processor under the web-inf/classes, which of course follows the package directory structure. In other words, the tag processor class is placed in the same location as all other Web application Java classes.

STEP5: Writing a JSP that uses tags

<%@ taglib Prefix="mytags Uri= "simpletags" %><html> Span style= "color: #0000ff;" ><body> << Span style= "color: #800000;" >mytags:simple1 %> body></< Span style= "color: #800000;" >html>           

The name in the URI is consistent with the name of the URI in the TLD file.

At this point, a simple custom label is created.

There are several common scenarios for customizing labels , namely:

The marked body (e.g., <x:label>...</x:label>, "..." as the body of the tag)

The expression is used in the marker body (such as,<x:label> ${movie} </x:label>, "${movie}" is the El expression that appears in the body of the tag)

Third, the cyclic implementation of the label body

Iv. Simple labels with attributes (e.g., <x:label movie= "${movie}"/>)

The following are some examples of these situations:

situation One: The writing is marked with body (body) , such as:

<mytags:simple2>    This is the body//This is the tagged body</mytags:simple2> 

In this case, in order to execute a statement inside the body, you need to add a sentence to the Dotag () method:

Getjspbody (). Invoke (null);

Invoke means "process the body of the token and print it to the response (response)".

Null means outputting the content to the response (response), rather than to what other writers are writing.

In addition , the "<body-content>empty</body-content>" column in the TLD also needs to be changed to:

<body-content>scriptless</body-content>    

The parameters of the four different body-content are then introduced.

case two, if the tag body uses an expression , such as:

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

Then the assignment of this expression needs to be done in the Dotag () of the label processor, such as:

Getjspcontext (). SetAttribute ("message", "Wear Sunscreen"); Getjspbody (). Invoke (null);  Be sure to remember to write this sentence, otherwise the tag body will not execute   

situation three, if you want to loop output a set of data , how should it be implemented? Such as:

 <table>< Span style= "color: #0000ff;" ><mytags:simple3> <tr><td>${movie}</ Td></tr>< Span style= "color: #0000ff;" ></mytags:simple3> </table>  

Obviously, you want to form a table by looping through different movie types. Then the Dotag () method of the tag processor should read:

string[] Movies = {"Monsoon wedding", "saved!", "... ..."}; throws Jspexception, IOException {for    (int i = 0; i < movies.length; i++) {getjspcontext (). SE Tattribute ("movie", Movies[i]); Getjspbody (). Invoke (null);  the label Body } will be executed once per invoke         

Situation four, if this simple tag is a property, what should I do? Such as:

<Table><Mytags:simple5Movielist= "${moviecollection}"><tr> <td>${movie.name}</ td> <td>${movie.genre}</ td> </tr></mytags: Simple5></ Table>           

Since attributes appear in the tag, the description in the TLD must reflect this, so the TLD should be adjusted to:

<?XML version= "1.0" encoding= "Iso-8859-1"?><Taglibxmlns= "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>--> < rtexprvalue>true </rtexprvalue> <!---- > </attribute</tag>< Span style= "color: #0000ff;" ></taglib>      

In addition, in the tag processor class, this attribute should also be reflected in the corresponding:

Extends simpletagsupport{       private List movielist;    public void Setmovielist (List movielist) {        this.movielist = movielist;    }    void Dotag () ....}   

Add :

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

①empty

That is, the marker body is empty

②scriptless

This tag cannot have script elements, but it can have template text and El, and it can be custom and standard action

③tagdependent

The marker body is treated as plain text, so the El is not counted, nor does the departure marker/action

④jsp

Anything that can be placed in a JSP can be placed in this tag body.

"Turn from: http://www.cnblogs.com/elaron/archive/2012/10/10/2717797.html"

JSP Custom Labels

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.