Simple javaweb--Simple Label summary

Source: Internet
Author: User
Tags tag name tld

Custom Tags:What is a custom label?

A user-defined custom JSP tag. When a JSP page with a custom label is compiled into a servlet by the JSP engine, the tag tag is converted to aLabel Processing classThe operation of the object. As a result, when the JSP page is converted to a servlet by the JSP engine, the tag tag is actually converted to the operation of the tag processing class.What is the use of custom labels?

? Custom tags can reduce the complexity and maintenance of JSP development, and from the HTML point of view, you can make the HTML not too much attention to the more complex business logic (business logic). Using custom labels,can be a reasonable division between software developers and page designers: page designers can focus on using tags (html,xml or JSP) to create sites, while software developers can focus on implementing the underlying functionality, such as internationalization, to improve engineering productivity? The tag library with shared features is applied to different projects, which reflectssoftware re-useof thought.Development Steps for custom labels:

1. Write the Java Class (label processor) that completes the label function

2. Write a tag library profile (TLD file) in which to describe the label

3. Import and use the tag on the JSP page


1. Write the Java Class (tag processor) that completes the label function:

in fact, it is a Java class, just implement an interface or inherit a certain class. Can implement the Javax.servlet.jsp.tagext.SimpleTag interface or

Inherit the Simpletagsupport class.

To implement an interface, several methods are required to implement it: Dotag (), SetParent (jsptag parent), getParent (); Setjspcontext (Jspcontext pc); Setjspbody (Jspfragment jspbody). The most important of these is the Dotag () method. By looking at the method name, we need to write the code of the function that the tag needs to implement in the method. Setjspcontext (Jspcontext pc) is also important to pass PageContext, which represents the JSP engine, to the label processing class, which precedes the Dotag () method.

Private PageContext PageContext; @Overridepublic void Setjspcontext (Jspcontext pc) {//TODO auto-generated method stubthis.pagecontext= (PageContext) PC;}
With PageContext you can get the other 8 hidden objects of the page, such as Out,response,request and so on, we We can use these hidden objects to implement what we want to achieve. So the JSP page can do, tags can do.

If you choose to inherit the method of the Simpletagsupport class, we need to rewrite the Dotag () method. Where does the PageContext object go to get it? Open the API, see a getjspcontext() method, get the Jspcontext object, and then force the transformation to PageContext object.

For example, we need to simply output "Hello" in the browser interface now.

Dotag () Method:

public void Dotag () throws Jspexception, IOException {System.out.println ("Dotag"); HttpServletRequest request = (HttpServletRequest) pagecontext.getrequest ();p agecontext.getout (). println ("Hello" + Request.getparameter ("name"));}

2. Write a tag library description file (TLD file) in which to describe the tag:

<?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 of TLD Files--<description>mytag 1.0 Core LIBR ary</description> <display-name>mytag core</display-name> <tlib-version>1.0</ Tlib-version> <!--recommend that you use a label prefix--<short-name>mytag</short-name> <!--as the ID of a TLD file on a JSP page. To uniquely identify the current TLD file, the URI of multiple TLD files cannot be duplicated. By using the URI attribute of the taglib tag of the JSP page-<uri>http://www.mytag.com/mytag/core</uri> <!--describe the custom Hellosimplet Tag AG--<tag> <!--tag name--The name of the tag when using it on a JSP page--<name>hello</name> <!--tag's full class name--&L T;tag-class>tag. Hellosimpletag</tag-class> <!--label body type--<body-content>empty</body-content> <!--describe the current label The properties of--> <attribute> <!--property name--<name>value</name> <!--whether the property is mandatory--<required>false</ required> <!--runtime expression value whether the current property can accept dynamic values for run-time expressions--<rtexprvalue>false</rtexprvalue&gt  ; </attribute> <attribute> <!--property name--<name>count</name> <!--whether the property is mandatory---< required>false</required> <!--runtime expression value whether the current property can accept dynamic values for run-time expressions--&LT;RTEXPRVALUE&GT;FA lse</rtexprvalue> </attribute> </tag> </taglib>

Where name and count are the properties of the custom label.


3. Import and use the tag on the JSP page:

<myTag:hello/>
The label names in the Hello and TLD files are consistent. There can be more than one label and signature in a TLD file. So you should also pay attention to the point when you import it.

With the above steps, a simple custom label development is completed.

So what do the properties in the custom tag do? Maybe we need to pass in a few parameters in our TLD file, then we need the attributes. To use attributes, you first need to declare these properties and the corresponding set () methods in the Java class.

private string Value;private string count;public void SetValue (String valuestring) {this.value = valuestring;} public void SetCount (String count) {This.count = count;}
At the same time, these attributes need to be described in the TLD file. Finally, when we import the use, write these parameter names and values can be
<mytag:value= "MyTag" count= "ten"/>

Dotag () Method:
public void Dotag () throws Jspexception, IOException {System.out.println ("Dotag"); HttpServletRequest request = (HttpServletRequest) pagecontext.getrequest (); System.out.println ("Value:" +value+ "Count:" +count);p agecontext.getout (). println ("Hello" +request.getparameter (" Name "));



Simple javaweb--Simple Label summary

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.