Custom tag Library Development and El expressions

Source: Internet
Author: User

Custom tag Library Development and El expressions

 

1. Development of custom tag Library
The custom tag library is mainly used to remove Java code from the JSP page.

Step 1:
Compile a class that implements the tag interface (it is recommended to inherit tagsupport) and keep Java code in this class.


Package CN. Soldier. Tag;

Import java. Io. ioexception;

Import javax. servlet. jsp. jspexception;
Import javax. servlet. jsp. tagext. tagsupport;

Public class viewip extends tagsupport {

Private Static final long serialversionuid = 1l;

@ Override
Public int dostarttag () throws jspexception {
String _ IP = pagecontext. getrequest (). getlocaladdr ();
Try {
Pagecontext. getout (). Write (_ IP );
} Catch (ioexception e ){
Throw new runtimeexception ();
}
Return super. dostarttag ();
}

}


Step 2:
Write a TLD file in the Web-inf file, and describe the tag processor class into a tag in the TLD file.


<? XML version = "1.0" encoding = "ISO-8859-1"?>

<! Doctype taglib
Public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.2 // en"
Http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd>

<Taglib>
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 1.2 </JSP-version>
<Short-Name> soldier </short-Name>
<URI>/soldier </uri>
<Tag>
<Name> viewip </Name>
<Tag-class> CN. Soldier. Tag. viewip </Tag-class>
<Body-content> Empty </body-content>
</Tag>
</Taglib>

Step 3: Use
<% @ Taglib uri = "/soldier" prefix = "Soldier" %>

<SOLDIER: viewip/>


Implementation principle of custom tags:

IE Web Server 1.jsp viewtag

When the browser requests the JSP page from the server and the server interprets the custom tag,
1. First instantiate the corresponding Tag Processor class.
2. Call the setpagecontext method to pass the pagecontext object of the page to the tag processor class.
3. Determine whether a tag has a parent tag. If yes, use the parent tag as an object and pass it to the tag processor class through the setparent () method. Null is not passed
4. The above Tag Processor class has been initialized. The server starts to execute the tag. In this case, the start tag of the tag is met, and the dostartag method is called.
5. If the tag has a TAG body, the server generally executes the TAG body
6. If the server encounters an end tag, the doendtag method is executed.
7. After the entire tag is executed, the server generally calls the release () method. Resources used to release tags.
8. The server then executes the content following the tag.

5 Functions of tag labels V1.0:
1. Port Java code
@ Override
Public int dostarttag () throws jspexception {
String _ IP = pagecontext. getrequest (). getlocaladdr ();
Try {
Pagecontext. getout (). Write (_ IP );
} Catch (ioexception e ){
Throw new runtimeexception ();
}
Return super. dostarttag ();
}
2. control whether part of JSP page content is executed
Public class tagdemo1 extends tagsupport {
@ Override
Public int dostarttag () throws jspexception {
// Usage to control whether the label body content is executed
Return tag. eval_body_include; // when the returned result is this, the content of the label body is executed.
// Return tag. skip_body // when the returned result is returned to this object, the content of the TAG body is not executed.
}
}
3. control whether the entire JSP page is executed
Public class tagdemo2 extends tagsupport {
@ Override
Public int doendtag () throws jspexception {
// Return tag. skip_page; // The content behind the TAG body is not executed.
Return tag. eval_page; // The keyi behind the TAG body is executed.
}
@ Override
Public int dostarttag () throws jspexception {
Return tag. eval_body_include;
}
}
4. Control repeated execution of JSP page content
Public class tagdemo3 extends tagsupport {
Int x = 3;
@ Override
Public int dostarttag () throws jspexception {
Return tag. eval_body_include;
}
@ Override
Public int doafterbody () throws jspexception {
X --;
If (x> 0 ){
Return iterationtag. eval_body_again; // the content of the label body continues to be executed.
} Else {
Return iterationtag. skip_body; // the content of the label body is skipped, that is, the execution is terminated.
}
}
}
5. Modify JSP page content output
Public class tagdemo4 extends bodytagsupport {
@ Override
Public int dostarttag () throws jspexception {
Return bodytag. eval_body_buffered; // encapsulate the label body into an object, which can be removed using the getbodycontent () method.
}
@ Override
Public int doendtag () throws jspexception {
Bodycontent BC = This. getbodycontent (); // obtain the TAG body content.
String content = BC. tostring ();
Content = content. touppercase (); // converts it to uppercase.
Try {
This. pagecontext. getout (). Write (content );
} Catch (ioexception e ){
E. printstacktrace ();
}
Return tag. eval_page; // The content behind the TAG body can be executed.
}
}


