Getting started with JSP custom tags,

Source: Internet
Author: User
Tags tld

Getting started with JSP custom tags,

This article shares with you the tutorials on getting started with JSP custom tags.

1. JSP custom tag:

Custom tags are custom JSP language elements. When a JSP page contains a custom tag, it is converted into a servlet, And the tag is converted into an operation on the object called tag handler, that is, the Web iner calls those operations when the servlet executes. JSP tag extension allows you to create new tags and insert them directly to a JSP page. The Simple Tag Handlers is introduced in the JSP 2.0 specification to write these custom tags. You can inherit the SimpleTagSupport class and override the doTag () method to develop a simple custom tag.

2. develop custom tags

Follow these steps to create a custom tag for the IP address of the tactical client:

Compile a common java class that inherits the SimpleTagSupport class,

Public class ShowIp extends SimpleTagSupport {/*** the following shielded code has been done in SimpleTagSupport code! You do not need to repeat it here! * // * Private JspContext context; * // *** input pageContext * // * @ Overridepublic void setJspContext (JspContext pc) {this. context = pc;} */@ Overridepublic void doTag () throws JspException, IOException {PageContext pageContext = (PageContext) this. getJspContext (); ServletRequest request = pageContext. getRequest (); String ip = request. getRemoteHost (); JspWriter out = pageContext. getOut (); out. write ("display customer ip addresses using custom tags" + ip addresses); List <String> a = null ;}}

Create the mytaglib. tld file in the WEB-INF directory of the web project, which is called the Declaration file of the 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"> <description> A tag library exercising SimpleTag handlers. </description> <! -- Version of the tag library --> <tlib-version> 1.0 </tlib-version> <! -- Tag library prefix --> <short-name> rlovep </short-name> <! -- Unique tag of a tld file --> <uri> http://rlovep.com </uri> <! -- Define the tag, which must be placed before the method --> <tag> <! -- Tag name --> <name> showIp </name> <! -- Tag processing class --> <tag-class> com. rlovep. tags. showIp </tag-class> <body-content> empty </body-content> </tag> </taglib>

Import the custom tag library in the header of the jsp page: the url you write in the tld, And the prefix is also defined in the tld file.

<%@ taglib uri="http://rlovep.com" prefix="rlovep" %>

Use custom tags in jsp

<% -- Test a simple custom tag. The TAG body (I am you) is not displayed -- %> <rlovep: showIp> I am you </rlovep: showIp>

3. Execution Process of custom tags

When accessing: http: // localhost: 8080/stuJsp/Hellotags. jsp; to restart Tomcat so that the server is started, load all the files under the WEB-INF directory of each web application !!! For example. Web. xml, tld file !!!
The procedure is as follows:

  • Check the taglib command of the jsp file to see if a tld file with a url of http://rlovep.com exists. If no, an error is returned.
  • Execute jsp file conversion: translate jsp file into java source file-> compile class-> construct class Object-> call _ jspService () method
  • Read the mytaglib. tld file to check whether there are labels with showIp.
  • Find the corresponding tag, read the content, and get com. rlovep. tags. ShowIp.
  • Construct a ShowIp object, and then call the dotag method in ShowIp;

4. Access TAG body

You can include the message content in a tag like a standard tag library. For example, we want to include content in our custom

The format is as follows:

<Rlovep: showIp> I am you </rlovep: showIp>

However, to display text, you need to modify the processing class and tld file:
Add the following content to the doTag method:

JspContext jspContext2 = this. getJspContext (); // two methods for displaying the label body // method 1 is called directly // jspBody. invoke (null); // method 2 is output to out // jspBody. invoke (jspContext2.getOut ());

Modify the tld file:

<Tag> <! -- Tag name --> <name> showIp </name> <! -- Tag processing class --> <tag-class> com. rlovep. tags. ShowIp </tag-class> <! -- The content format of the output tag body cannot be written into jsp java code. --> <body-content> scriptless </body-content> </tag>

Now you can display the content of the label body;

<% -- The tag display -- %> <rlovep: showIp> I am you </rlovep: showIp>

Content format of the output TAG body:
JSP:Used in Traditional labels. Java code of jsp can be written and executed.
Scriptless:The tag body cannot write jsp java code.
Empty:It must be an empty tag.
Tagdependent:The tag body content can be written into jsp java code, but it does not hold

5. Add attributes to the TAG body:

You can set various attributes in the Custom standard. To receive attributes, the custom value tag class must implement the setter method;

The format is as follows:

<! -- Test the label with attributes. The label body is displayed as processed by class --> <rlovep: AttributeTags name = "peace" value = "12345

To define an attribute, follow these steps:
Writing and processing class: AttributeTags extends SimpleTagSupport

Add two attributes: // declare the member variable private Integer value; private String name; and write the setter method for the two member attributes; public void setValue (Integer value) public void setName (String name)

Specify the tag and attributes in the tag library file tld:

