09.javaweb Simple Label Programming

Source: Internet
Author: User
Tags closing tag control label tld

First, simple label

1, Introduction: Because the traditional label using three label interface to complete different functions, appear too cumbersome, not conducive to the promotion of label technology, sun Company to reduce the learning difficulty of the label technology, in the JSP 2.0 Defines a simpler, easier to write and invoke simpletag interface to implement the functionality of the label.

2, Implement Simpletag the label of the interface is called a simple label. There are five ways to tag a simple label

Setjspcontext method: used to pass the PageContext object of the JSP page to the label processor object

SetParent method: Used to pass the parent tag processor object to the current label processor object

GetParent method: The parent Tag Processor object used to obtain the current label

Setjspbody method: Used to pass the Jspfragment object representing the label body to the label processor object

Dotag Method:

Used to complete all of the label logic, including output, iteration, modify tag body contents, etc. In the Dotag method, you can throw The javax.servlet.jsp.SkipPageException exception, which informs the Web container that no longer executes the contents of the JSP page after the closing tag, is equivalent to the case where the Tag.skip_page constant is returned in the Doendtag method of the traditional label.

3, Web steps for a container to perform a simple label

The A.web container invokes the Setjspcontext method of the label processor object, passing the PageContext object representing the JSP page to the label processor object.

The B.web container invokes the SetParent method of the label processor object, passing the parent tag processor object to the label processor object. Note that the Web container calls this method only if the label has a parent tag.

C. If a property is set when the tag is called, the container invokes the setter method corresponding to each property to pass the property value to the label processor object. If the property value of a label is an El expression or a script expression, the Web container evaluates the expression first and then passes the value to the label processor object.

D. If a simple tag has a tag body, the Web container calls the Setjspbody method to pass in the Jspfragment object that represents the label body.

E. The Dotag () method of the label processor is invoked by the Web container when the tag is executed, and the developer can implement, iterate, and modify the label body by manipulating the Jspfragment object in the method body.

4, Simple Tag Example: Sun Company for Simpletag interface provides a default implementation class Simpletagsupport , inheriting this class can complete the simple label development

