Jsp custom tags and Methods

Source: Internet
Author: User
Tags tld

Jsp custom tags and Methods

Detailed description of Jsp custom tags and Methods

First, there must be a Tag processing class. The Tag processing class can directly implement the Tag interface, or inherit the TagSupport class already implemented in Java. TagSupport also inherits the self-Tag interface, it has implemented the Tag interface internally, generally inheriting the TagSupport class, and then rewriting the doStartTag and doEndTag methods of the parent class,

For the start tag, the returned values include EVAL_BODY_INCLUDE and SKIP_BODY. The former indicates the execution TAG body, and the latter indicates that the TAG body is skipped;
There are two main types of return values for the end tag: EVAL_PAGE and SKIP_PAGE. The former indicates that the content after the end tag is executed, and the latter indicates that the content after the end tag is ignored, there are two implicit attributes in the tag processing class inherited from the TagSupport class. One is parent, which indicates the processing class of the Upper-layer tag of the tag, and the other is pageContext, pageContext is an attribute introduced to facilitate the use of WEB elements, such as HttpSession. classes that implement the Tag interface can also use these two implicit attributes, you only need to implement the setParent () method and setPageContext () method.

The following is the code style of a tag processing class:

Public class MyTag extends TagSupport {private String prop1; private String prop2;/*****/private static final long serialVersionUID =-8771409930058657336L; @ Override // There are two types of EVAL_PAGE and SKIP_PAGE returned values for the end tag. The former indicates that the content after the end tag is executed, and the latter indicates that the content after the end tag is ignored; public int doEndTag () throws JspException {// TODO Auto-generated method stub // You can process your own logic, and then select the return value. If not, call super. doEndTag () returns EVAL_PAGE // return EVAL_PAGE; // return SKIP_PAGE; return super. doEndTag () ;}@ Override/*** for the start tag, the returned values mainly include EVAL_BODY_INCLUDE and SKIP_BODY. The former indicates the execution TAG body, the latter indicates that the label body */public int doStartTag () {// TODO Auto-generated method stub // also executes its own logic, then, we decided to display the label body. // two internal attributes can be used: parent and pageContext if (yourCondition) return EVAL_BODY_INCLUDE; else return SKIP_BODY;} // omit the get and set Methods}

Next is to create under the WEB-INF. the tld file can be named at will. The tld file describes the tag library, including the tags and methods in the tag library, and the tag attributes, with this file, the system will load the file when the WEB server starts, then when the tag library is used on the page, the system will go to the tld file that has been loaded in the WEB-INF directory to find the corresponding tag library, if not, it will error!

Below is a tld file style:

<? Xml version = "1.0" encoding = "UTF-8"?> <Taglib xmlns =" http://java.sun.com/xml/ns/j2ee "Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Xsi: schemaLocation =" http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee /Web-jsptaglibrary_2_0.xsd "version =" 2.0 "> <tlib-version> 1.0 </tlib-version> <! -- Version --> <short-name> my </short-name> <! -- Name --> <uri> http://www.test.com/core </Uri> <! -- This uri is the uri when the page is introduced --> <function> <! -- This is the definition of the method. The definition of the method is relatively simple, as long as the method is public and static, this class does not need to inherit any classes and interfaces --> <name> funcTest </name> <! -- Method name --> <function-class> com. test. util. Util </function-class> <! -- Class where the method is located --> <! -- Some information about the method. boolean indicates the return type, funcName indicates the method name, followed by the parameter type. For other non-basic types of data, the full name must be used, such as java. lang. string --> <function-signature> boolean funcName (int, int, int) </function-signature> </function> <tag> <name> tagName </name> <! -- Tag name, which is previously used with this name --> <tag-class> com. test. web. tag. MyTag </tag-class> <! -- Tag class --> <attribute> <name> prop1 </name> <! -- Attribute name in the label class --> <required> true </required> <! -- Required? --> </attribute> <name> prop2 </name> <required> true </required> </attribute> </tag> </taglib>

Then you can directly introduce the tag library to use custom tags on the page:

For example:

<% @ Taglib prefix = "my" uri = "http://www.test.com/core" %> // use <my: myTag prop1 = "" prop2 = ""> content </my: myTag>

Jstl is used to use custom methods:

After the jstl label is imported, you can use the following code:

$ {My: funcName (arg0, arg1, arg2)}. The parameter type must be matched,

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.