Custom tag Library development with El expression

Source: Internet
Author: User
Tags return tag tld

Custom tag Library development with El expression

Custom tag Library development with El expression

1. Development of Custom tag libraries
Custom tag libraries are primarily used to remove Java code from JSP pages.

Step One:
Write a class that implements the tag interface (it is recommended to inherit TagSupport) and keep the 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 Two:
Write the TLD file under the Web-inf file and describe the label processor class as a label 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 Three: Use
<%@ taglib uri= "/soldier" prefix= "Soldier"%>

<soldier:viewip/>


How the custom label is implemented:

IE Web server 1.jsp viewtag

When the browser requests a JSP page from the server and the server interprets the custom label,
1. First instantiate the corresponding label processor class.
2. Call the Setpagecontext method to pass the PageContext object of the page to the label processor class
3. Determine if the label has a parent tag, and if so, pass the parent tag as an object, passed to the label processor class through the SetParent () method. Null not passed
4. Complete the above label processor class initialization. The server starts executing the label, and then it encounters the label's start tag and calls the Dostartag method.
5. If the label has a label body, the server will generally execute the label body
6. The server encounters the end tag of the label, then executes the Doendtag method.
7. After the entire label is executed, the server generally calls the release () method. Frees the resources that the label consumes when it works.
8. The server then executes the contents after the tag.

Tag tag's 5 function v1.0:
1. Porting 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 is executed
public class Tagdemo1 extends TagSupport {
@Override
public int doStartTag () throws Jspexception {
Use to control whether the contents of the label are executed
Return tag.eval_body_include;//the contents of the tag body will be executed when the result is returned.
Return tag.skip_body//Returns the result to this, the tag body content is not executed.
}
}
3. Control whether the entire JSP page executes
public class Tagdemo2 extends TagSupport {
@Override
public int Doendtag () throws Jspexception {
Return tag.skip_page;//tag body after the content is not executed
Return tag.eval_page;//tag body behind content keyi 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; Tag body content continues to execute
} else {
Return iterationtag.skip_body;//tag body content skipped, end execution
}
}
}
5. Modify JSP page content output
public class Tagdemo4 extends Bodytagsupport {
@Override
public int doStartTag () throws Jspexception {
Return bodytag.eval_body_buffered;//wraps the label body to the object, which can be removed by the Getbodycontent () method
}
@Override
public int Doendtag () throws Jspexception {
bodycontent BC = this.getbodycontent ();//Get tag body content
String content = bc.tostring ();
Content =content.touppercase ();//Become uppercase
try {
This.pageContext.getOut (). write (content);
} catch (IOException e) {
E.printstacktrace ();
}
Return tag.eval_page;//tag body after content can be executed
}
}


Tag tag's 5 function v2.0:
1. Porting Java code

2. Control whether part of JSP page 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 tag body//invoke call
If you do not let the output, do not execute the above code on the line.
}
}
3. Control whether the entire JSP page executes
public class Tagdemo4 extends Simpletagsupport {
@Override
public void Dotag () throws Jspexception, IOException {
throw new Skippageexception ();//The remaining JSP is no longer executed after throwing an exception
}
}
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 tag body//invoke call
}
}
}
5. Modify JSP page content output
public class Tagdemo3 extends Simpletagsupport {
@Override
public void Dotag () throws Jspexception, IOException {
Jspfragment JF = This.getjspbody (); Fragment Fragment
Here, invoke accepts a writer, but the goal is to get the contents of the tag body, so we should find a writer with buffer, and the writer can return the buffer contents.
StringWriter SW = new StringWriter ();
Jf.invoke (SW);
//
String conter = sw.tostring ();//The contents of the tag body are obtained here.
This.getjspcontext (). Getout (). Write (Conter.touppercase ());//Modify Label body contents
}
}


The principle of simple label implementation:

IE Web server 1.jsp viewtag

When the browser requests a JSP page from the server and the server interprets the custom label,
1. First instantiate the corresponding label processor class.
2. Call the Setjspcontext method to pass the PageContext object of the page to the label processor class
3. Call the SetParent () method to pass the parent tag to the label processor class
4. Call the Setjspbody method to pass the encapsulated Jspfragment object to the label processor class.
5. Complete the above label processor class initialization. The server starts executing the Doendtag method.
6. After the label is executed, release the resources that the label is working on.
7. The server then executes the contents after the tag.

Tag with properties
public class Tagdemo5 extends Simpletagsupport {
private int count;
Tag with properties
@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 is configured 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 property must be identified
<rtexprvalue>true</rtexprvalue>//whether an expression is allowed
</attribute>
</tag>

Anti-theft chain label
public class Tagdemo6 extends Simpletagsupport {
Private String site;
Private String page;

Tag with properties
@Override
public void Dotag () throws Jspexception, IOException {
PageContext 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 label
Private Boolean test;
@Override
public void Dotag () throws Jspexception, IOException {
if (test)
This.getjspbody (). Invoke (null);//execute because there is no label body so there is no need to output the label body
}

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;

tag with attributes, omit setter 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 Labels

HTML forwarding Tags


How to package a custom label for development


Scopes in El

Scope Correspondence Relationship in El
*pagecontext the PageContext object of the current page
*pagescope map the data in the page scope to a Map object
*requestscope map data from the request scope to a Map object
*sessionscope map data from session scope to a Map object
*applicationscope map the data in the application scope to a Map object
*param corresponding to Request.getparameter ()
Paramvalues corresponding to Request.getparametervalues ()
Header corresponds to Request.getheader ()
Headervalues corresponding to Request.getheadervalues ()
Cookie corresponds to Request.getcookies ()
Initparam corresponding to Servletcontext.getinitparamter ()

Custom tag Library development with El expression

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.