JSP Custom Tag Tag

Source: Internet
Author: User
JSP custom tag tag

the benefits of customizing the tag tag
Programmers can customize some specific function of the tag, to encapsulate the code, to achieve division of labor, reusability and other benefits.
how to store tag tags
The tags folder is usually created under the WEB Engineering Web-inf folder to store the custom tag, such as/web-inf/tags
The syntax of the tag tag
To know how to define the tag tag, you need to know the basic properties of the tag tag, such as:

<%@ tag body-content= "Empty" trimdirectivewhitespaces= "true" pageencoding= "UTF-8"%>

Usually we add the above code at the beginning of the *.tag file to tell the JSP container that this is a tag tag file.

Body-content has the following three properties:
1. Empty: This is an empty tag.
2. Scriptless: The tag body can have content, and the JSP container will handle the JSP elements inside, which can be text, El expressions, standard actions, and even another custom tag.
3. Tagdependent: Tag bodies can have content, and the JSP container treats them as pure files

Trimdirectivewhitespaces= "true" to delete extra empty lines
Pageencoding= "UTF-8" indicates the page's encoded format

Attribute can also be defined in the tag, for example:

<%@ attribute name= "x" required= "true" rtexprvalue= "true"%>
<%@ attribute name= "y" required= "true" Rtexprvalue= "true"%>

Attributes are described as follows:
1. Name: The title of this attribute.
2. Required:true/false, whether necessary.
3. Rtexprvalue:true/false, this attribute can use El expression, otherwise it is plain text.
4. Type: Set this attribute type, the JSP container will automatically convert the result to this class.

Simple to say the above definition is not a bit confused. The farmer chooses the bean species--for example (grain) as the certificate:
First, show the directory structure of the tag in the Web project,

Let's take a look at the index.jsp code:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
    pageencoding=" UTF-8 "%> <%@ taglib prefix="
C "uri=" Http://java.sun.com/jsp/jstl/core " %>
<%@ taglib tagdir= "/web-inf/tags/" prefix= "Yu"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

The Add.tag file is referenced in the index.jsp and look at the definition of the Add.tag file:

<%@ tag body-content= "Empty" trimdirectivewhitespaces= "true" pageencoding= "UTF-8"%>, <%@ taglib prefix=
" C "uri=" Http://java.sun.com/jsp/jstl/core "%>
<%@ attribute name=" x "required=" true "rtexprvalue=" true "% >
<%@ attribute name= "y" required= "true" rtexprvalue= "true"%>
<div>
    

Run up the program let's look at the results of the run:

At this point, basically already understand the tag file use it, then we do further study.
Continue to write index.jsp code to verify the tag file for body-content= "scriptless":

<!--test Tag [body-content= "Scriptless"]-->
<yu:scriptless_tag> This
tag ' s body-content is [ Scriptless], let ' s have a test, I ' m here.<br/>
<label>contextPath:<label/> ${ PageContext.request.contextPath}
</yu:scriptless_tag>

Let's take a look at the definition of the Scriptless_tag.tag file:

<%@ tag body-content= "scriptless" trimdirectivewhitespaces= "true"  pageencoding= "UTF-8"%>
<div Style= "background-color:red; Width=auto ">my backgroud color is red</div>
<jsp:doBody></jsp:doBody>
<div style=" Background-color:blue; width=70px; " >my Backgroud Color is blue</div>

Careful friend found that there is more than a \<\jsp:dobody> tag, the role of the label is to speak index.jsp in the tag file tag of the body displayed in the place, to see the results of the program run, at a glance:

The body of the Scriptless_tag tag tag in the index.jsp file is displayed between the red background and the blue background, and the Code ${pagecontext.request.contextpath} is parsed into/yutesttag

Continue to write index.jsp code to verify the tag file for body-content= "Tagindependent":

<!--test Tag [body-content= "Tagindependent"]-->
<yu:tagdependent_tag> This
tag ' s body-content is [tagindependent], let's have a test, I ' m here.
<label>contextPath:<label/> ${pagecontext.request.contextpath}
</yu:tagdependent_tag>

The definition of a Scriptless_tag.tag file is basically the same as the content of Scriptless_tag.tag:

<%@ tag body-content= "Tagdependent" trimdirectivewhitespaces= "true" language= "java" pageencoding= "UTF-8"%>
<div style= "background-color:red width=auto" >my backgroud color is red</div> <jsp:dobody
></jsp:doBody>
<div style= "background-color:blue; width=70px;" >my Backgroud Color is blue</div>

Keep looking at the results of the operation:

Code ${pagecontext.request.contextpath} is not parsed, just as a plain text file

Finally, attach the complete code of INDEX.JSP:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> <% @ taglib tagdir= "/web-inf/tags/" prefix= "Yu"%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

To this I believe that the difference between the three types of tag is already obvious, first of all, simply write to this, if there is a mistake, please leave a message to add a note.

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.