Learning to write tags

Source: Internet
Author: User
Tags tld

Learning to write tags
// Wrote by bgiraffe 14-10-2006
// Example of this article download http://pickup.mofile.com/3643634964818862

Learning to write tag files ):

The purpose of custom tag is to allow programmers to customize tags for specific functions and encapsulate code to achieve division of labor and reusability.
Multiple benefits.

The use of tag files is added to JSP specification 2.0, eliminating the need for custom tags to be a Java class and
It is difficult to add a tag description file (TLD) encoding.

The following describes how to use the Tag file.

1. Storage:

A Tag file uses tags as the suffix name, and stores the same type of tag in the same folder to form a tag library.
"/WEB-INF/tags/" Save.

For example, next I plan to create a set of markup libraries related to mathematics, I create a "maths" folder under "/WEB-INF/tags/", however
Then you can create a Tag file named tag under "/WEB-INF/tags/Maths.

Now I want to add a number and name it "Add. Tag". Put it in the maths tag library.

2. Syntax:

The tag file is actually a JSP file, so the syntax is basically the same as that of JSP. Add the following element to the first line to tell
The server is a Tag file:

<% @ Tag %>

We also need to add some attributes to tell the server the settings of this Tag file:

1) body-content-set the subject content type of the tag:

A. Empty
This is an empty tag.
B. scriptless
The subject can have content, while the JSP Container will process the JSP elements in it. In other words, it can be
Text, El expressions, standard actions, and even other custom tags.
C. tagdependent
The subject can have content, while the JSP Container treats them as pure files.

2) pageencoding-sets the encoding of this tag.

Our Add Tag is an empty tag, and the encoding is "UTF-8", so Add the following sentence:

<% @ TAG body-content = "empty" pageencoding = "UTF-8" %>

The following elements are used to declare attributes in the tag:

<% @ Attribute %>

Attribute elements also have several attributes:

1) name-the attribute name.

2) required-True/false, required or not.

3) rtexprvalue-True/false. Can this attribute use an El expression? Otherwise, it is plain text.

4) type-set the type of this attribute. the JSP Container will automatically convert the result to this class.

Our Add. Tag has two attributes, representing two numbers to be added, X and Y. They are both required and can be expressed using the El expression:

<% @ Attribute name = "X" required = "true" rtexprvalue = "true" %>
<% @ Attribute name = "Y" required = "true" rtexprvalue = "true" %>

So how should we read these passed attributes when all the tags are set? In fact, it is very simple, marking a file is
JSP files, the input property values are stored in the scope, so you can use the El expression:

$ {X + y}

This markup file is complete, and the following is the complete code, you can also open "/WEB-INF/tags/Maths/Add. Tag" directly ":

<% --
Maths tag library Add Tag

Function: adds input parameters.
Parameters:
X, Number 1, required
Y, number 2, required
-- %>

<% @ TAG body-content = "empty" pageencoding = "UTF-8" %>

<% -- Declare Attributes -- %>
<% @ Attribute name = "X" required = "true" rtexprvalue = "true" %>
<% @ Attribute name = "Y" required = "true" rtexprvalue = "true" %>

<% -- Content -- %>
$ {X + y}

After marking the file, how can we call it in the JSP file? First, import the maths tag library and call the standard action.
Use the taglib command, but change the URL attribute to tagdir, specify the location of the custom tag library, and give it a prefix.
The following uses "maths ":

<% @ Taglib tagdir = "/WEB-INF/tags/Maths/" prefix = "maths" %>

Then we can call the standard action to use it. Of course, the required attributes must be available, or an error will be reported:

<Maths: Add x = "10" Y = "10"/>

The complete code for calling the JSP page marked by add is as follows. Similarly, you can directly open "/Add. jsp ":

<% -- Maths mark library add mark test -- %>
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" %>
<% -- Call the custom tag library maths -- %>
<% @ Taglib tagdir = "/WEB-INF/tags/maths" prefix = "maths" %>

<HTML>
<Head>
<Title> maths tag library Add Tag test </title>
</Head>
<Body>
Result: <br/>
<% -- Mark with add -- %>
<Maths: Add x = "10" Y = "10"/>
</Body>
</Html>

I believe everyone understands the basic working principles? You can regard a Tag file as a JSP page, and you can input some parameters to it,
The page that calls it is like containing it, so you can mark the file with "helloworld", an HTML code, or even a package
With other actions, the page for calling it will be displayed. You can open "/helloworld. jsp" and check that it calls another
Mark the helloworld mark in the others library.

The Add Tag is an empty tag. We have learned body-content = "scriptless". How should we handle the number of labeled subjects?
What about data? Let's add a new tag library named string, and then add a Tag file "show. Tag". Similarly, add the tab
This tells the server that this is a Tag file. This time, the body-content attribute is changed to scriptless:

