Java JSP Custom Tags

Source: Internet
Author: User
Tags tld

1 Custom Labels 1.1 Introduction

Requirements: output to the browser the current customer's IP Address (use jsp tags only)

1.2 first custom label development steps

1) write a generic java class that inherits the Simpletagsupport class, called the tag processor class

/**

* Label Processor class

* @author APPle

* 1) inherit Simpletagsupport

*

*/

Public class Showiptag extends simpletagsupport{

Private Jspcontext context;

/**

* incoming pagecontext

*/

@Override

Public void setjspcontext (Jspcontext pc) {

this. Context = PC;

}

/**

* 2) cover Dotag method

*/

@Override

Public void Dotag () throws Jspexception, IOException {

Export the client's IP address to the browser

PageContext PageContext = (pagecontext) context;

HttpServletRequest request = (HttpServletRequest) pagecontext.getrequest ();

String IP = request.getremotehost ();

JspWriter out = Pagecontext.getout ();

Out.write (" Export customer's IP address using custom tags :"+ip);

}

}

2) Create a itcast.tld file in the web-inf directory of the Web Project , the TLD A declaration file called a tag library. (Refer to the TLD file of the core tag library )

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="Http://java.sun.com/xml/ns/javaee"

xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2 _1.xsd "

version="2.1">

<!-- Tag library version---

<tlib-version>1.1</tlib-version>

<!-- Tag library prefix ---

<short-name>itcast</short-name>

< unique tags for!-- tld files --

<uri>http://gz.itcast.cn</uri>

<!-- a label declaration--

<tag>

<!-- Label name --

<name>showIp</name>

<!-- full name of the tag processor class--

<tag-class>gz.itcast.a_tag. Showiptag</tag-class>

<!-- output tag body content format --

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

</tag>

</taglib>

3) Import the custom tag library in the header of the JSP page

<% @taglib uri="http://gz.itcast.cn" prefix="Itcast"%>

4) use a custom label in the JSP

<itcast:showIp></itcast:showIp>

1.3 The execution of custom labels

question: http://localhost:8080/day14/01.hellotag.jsp How do I access a custom label?

premise: when the Tomcat server is started, load to each Web app and load each Web app's web-inf all files under the directory!!! For example. Web. XML, TLD file!!!

1) Access to 01.hellotag.jsp resources

2 tomcat server put jsp file translated into java source file -> compilation CLASS-> Structural class object -> call _jspservice () method

3) Check the taglib instruction of the jsp file , whether there is a tld file named http://gz.itcast.cn . If not, then the error

4) Previous step has read the itcast.tld file

5) Read <itcast:showIp> to itcast.tld file for presence <name> <tag> tags for Showip

6) Find the corresponding <tag> tag, then read the <tag-class> content

7) Get gz.itcast.a_tag. Showiptag

Construction Showiptag object, and then call Showiptag inside the method

1.4 The lifecycle of custom label processor classes

Simpletag Interface:

void Setjspcontext (Jspcontext pc)-- set pagecontext object, incoming pagecontext (must be called)

get pagecontext object by Getjspcotext () method

void SetParent (Jsptag parent)-- Sets the parent tag object, passes in the parent label object, and does not call this method if there is no parent tag . gets the parent tag object through the GetParent () method.

void Setxxx ( value )-- sets the property value.

void Setjspbody (Jspfragment jspbody)-Sets the contents of the label body. The tag body content is encapsulated into the jspfragment object and then passed in to the Jspfragment object. the contents of the label body are obtained by Getjspbody () method . If there is no label body content, This method is not tuned

void Dotag ()- the method that is called when the label is executed. (must be called)

1.5 The role of custom tags

1) control the contents of the label body output

2) control whether the remaining contents of the label are output

3) control duplicate output label body contents

4) Change the contents of the label body

5) labels with attributes

Steps:

5.1 Adding an idiom variable and setter method to the label processor

1. declaring a member variable for a property

Private Integer num;

2. key point: Must provide a public Setter method used to assign a value to a property

Public void setnum (Integer num) {

this. num = num;

}

1.6 Output tag body content format

JSP: used in traditional tags. Java code that can write and execute JSP .

Scriptless: tag body can not write jsp java code

Empty: must be a blank label.

Tagdependent: The tag body content can write jsp java code, but will not execute.

Java JSP Custom Tags

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.