Development and application of java_jsp custom label

Source: Internet
Author: User
Tags tag name tld

The JSTL provides four tag libraries (core tag libraries, internationalized tag libraries, database tag libraries, and XML tag libraries), which involve dozens of tags. Although these tags can do more complex work, they still do not meet the special needs of the program. Therefore, users need to customize the JSP tag according to their own needs, this user-implemented JSP tag is called a custom label.

.1 Custom Label Basics

The labels in the custom labels and jstl are technically indistinguishable, and can be collectively referred to as JSP tags. JSP tags are called in the JSP page in XML syntax format, and when the JSP engine translates the JSP page into a servlet, it translates the calls into executing the appropriate Java code. In other words, the JSP tag actually calls some Java code, just in a different form (XML syntax format) in the JSP page.

1.1 Writing the label of the output random number

Before starting the custom label learning, in this section first implement a simple custom label to allow the reader to warm up, while the reader through this section of the example can be a custom label implementation methods and procedures have a perceptual understanding.

"Instance 1-1" implements the label of the output random number

1. Example Description

The function of the custom label (random label) implemented in this example is to output a random integer of a specified range. The random tag has the following characteristics:

No label body.

There are two properties: Min and Max. Where the Min property represents the smallest value that generates a random number, the Max property represents the maximum value that generates a random number. The default value of the Min property is the default value of the 0,max property is Integer.max_value.

The range of generated random numbers is min <= random < max.

The standard invocation form of the random tag is as follows:

<ct:random min= "1" max= "/>"

where "CT" is the prefix when the tag is called, specified by taglib the specified prefix property. The function of the above code is to output a random number between 1 (inclusive) and 100 (not included).

2. Writing label classes

The Label class is the core part of the custom label. There are many ways to implement a label class, but the simplest way is to write a

The Javax.servlet.jsp.tagext.TagSupport class inherits the Java class and overrides the doStartTag method of the TagSupport class in the class. In order to read the property values in a tag, you also need to provide a Label class property for each label property in the Label class and a setter method for that property (no Getter method is required). The code that generates the random number needs to be placed in the doStartTag method of the label class. The implementation code for this tag class is as follows:

Java code
  1. Package chapter1;
  2. Import java.io.IOException;
  3. Import Java.util.Random;
  4. Import javax.servlet.jsp.JspException;
  5. Import Javax.servlet.jsp.tagext.TagSupport;
  6. Public class Randomtag extends TagSupport
  7. {
  8. Encapsulates the JavaBean property of the two properties of the random tag
  9. Private int min = 0;
  10. Private int max = Integer.max_value;
  11. Setter Method for Min property
  12. Public void setmin (int min)
  13. {
  14. this.min = min;
  15. }
  16. Setter method for Max Property
  17. Public void Setmax (int max)
  18. {
  19. This.max = max;
  20. }
  21. doStartTag method for overriding TagSupport class
  22. Called when the start tag of a label (that is, <ct:random>) is encountered
  23. @Override
  24. public int doStartTag () throws Jspexception
  25. {
  26. Try
  27. {
  28. Random random = new Random ();
  29. Generate a random number between min and Max
  30. int result = min + random.nextint (max-min);
  31. Outputs the generated random number to the client
  32. Pagecontext.getout (). Write (string.valueof (result));
  33. }
  34. catch (IOException E)
  35. {
  36. }
  37. The doStartTag method of the TagSupport class returns skip_body by default, indicating that the custom label body is ignored
  38. return Super.dostarttag ();
  39. }
  40. }

A PageContext variable is used in the doStartTag method of the Randomtag class to obtain the JspWriter object (the JSP's out

Built-in objects). The PageContext variable is a class variable defined in the TagSupport class that passes through the TagSupport class

The Setpagecontext method assigns the value. In fact, the Setpagecontext method is defined in the tag interface, and TagSupport implements the

The Setpagecontext method of the tag interface. The servlet container first invokes the Setpagecontext method of the tag interface to initialize the PageContext variable before calling the doStartTag method. The tag interface will be described in section 1.1.3, where it is necessary to implement the tag interface as long as all the tag classes are known. To simplify the implementation of the Label class, the JSP API provides a TagSupport class, with the TagSupport class, where the user does not need to implement all the methods of the tag interface when writing the tag class.

3. Writing tag library descriptor files (TLD files)

There are several TLD files in the Jstl (see Figure 9.1), where the TLD is the abbreviation for the tag Library descriptor (Tag Gallery descriptor).

For a custom label to work correctly, you must configure the label in the TLD file. In the TLD file, the following two parts are mainly configured: the configuration information of the tag library, the configuration information of the specific tag in the tag library

The configuration information of the tag library mainly includes the following content:

version of the tag library: Use the <tlib-version> tag settings.

The minimum JSP version required for normal use of tags in the tag library: Use the <jsp-version> tag settings.

Default prefix for tag libraries: Use <short-name> tag settings.

URI for Tag library: Use <uri> tag settings.

Description of the tag library: Use the <description> tag settings.

The configuration information of the label mainly includes the following content:

Tag name: Use the <name> tag settings.

Java class for tags: use <tag-class> tag settings.

Type of label body: Use <body-content> label settings.

Description of the label: use the <description> tag settings.

Tag Property Information: Each Label property corresponds to a <attribute> tag. You can specify the label name (using the <name> tag setting) in the <attribute> tab, whether the property must be specified (using the <required> tag setting), whether the property supports dynamic property values (using the < Rtexprvalue>, and other configuration information such as label designations.

Create a Jsp-taglib.tld file in the Web-inf directory and enter the following in the file:

Java code
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <taglib xmlns="HTTP://JAVA.SUN.COM/XML/NS/J2EE"
  3. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  4. Xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "version=" 2.0 ">
  6. <!--Tag library configuration information--
  7. <description> Custom Tag Libraries </description>
  8. <tlib-version>1.1</tlib-version>
  9. <short-name>ct</short-name>
  10. <uri>http://nokiaguy.blogjava.net</uri><taglib>
  11. <!--configuration information for the random tag--
  12. <tag>
  13. <description> generates a specified range of random numbers </description>
  14. <name>random</name>
  15. <tag-Class>chapter1. randomtag</tag-class>
  16. <body-content>empty</body-content>
  17. <attribute>
  18. <name>min</name>
  19. <required>false</required>
  20. <rtexprvalue>false</rtexprvalue>
  21. </attribute>
  22. <attribute>
  23. <name>max</name>

Development and application of java_jsp custom label

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.