<% @ TAG body-content = "scriptless" pageencoding = "UTF-8" %>

Then we add a standard action:

<JSP: Dobody>

It is used to read the subject content of the tag. When no variable is specified, it directly outputs the content to the called page.

We will write a "/show. jsp" to simply call this show Tag:

<% -- String mark library show Mark test -- %>
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" %>
<% -- Call the custom tag library string -- %>
<% @ Taglib tagdir = "/WEB-INF/tags/string" prefix = "string" %>

<HTML>
<Head>
<Title> string tag library trim tag test </title>
</Head>
<Body>
<% -- Mark with show -- %>
<String: Show>
This is the marked subject content
</String: Show>
</Body>
</Html>

<JSP: Dobody> three attributes are available:

1) var-when this variable is specified, the content of the read body will be saved as a string, and the page will not be output directly.
Area.

2) varreader-similar to VaR, but not a string, but a java. Io. Reader.

3) Scope-the scope to which the variables are stored, including page, request, session, and application. The default value is
Page.

Next we will expand the string tag library, add a new tag "trim. Tag", add tag commands, and set
Body-content is scriptless, which indicates that it can have subject content, because we will use the core tag library, so we add taglib
Import the command to the core tag library and add the JSP: Dobody command to read the body content and save it to the variable body:

<% @ TAG body-content = "scriptless" pageencoding = "UTF-8" %>

<% -- Import core tag library -- %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>

<% -- Save the body content to the body variable. It is stored in page -- %>
<JSP: Dobody Var = "body"/>

We have obtained a string of the subject content, so we can write a piece of code to remove the blank space in the string, we can use the core
A token fortokens In the heartbeat library is used to separate the string from the blank space, and then output the result.
The complete code of this tag can also be opened directly "/WEB-INF/tags/string/trim. Tag ":

<% --
String tag library trim tag

Function: removes the white space in the internal text of the marked subject.
-- %>

<% @ TAG body-content = "scriptless" pageencoding = "UTF-8" %>

<% -- Import core tag library -- %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>

<% -- Save the body content to the body variable. It is stored in page -- %>
<JSP: Dobody Var = "body"/>

<% -- Remove the white space of the subject string -- %>
<C: fortokens Var = "char" items = "$ {body}" delims = "" >$ {Char} </C: fortokens>

Note that the last line must be written in this way. What does this mean? Many people (including me) may write as follows:

<C: fortokens Var = "char" items = "$ {body}" delims = "">
$ {Char}
</C: fortokens>

This is a good habit. The code looks much more neat, but the JSP Container will replace the line feed with a blank one... haha, just try it yourself.
^

In this demonstration, we can see that the Tag file is not as troublesome as adding a TLD to the previous tag processing class, but also
File, we can import another tag library to expand the function, it is very convenient.

I believe everyone has a certain understanding of the basic application of the Tag file. Next let's look at other functions of the Tag file.

If we want to create some tag files for webpage users and the Tag file supports the attributes of all HTML elements, you can
It is time-consuming to create an attribute tag for every HTML Tag attribute,
Therefore, we can add the dynamic-attributes attribute in the tab command, indicating that this tag can allow any attribute value.
The JSP Container automatically saves all undeclared attributes to a map set named after your given values.
Save in page scope. We will create a new Tag file to the others tag library and add the following code lines:

<% @ TAG body-content = "empty" pageencoding = "UTF-8"
Dynamic-attributes = "attributeslist" %>

When this tag is called, all undeclared attributes will be saved to the attributeslist map set. We can use
The foreach of the core tag library lists them, as the complete code below, you can also open "/WEB-INF/tags/Others/
Showattributes. Tag ":

<% --
Others tag library showattributes tag

Function: Read the property set saved by dynamic-attributes and output it to the called page.
-- %>
<% @ TAG body-content = "empty" pageencoding = "UTF-8"
Dynamic-attributes = "attributeslist" %>
<% -- Import core tag library -- %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>

<% -- Output all attributes -- %>
<P> This tag contains the following dynamic attributes </P>
<% -- Use foreach to traverse the "attributeslist" set and output it to the call page -- %>
<C: foreach Var = "item" items = "$ {attributeslist}" varstatus = "I">
<P >$ {I. index + 1}. $ {item. Key }:: {item. Value} </P>
</C: foreach>

Compile a JSP page and call the showattributes tag. The following is the complete code. You can also directly open
"/Showattributes. jsp ":

<% -- Test of showattributes tag of others tag library -- %>
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" %>
<% -- Call the custom tag library others -- %>
<% @ Taglib tagdir = "/WEB-INF/tags/Others/" prefix = "Others" %>

<HTML>
<Head>
<Title> others tag library showattributes tag test </title>
</Head>
<Body>
<Others: showattributes param1 = "hello" param2 = "on9" color = "red"/>
</Body>
</Html>

(Conversion-learning to write a Tag file)

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.