Javaweb: Label (ii)

Source: Internet
Author: User
Tags tld

Connect to the next section

Setjspcontext:

Must be called by the JSP engine, before Dotag, the PageContext that represents the JSP engine is passed to the label processor class.

Custom Labels with attributes:

1) First define setter methods in the tag processor class, it is recommended that all property types be set to String type.

For example:

Private String value; Private String count;  Public void SetValue (String value) {  this. Value = value;}  Public void SetCount (String count) {  this. Count = count;}

2), describe the attribute in the. TLD Profile:

For example:

<!--describes the properties of the current label -<attribute>      <!--property name, the same as the property defined by the setter method of the label processor class -      <name>Value</name>      <!--whether the property is required -      <Required>True</Required>      <!--Rtexprvalue:runtime Expression Value whether the current property can accept dynamic values for run-time expressions -      <Rtexprvalue>True</Rtexprvalue></attribute>

3), use the property in the page, the property name is the name defined in the. tld file.

Example: <Hnust:hello value= "${param.name}" count= "Ten" />

4), Usually, the development of simple tags directly inherit simpletagsupport , you can directly call its corresponding getter method to get the corresponding API.

1  Public classSimpletagsupportImplementssimpletag{2     3      Public voidDotag ()4         throwsjspexception, ioexception{}5     6     PrivateJsptag Parenttag;7     8      Public voidsetParent (jsptag parent) {9          This. Parenttag =parent;Ten     } One      A      PublicJsptag getParent () { -         return  This. Parenttag; -     } the      -     PrivateJspcontext Jspcontext; -      -      Public voidsetjspcontext (Jspcontext pc) { +          This. Jspcontext =pc; -     } +      A     protectedJspcontext Getjspcontext () { at         return  This. Jspcontext; -     } -      -     Privatejspfragment jspbody; -                  -      Public voidsetjspbody (jspfragment jspbody) { in          This. Jspbody =Jspbody; -     } to      +     protectedjspfragment getjspbody () { -         return  This. Jspbody; the     }    *}

Custom labels with label body:

1), if a label has a tag body: <hnust:testjspfragment>abcdefg</hnust: Testjspfragment>

Use the Jspfragment object in the label processor of the custom label to encapsulate the tag body information.

2), if the label is configured to contain the tag body, then the JSP engine calls the Setjspbody () method to pass the Jspfragment to the label processor class.

A Getjspbody () method is also defined in Simpletagsupport to return the Jspfragment object.

3), jspfragment Invoke (Wirter) method: The label body content from the Writer output, if NULL, is equivalent to invoke (Getjspcontext.getout ()), that is, the contents of the label directly to the page output.

Sometimes, with StringWriter, you can get the contents of the tag body first in the tag processor class:

①, using StringWriter to get the content of the label body.

StringWriter SW = new StringWriter (); Bodycontent.invoke (SW);

② the contents of the label body into uppercase

String content = sw.tostring (). toUpperCase ();

4), in the. tld file, use the Body-content node to describe the type of label body:

<body-content>: Specifies the type of label body, in most cases, the value is scriptless. There are 3 possible values: empty: No label body; Scriptless: The tag body can contain El expressions and JSP action elements, but not the scripting elements of the JSP; Tagdependent: Indicates that the label body is referred to the label itself to resolve the processing. If you specify Tagdependent, all the code in the tag body is left intact to the label processor instead of passing the execution result to the label processor.

such as:<body-content>tagdependent</body-content>

5), define a custom label:

1  Public classTestjspfragmentextendsSimpletagsupport {2     3 @Override4      Public voidDotag ()throwsjspexception, IOException {5Jspfragment bodycontent =getjspbody ();6         //Jspfragment.invoke (witer): Writer is the character stream of the content output of the tag body, if NULL, then7         //output to Getjspcontext (). Getout (), which is output to the page.8         9         //1. Use StringWriter to get the contents of the tag body.TenStringWriter SW =NewStringWriter (); One Bodycontent.invoke (SW); A          -         //2. Turn the contents of the label body into uppercase -String content =sw.tostring (). toUpperCase (); the          -         //3. Get the Out hidden object of the JSP page, output to the page - Getjspcontext (). Getout (). print (content); -     } +      -}

Tags with parent tags:

1), the parent tag cannot get a reference to a child tag, the parent tag only uses the tag as the label body.

2), sub-tags can obtain a reference to the parent tag through the GetParent () method (the method that inherits the Simpletagsupport or self-implementing Simpletag Interface): If the child tag does have a parent tag, the JSP engine passes the reference that represents the parent tag SetParent (jsptag Parent) is assigned to the label processor.

3), note: The type of the parent tag is Jsptag type, the interface is an empty interface, but to unify the simpletag and tag. The actual use requires a casting of type.

4), in the. TLD configuration file, there is no need for additional configuration for the parent tag, but the child tag is in the form of a label body, so the <body-content></body-content> of the parent tag should be set to scriptless.

Javaweb: Label (ii)

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.