JSP custom Tags Getting Started instance detailed

Source: Internet
Author: User

JSP custom tags can be used in the main two packages

javax.servlet.jsp.*;javax.servlet.jsp.tagext.*;

Customize the label <userInfo:showUserInfo/> realize the user's presentation.

As follows


Define user-tired User.java get set is not in the repeating.

Private String UserName;
Private Integer age;
Private String Email;

Public User () {
This.username= "Zhang San";
this.email= "[email protected]";
this.age=28;
}


Create a custom Label class Userinfotag.java

Package Com.test.tag;


Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import Javax.servlet.jsp.tagext.TagSupport;


Import Com.test.entity.User;


public class Userinfotag extends TagSupport {

Private user User=new user ();

@Override
public int doStartTag () throws Jspexception {
TODO auto-generated Method Stub

JspWriter out = This.pageContext.getOut ();
try {
if (user = = null) {
Out.print ("UserInfo is not Found ...");
return skip_body;
}
Out.println ("<table width= ' 500px ' border= ' 1 ' align= ' center ' >");
Out.println ("<tr>");
Out.println ("<td width= ' 20% ' >Username:</td>");
Out.println ("<td>" + user.getusername () + "</td>");
Out.println ("</tr>");
Out.println ("<tr>");
Out.println ("<td>Age:</td>");
Out.println ("<td>" + user.getage () + "</td>");
Out.println ("</tr>");
Out.println ("<tr>");
Out.println ("<td>Email:</td>");
Out.println ("<td>" + user.getemail () + "</td>");
Out.println ("</tr>");
Out.println ("</table>");
} catch (Exception e) {
throw new Jspexception (E.getmessage ());
}
return Super.dostarttag ();
}

@Override
public int Doendtag () throws Jspexception {
TODO auto-generated Method Stub
return eval_page;
}

@Override
public void release () {
TODO auto-generated Method Stub
Super.release ();
This.user=null;
}

Public User GetUser () {
return user;
}

public void SetUser (user user) {
This.user = user;
}

}


2 Create a tag library description file in Web-inf. TDL (Tag libraries Description)

<?xml version= "1.0" encoding= "UTF-8"?>
<taglib version= "2.0" 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 web-jsptaglibrary_2_0.xsd" >
<tlib-version>1.0</tlib-version>
<jsp-version>3.0</jsp-version>
<short-name>userInfo</short-name>
<uri>mytaglib</uri>
<tag>
<name>showUserInfo</name>
<tag-class>com.test.tag.UserInfoTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>user</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

3 Configuring Web. Xml

<jsp-config>

<taglib>

<taglib-uri>mytaglib</taglib-uri>

<taglib-location>/WEB-INF/mytaglib.tld</taglib-location>

</taglib>

</jsp-config>

4 the page header that needs to use the label is introduced

<%@ taglib uri= "/mytaglib" prefix= "CC"%>

<userInfo:showUserInfo/>

Label description


The Userinfotag class we created inherits the TagSupport class, and it implements the tag interface, and the tag interface's life cycle is controlled by the container in which it resides, as follows:

Setpagecontext () injects the PageContext of the JSP page in order to have access to the PageContext property of the JSP Page object in a later method

SetParent () sets the parent label for this label

SetAttribute () injects attributes from the tag into this class's properties, without having to implement it yourself but to provide the property's get and set methods

doStartTag () is called after the start Tag property is set, and if return Skip_body ignores the contents of the label, the contents of the label body are output if Eval_body_include is returned

Doendtag () is called before the end tag, and returns Skip_page skips the output after the entire JSP page, returning Eval_page to perform the remainder of the page

Release () called at the end of the life cycle

At this point, a simple custom label is implemented.

SOURCE http://download.csdn.net/detail/qiyejunlintian/8037433

JSP custom Tags Getting Started instance detailed

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.