<! -- Tag name --> <name> AttributeTags </name> <! -- Tag processing class --> <tag-class> com. rlovep. tags. AttributeTags </tag-class> <! -- The content format of the output TAG body cannot be used to write jsp java code --> <body-content> scriptless </body-content> <! -- Configure attribute name --> <attribute> <name> name </name> <! -- Required? --> <required> true </required> <! -- Whether EL expressions are supported --> <rtexprvalue> true </rtexprvalue> </attribute> <! -- Configure attribute value --> <attribute> <name> value </name> <! -- Required? --> <required> true </required> <! -- Whether EL expressions are supported --> <rtexprvalue> true </rtexprvalue> </attribute> </tag>

Now you can use tags with attributes.
You can configure the following attributes When configuring a tld:

6. Custom tags with sub-tags:

Just like the choose tag of the core tag library, we can also define nested custom tags. This section mainly describes how to create a choose tag similar to the core tag library. The procedure is as follows:

Create a processing class,The processing class is the same as the previous method. The getParent () method is used to obtain the parent tag;
Create three processing files: ChooseTag, OtherWiseTag, and whenTag

// ChooseTag class: public class ChooseTag extends SimpleTagSupport {// when this variable is not a tag attribute, it is changed by the when tag; obtained by othewise; private boolean flag; public boolean isFlag () {return flag;} public void setFlag (boolean flag) {this. flag = flag ;}@ Overridepublic void doTag () throws JspException, IOException {// The Choose label displays the label body and serves as the parent label of the other two labels; getJspBody (). invoke (null) ;}}// whenTag class public class whenTag extends SimpleTagSupport {// Add tes T attribute private boolean test; public boolean isTest () {return test;} public void setTest (boolean test) {this. test = test ;}@ Overridepublic void doTag () throws JspException, IOException {// if the tag attribute is true, the TAG body if (test) {getJspBody () is displayed (). invoke (null);} // set the parent tag to otherwise and use ChooseTag parent = null; if (getParent () instanceof ChooseTag) {parent = (ChooseTag) getParent (); parent. setFlag (test) ;}}// OtherWiseTag class: public c Lass OtherWiseTag extends SimpleTagSupport {@ Overridepublic void doTag () throws JspException, IOException {boolean test = true; // obtain the test of the parent tag, set if (getParent () instanceof ChooseTag) by his previous when {// obtain the test of the parent tag, set ChooseTag parent = (ChooseTag) by his previous when) getParent (); test = parent. isFlag ();} if (! Test) {getJspBody (). invoke (null );}}}

Write a tld file: same as other tag Definitions

<! -- Define tag, choose --> <tag> <! -- Tag name --> <name> choose </name> <! -- Tag processing class --> <tag-class> com. rlovep. tags. ChooseTag </tag-class> <! -- The content format of the output tag body cannot be written into jsp java code. --> <body-content> scriptless </body-content> </tag> <! -- Define tag, when --> <tag> <! -- Tag name when --> <name> When </name> <! -- Tag processing class --> <tag-class> com. rlovep. tags. whenTag </tag-class> <! -- The content format of the output TAG body cannot be used to write jsp java code --> <body-content> scriptless </body-content> <! -- Configure the property name --> <attribute> <name> test </name> <! -- Required? --> <required> true </required> <! -- Whether EL expressions are supported --> <rtexprvalue> true </rtexprvalue> </attribute> </tag> <! -- Define tag, Otherwise --> <tag> <! -- Tag name --> <name> otherwise </name> <! -- Tag processing class --> <tag-class> com. rlovep. tags. OtherWiseTag </tag-class> <! -- The content format of the output tag body cannot be written into jsp java code. --> <body-content> scriptless </body-content> </tag>

Tags using tape tags: they are slightly different from other tags and need to be nested.

<! -- Test choose --> <rlovep: choose> <rlovep: When test = "$ {10 <5}"> when the condition is set, execute When </rlovep: When> <rlovep: otherwise> the condition is not true. Execute otherwise </rlovep: otherwise> </rlovep: choose>

Here is an introduction to custom tags;

404 page, 505 page, error page configuration method:

You can configure the global 404 page, 505 page, and error page for your website in web. xml. The configuration method is as follows: Remember to create the corresponding jump file.

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0"> <! -- NULL pointer configuration exception --> <error-page> <exception-type> java. lang. nullPointerException </exception-type> <location>/error. jsp </location> </error-page> <! -- Configure the 505 error page --> <error-page> <error-code> 500 </error-code> <location>/common/500.jsp</location> </error-page> <! -- Configure the 404 error page --> <error-page> <error-code> 404 </error-code> <location>/common/404.html </location> </error-page> </web-app>

The introduction to JSP is here, and I hope it will help you learn it.

Articles you may be interested in:
  • Custom jsp Tag)
  • Detailed description of jsp struts1 tag instances
  • A simple example of a JSP custom tag
  • Summary of the implementation process of JSP custom tag Taglib
  • Use of common jsp labels
  • How can I implement cascade using the select tag on a JSP page?
  • Jsp base tag and meta tag learning Summary
  • How to format a timestamp string as a time tag in a jsp page
  • JSP custom pagination TAG

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.