Five functions of tag tags:
1. Port Java code

2. control whether part of JSP page content is executed
Public class tagdemo1 extends simpletagsupport {
@ Override
Public void dotag () throws jspexception, ioexception {
Jspfragment JF = This. getjspbody (); // fragment Fragment
JF. Invoke (this. getjspcontext (). getout (); // execute the TAG body // call invoke
// If the output is not allowed, the above Code will not be executed.
}
}
3. control whether the entire JSP page is executed
Public class tagdemo4 extends simpletagsupport {
@ Override
Public void dotag () throws jspexception, ioexception {
Throw new skippageexception (); // The remaining JSP will not be executed after an exception is thrown.
}
}
4. Control repeated execution of JSP page content
Public class tagdemo2 extends simpletagsupport {
@ Override
Public void dotag () throws jspexception, ioexception {
Jspfragment JF = This. getjspbody (); // fragment Fragment
For (INT I = 0; I <3; I ++ ){
JF. Invoke (this. getjspcontext (). getout (); // execute the TAG body // call invoke
}
}
}
5. Modify JSP page content output
Public class tagdemo3 extends simpletagsupport {
@ Override
Public void dotag () throws jspexception, ioexception {
Jspfragment JF = This. getjspbody (); // fragment Fragment
// Invoke accepts a writer, but the purpose is to obtain the content of the label body. Therefore, you should find a writer with a buffer and the writer can return the buffer content.
Stringwriter Sw = new stringwriter ();
JF. Invoke (SW );
//
String Conter = Sw. tostring (); // The TAG body content is obtained here.
This. getjspcontext (). getout (). Write (Conter. touppercase (); // modify the label body content
}
}


Implementation principle of simple labels:

IE Web Server 1.jsp viewtag

When the browser requests the JSP page from the server and the server interprets the custom tag,
1. First instantiate the corresponding Tag Processor class.
2. Call the setjspcontext method to pass the pagecontext object of the page to the tag processor class.
3. Call the setparent () method to pass the parent tag to the tag processor class.
4. Call the setjspbody method to pass the encapsulated jspfragment object to the label processor class.
5. The above Tag Processor class has been initialized. The server starts executing the doendtag method.
6. resources used to release tags after they are executed.
7. The server then executes the content following the tag.

Tag attributes
Public class tagdemo5 extends simpletagsupport {
Private int count;
// Tag attributes
@ Override
Public void dotag () throws jspexception, ioexception {
Jspfragment JF = This. getjspbody ();
For (INT I = 0; I <count; I ++ ){
JF. Invoke (this. getjspcontext (). getout ());
}
}
Public void setcount (INT count ){
This. Count = count;
}
}
The TLD configuration is as follows:
<Tag>
<Name> demo5 </Name>
<Tag-class> CN. Soldier. simpletag. tagdemo5 </Tag-class>
<Body-content> scriptless </body-content>
<Attribute>
<Name> count </Name>
<Required> true </required> // whether this attribute must be identified
<Rtexprvalue> true </rtexprvalue> // whether the expression is allowed
</Attribute>
</Tag>

Anti-leech label
Public class tagdemo6 extends simpletagsupport {
Private string site;
Private string page;

// Tag attributes
@ Override
Public void dotag () throws jspexception, ioexception {
Pagecontext = (pagecontext) This. getjspcontext ();
Httpservletrequest request = (httpservletrequest) pagecontext. getrequest ();
Httpservletresponse response = (httpservletresponse) pagecontext. getresponse ();
String Referer = request. getheader ("Referer ");

If (Referer = NULL |! Referer. startswith (SITE )){

If (page. startswith (request. getcontextpath ())){
Response. sendredirect (PAGE );
} Else if (page. startswith ("/")){
Response. sendredirect (request. getcontextpath () + page );
} Else {
System. Out. println (request. getcontextpath () + "/" + page );
Response. sendredirect (request. getcontextpath () + "/" + page );
}
Throw new skippageexception ();
}

}

Public void setsite (string site ){
This. Site = site;
}

Public void setpage (string page ){
This. Page = page;
}

}

<Tag>
<Name> demo6 </Name>
<Tag-class> CN. Soldier. simpletag. tagdemo6 </Tag-class>
<Body-content> Empty </body-content>
<Attribute>
<Name> site </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
<Attribute>
<Name> page </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>

<Simpletagsoldier: demo6 page = "message. jsp" Site = "http: // localhost: 8080/"/>

If tag
Private Boolean test;
@ Override
Public void dotag () throws jspexception, ioexception {
If (test)
This. getjspbody (). Invoke (null); // execute. Because the label body is not included, no output label body is unnecessary.
}

Public void settest (Boolean test ){
This. test = test;
}
<Tag>
<Name> demo7 </Name>
<Tag-class> CN. Soldier. simpletag. tagdemo7 </Tag-class>
<Body-content> Empty </body-content>
<Attribute>
<Name> test </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>

 

If-else label


Public class choosetag extends simpletagsupport {
Private Boolean isdo;

// The tag has attributes. setter is omitted here.
@ Override
Public void dotag () throws jspexception, ioexception {
This. getjspbody (). Invoke (null );
}

Public Boolean isdo (){
Return isdo;
}

Public void setdo (Boolean isdo ){
This. isdo = isdo;
}
}

Public class whentag extends simpletagsupport {
Private Boolean test;

@ Override
Public void dotag () throws jspexception, ioexception {
Choosetag parent = (choosetag) This. getparent ();
If (Test &&! Parent. isdo ()){
This. getjspbody (). Invoke (null );
Parent. setdo (true );
}
}

Public void settest (Boolean test ){
This. test = test;
}
}

Public class otherwisetag extends simpletagsupport {
@ Override
Public void dotag () throws jspexception, ioexception {
Choosetag parent = (choosetag) This. getparent ();
If (! Parent. isdo ()){
This. getjspbody (). Invoke (null );
Parent. setdo (true );
}
}
}

<Tag>
<Name> choose </Name>
<Tag-class> CN. Soldier. simpletag. example. choosetag </Tag-class>
<Body-content> scriptless </body-content>
</Tag>
<Tag>
<Name> when </Name>
<Tag-class> CN. Soldier. simpletag. example. whentag </Tag-class>
<Body-content> scriptless </body-content>
<Attribute>
<Name> test </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>
<Tag>
<Name> otherwise </Name>
<Tag-class> CN. Soldier. simpletag. example. otherwisetag </Tag-class>
<Body-content> scriptless </body-content>
</Tag>

<Sold: Choose>
<Sold: When test = "true"> AAA </sold: When>
<Sold: otherwise> BBB </sold: otherwise>
</Sold: Choose>


Iteration tag

HTML forwarding tags


How to package and develop custom labels


Scope in El

Scope correspondence in El
* Pagecontext object of the current page
* Pagination maps data in the page scope to a map object.
* Requestscope maps data in the request scope to a map object.
* Sessionscope maps data in the session scope to a map object.
* Applicationscope maps data in the application scope to a map object.
* Param corresponds to request. getparameter ()
Request. getparametervalues () for paramvalues ()
Request. getheader () corresponding to the header ()
Headervalues corresponds to request. getheadervalues ()
Cookie corresponds to request. getcookies ()
Initparam corresponds to servletcontext. getinitparamter ()

Custom tag Library Development and El expressions

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.