08. Learn JSP traditional tag programming in 10 minutes, 08jsp

Source: Internet
Author: User
Tags tld

08. Learn JSP traditional tag programming in 10 minutes, 08jsp

I. Recognize tags

1. Note: traditional tag programming is rarely used in development. Learning tag programming mainly aims to improve the knowledge system.

2. The main function of the tag: remove or reduce the java code in jsp

3. Main Components and operating principles of tags

4. Simple tag example: Inherit javax. servlet. jsp. tagext. TagSupport

Tag development procedure

4.1 compile a class that inherits TagSupport (or implements the Tag Interface)

Package com. chen. ying; import java. io. IOException; import javax. servlet. http. httpServletRequest; import javax. servlet. jsp. jspException; import javax. servlet. jsp. jspWriter; import javax. servlet. jsp. tagext. tagSupport; public class FirstTag extends TagSupport {public int doStartTag () throws JspException {System. out. println ("Call doStartTag method"); HttpServletRequest req = (HttpServletRequest) pageContext. getRequest (); JspWriter out = pageContext. getOut (); String ip = req. getRemoteAddr (); // get the IP address try {out through the request object. write (ip); // The output may throw an exception} catch (IOException e) {e. printStackTrace ();} return TagSupport. SKIP_BODY; // indicates skipping the label body }}

 

4.2 create a *. tld file in the WEB-INF directory to represent the tag library and describe the tag processing class in the *. tld File

<? 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"> <! -- Description: Used to add the description of taglib (TAG Library) --> <description> custom tag library developed by Chen Cheng </description> <! -- Taglib (TAG Library) version --> <tlib-version> 1.0 </tlib-version> <short-name> GaclTagLibrary </short-name> <! -- Set a uri for the custom tag library to indicate the tag library. The uri starts with a slash (/) and the content after the slash (/) is written at will, as shown in/chen, when referencing a tag library on the Jsp page, you need to use uri to find the tag library. On the Jsp page, you need to introduce the tag library as follows: <% @ taglib uri = "/chen" prefix = "anyword" %> --> <uri>/chen </uri> <! -- A taglib (tag Library) contains multiple custom tags, each of which is described using one tag --> <! -- A tag corresponds to a custom tag --> <tag> <description> This tag is used to output the Client IP address </description> <! -- Configure a tag name for the tag processor class. When tag is used on the Jsp page, the tag processor class to be called can be found through IP address. chen. ying. firstTag class --> <name> IP </name> <! -- The processor class corresponding to the tag --> <tag-class> com. chen. ying. firstTag </tag-class> <body-content> empty </body-content> </tag> </taglib>

4.3 Use custom tags on the jsp page

Use <% @ taglib uri = "uri of the tag library" prefix = "prefix of the tag" %> to introduce the tag library to be used. You can set any prefix.

 

Display result

The doStartTag () method is triggered every time the

From the code above, we can see that using tags can remove the java code in jsp

2. Define tags with attributes

1. Requirements

 

2. Complete a date format display operation, that is, display date Based on the date format template entered by the user.

2.1 compile a label class

Package com. chen. ying; import java. io. IOException; import java. text. simpleDateFormat; import java. util. date; import javax. servlet. jsp. jspException; import javax. servlet. jsp. tagext. tagSupport; public class DataTag extends TagSupport {private String format; public int doStartTag () throws JspException {SimpleDateFormat sdf = new SimpleDateFormat (this. format); // set the date format try {pageContext. getOut (). write (sdf. format (new Date (); // display Date in the specified format} catch (IOException e) {e. printStackTrace ();} return TagSupport. SKIP_BODY;} public String getFormat () {return format;} public void setFormat (String format) {// set this in the tag using the reflection mechanism. format = format ;}}

2.2 configure tags in the tag Library

 

2.3 use tags in jsp

 

3. Summary

 

Iii. TagSupport

1. Requirements

 

2. Main TagSupport attributes and Methods

 

Int doStartTag ()

 

Int doAfterBody ()

 

Int doEndTag ()

3. Tag interface Execution Process

 

4. Tag containing the TAG body: determines whether a specified attribute name has an attribute within a certain attribute range.

4.1 write a tag processor class

Package com. chen. ying; import javax. servlet. jsp. jspException; import javax. servlet. jsp. tagext. tagSupport; public class AttributeTag extends TagSupport {private String name; // The name Of The receiving attribute private String scope; // the range of the receiving attribute public int doStartTag () throws JspException {Object value = null; if ("page ". equals (this. scope) {// is the page attribute range value = pageContext. getAttribute (this. name, pageContext. PAGE_SCOPE);} if ("request ". equals (this. scope) {// whether the request attribute range value = pageContext. getAttribute (this. name, pageContext. REQUEST_SCOPE);} if ("session ". equals (this. scope) {// whether the session attribute range value = pageContext. getAttribute (this. name, pageContext. SESSION_SCOPE);} if ("application ". equals (this. scope) {// whether the attribute range value = pageContext. getAttribute (this. name, pageContext. APPLICATION_SCOPE);} if (value = null) {// indicates that this attribute does not exist and the label body return TagSupport is not executed. SKIP_BODY;} else {// execute the label body return TagSupport. EVAL_BODY_INCLUDE;} public void setName (String name) {this. name = name;} public void setScope (String scope) {this. scope = scope ;}}

 

4.2 configure tags in the tag Library

 

 

4.3 use tags in jsp'

 

Whether to execute the label body depends on the returned value.

 

5. Summary

 

Iv. Iteration labels

1. Requirements

 

 

In the MVC mode, it is emphasized that script code should not appear in a JSP file, because it will damage the structure of the program and make maintenance very troublesome, JSP files are only used to receive, judge, and output. Now we need to output the set in JSP. To avoid script code, we can use iterative labels.

2. Iterative tag development steps

2.1 write a tag processing class

Public class IteratorTag extends TagSupport {private String name; private String scope; private String id; // specify the attribute name private Iterator for saving each element in the set <?> Iter = null; public int doStartTag () throws JspException {Object value = null; if ("page ". equals (this. scope) {// is the page attribute range value = pageContext. getAttribute (this. name, pageContext. PAGE_SCOPE);} if ("request ". equals (this. scope) {// whether the request attribute range value = pageContext. getAttribute (this. name, pageContext. REQUEST_SCOPE);} if ("session ". equals (this. scope) {// whether the session attribute range value = pageContext. getAttribute (this. name, pa GeContext. SESSION_SCOPE);} if ("application ". equals (this. scope) {// whether the attribute range value = pageContext. getAttribute (this. name, pageContext. APPLICATION_SCOPE);} if (value! = Null & value instanceof List <?>) {// This attribute is available and is of the List type this. iter = (List <?>) Value ). iterator (); if (iter. hasNext () {pageContext. setAttribute (this. id, iter. next (); // Save the set element to return TagSupport In the attribute range of the specified property name. EVAL_BODY_INCLUDE; // executes the label body and outputs the set element through the id attribute name} else {return TagSupport. SKIP_BODY ;}} else {return TagSupport. SKIP_BODY ;}} public int doAfterBody () throws JspException {if (iter. hasNext () {pageContext. setAttribute (this. id, iter. next (); // Save the set element in the attribute range, and specify return TagSupport in jsp. EVAL_BODY_AGAIN; // if there are other elements, hand them to doAfterBody () for processing} else {return TagSupport. SKIP_BODY ;}} public void setName (String name) {this. name = name;} public void setScope (String scope) {this. scope = scope;} public void setId (String id) {this. id = id ;}}

Note: If you want to execute the label body once, the returned value is in doStartTag ().EVAL_BODY_INCLUDEIf you want to execute the label body multiple times, the returned value is in doAfterBody ().EVAL_BODY_AGAIN;

2.2 configure the tag processing class in the tag Library

 

Sometimes the following error occurs in tomcat:

Unable to find setter method ***

You only need to add the <type> property type </type> in the corresponding property configuration.

 

2.3 use tags in jsp

 

Id is used to specify the attribute names in the attribute range where each element in the list is stored, so that the attribute names can be output in the expression language.

 

Result 2.4

 

 

3. Summary

To operate an object by using a tag, perform the following steps: first, store the object in the attribute range in the servlet, And then: 1. Determine the attributes of the tag, such as the name of the object attribute to be operated, range 2. Get the object through the object property, and then process the object. You can save the processed result to the property range of the specified property name, and then display the processing result in the TAG body, for example, id = "person ".

 

5. BodyTagSupport class

1. Requirements

 

 

2. Definition

 

 

3. Main Expansion Methods

 

 

4. BodyContent class

 

 

5. Execution Process of the BodyTag Interface

 

 

6. TagExtraInfo class and VariableInfo class

 

 

Main Methods of the TagExtraInfo class

 

 

Main Methods of the VariableInfo class

 

 

7. Summary

 

These are all traditional tag development and are not commonly used in actual development. The following simple tag is the focus

 

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.