4.1 writing label Processor classes

     Packagecom.chen.ying;Importjava.io.IOException;Importjavax.servlet.jsp.JspException;Importjavax.servlet.jsp.tagext.JspFragment;ImportJavax.servlet.jsp.tagext.SimpleTagSupport; Public classSimpleTagDemo01extendsSimpletagsupport { Public voidDotag ()throwsjspexception,ioexception{//the jspfragment of the label body is obtained to operate the label body;jspfragment JFM= This. Getjspbody (); Jfm.invoke (NULL);//default label body content output to browser    }}

4.2 Configuring labels in the tag library

4.3 in the JSP used in

5, Control label body Content repeat execution

5.1 writing label processing classes

 Packagecom.chen.ying;Importjava.io.IOException;Importjavax.servlet.jsp.JspException;Importjavax.servlet.jsp.tagext.JspFragment;ImportJavax.servlet.jsp.tagext.SimpleTagSupport; Public classSimpleTagDemo02extendsSimpletagsupport { Public voidDotag ()throwsjspexception,ioexception{//the jspfragment of the label body is obtained to operate the label body;jspfragment JFM= This. Getjspbody ();  for(inti=1;i<=3;i++) {Jfm.invoke (NULL);//default label body content is repeatedly output to the browser        }    }}

6 , modify JSP content and Output

6.1 Write tag processor class to convert tag body contents to uppercase

  Public classSimpleTagDemo03extendsSimpletagsupport { Public voidDotag ()throwsjspexception,ioexception{//the jspfragment of the label body is obtained to operate the label body;jspfragment JFM= This. Getjspbody (); StringWriter SW=NewStringWriter ();//String StreamJfm.invoke (SW);//output the label body to a string streamString Content=sw.getbuffer (). toString ();//get content in streamcontent=content.touppercase ();//Convert to uppercasePageContext PageContext= (PageContext) This. Getjspcontext (); //output the modified content to the browserpagecontext.getout (). write (content); }}

7 , control the entire JSP whether the page executes

write a class inheritance Simpletagsupport , and then rewrite Dotag method, in Dotag method Throws skippageexception exception, JSP When this exception is received, the remaining label will be ignored JSP the execution of the page.

 Public void throws jspexception, IOException {         // throws a Skippageexception exception to control the JSP after the tag does not execute          ThrowNew  skippageexception ();     }

8,jspfragment class

The instance object of the Javax.servlet.jsp.tagext.JspFragment class represents a fragment in the JSP that does not include a JSP script element. WEB when the container is handling the label body of a simple label, the contents of the label body are jspfragment object that is called by the label processor object. Setjspbody method to jspfragment object is passed to the label processor object. There are two methods of jspfragment

Getjspcontext (), returns the Jspcontext () object representing the calling page

public abstract void Invoke (Java.io.Writer out)

Used to execute the JSP code fragment represented by the Jspfragment object, which specifies the output stream object to which the execution result of the Jspfragment object is written, if the value passed to the parameter out is NULL, The execution result is written to the output stream object returned by the Jspcontext.getout () method. (In short, it can be understood as writing to a browser)

Invoke Detailed Method

The Jspfragment.invoke method is the most important method of jspfragment, which can be used to control whether the content of the tag body is executed and output, whether the content of the tag body is iterated, or the result of the label body execution is modified before output. For example:
If the Jspfragment.invoke method is not called in the label processor, the result is equivalent to ignoring the contents of the tag body;
Repeated calls to the Jspfragment.invoke method in the label processor, the label body content will be repeated execution;
To modify the contents of a tag in a label processor, simply specify an output stream object (such as StringWriter) that extracts the result data when the Invoke method is called, so that the execution result of the tag body is output to the output stream object. Then, from the output stream object to remove the data to modify and then output to the target device, you can modify the label body purpose.

9, simple label development with attributes

The property setting for a simple label is similar to the property setting for a traditional label. It is important to note that if the attribute value of the tag is 8 Basic data types, the JSP page The engine is automatically converted to the appropriate type, but if the label's attribute value is a composite data type, then the JSP engine cannot be automatically converted

Here's how to assign values to property values for a composite data type:

The above assigns a value to a property of a date type, which can be assigned to an attribute using an expression language or an expression.

10,dynamicattribute interface: Using this interface, you can receive dynamic properties

This interface has only one method to receive dynamic properties, which is called when the label contains dynamic properties that are not configured in the tag library.

Public void Setdynamicattribute (string uri, String localname, Object value) throws jspexception {

}

10.1 Write a label processing class that can receive dynamically filled properties and output the attribute addition value to the browser

 Public classSimpleTagDemo04extendsSimpletagsupportImplementsdynamicattributes{PrivateMap<string,float> map=NewHashmap<string,float>();  Public voidDotag ()throwsjspexception,ioexception{Float sum=0.0f;  for(Map.entry<string, float>M:map.entryset ()) {Sum+=m.getvalue ();//Add the attribute values from the map        }        Super. Getjspcontext (). Getout (). Write (sum+ "");//Output to Browser    }     Public voidSetdynamicattribute (String uri, String localname, Object value)throwsjspexception {//Loop executes this function to receive dynamic propertiesMap.put (Localname,float.parsefloat (value.tostring ())); }}

10.2 Configure this label in the tag library

10.3 Use this tag in JSP

Results

Second, the label development details

1 , when developing a simple label class, do not go directly to implement Simpletag interface, but should inherit Simpletagsupport class, Simpletagsupport class is a default implementation class of Simpletag interface, By inheriting the Simpletagsupport class, you can directly use those methods that the Simpletagsupport class has implemented, and if the method implementation of the Simpletagsupport class does not meet the business requirements, Then the corresponding method can be rewritten according to the specific business situation.

2 ,There are four types of label bodies (Body-content) in the TLD file:empty,JSP,scriptless, tagdependent

in the simple label (Sampletag) in the label body body-content The value is only allowed to be Empty , scriptless , tagdependent , not allowed to be set to JSP, If set as JSP There will be an exception : The JSP tag technology is designed to remove Java code written on the JSP page, if Java code is allowed in the JSP tag, then it violates the original intention of JSP tag technology design. Therefore, Java code is not allowed in the tag body of a simple tag.

If the value of the label body Body-content is set to tagdepentend, then it means that the contents of the tag body are used for the label processor class.

For example: Develop a SQL tag that queries the user, and the SQL statement in the tag body is used for the label processor of the SQL label.

<gacl:sql>select * from user</gacl:sql>

3, if there are two tag libraries with the same URI, you cannot simply refer to the tag library via a URI, which can be distinguished by uri= the tag library TLD directory

<% @taglib uri= "/web-inf/gacl.tld" prefix= "GaCl"%>,

<% @taglib uri= "/web-inf/simpletag.tld" prefix= "GaCl"%>

Third, Summary

1, write a class to inherit the Simpletagsupport class, and then according to the business needs to rewrite the Simpletagsupport class has implemented methods, generally only need to rewrite the Dotag () method.

2. Create a TLD file in the Web-inf directory and add a description of the tag to the TLD file. TLD files may not be placed in the Web-inf directory or in other directories, and are used in the Web-inf directory.

09.javaweb Simple Label Programming

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.