Display the data list in the jsp custom tag Library

Source: Internet
Author: User

Article Title: display the data list in the jsp custom tag Library

Author: javaboy2012
Email: yanek@163.com
Qq: 1046011462

 

1. Define the tag Library Class UserListTag. java

Package com. yanek. cms. tag;

Import java. io. IOException;
Import java. util. ArrayList;
Import java. util. Iterator;
Import java. util. List;

Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. tagext. BodyTagSupport;

Import com. yanek. cms. vo. UserInfo;

Public class UserListTag extends BodyTagSupport {

Private String name; // an attribute name
Private Iterator it; // the object to be iterated
Private int cateid; // user category id

@ Override
Public int doEndTag () throws JspException {
Try {
If (bodycontent! = NULL ){
Bodycontent. writeout (bodycontent. getenclosingwriter ());
}
} Catch (ioexception e ){
E. printstacktrace ();

}
Return eval_page;
}

@ Override
Public int dostarttag () throws jspexception {

// Here, different list data is constructed based on the user type, which can be obtained based on the database.
List <userinfo> Users = new arraylist <userinfo> ();
If (cateid = 1 ){
Users. Add (New userinfo ("Michael", 20, "Zhangsan@163.com "));

Users. Add (New userinfo ("Li Si", 30, "Lisi@sina.com "));
} Else {
Users. Add (New userinfo ("Wang Wu", 33, "Wangwu@qq.com "));
Users. Add (New userinfo ("Zhao Liu", 33, "zhaoliu@qq.com "));
}
It = users. iterator ();

If (IT = NULL ){
Return skip_body;

} Else {
Return continuenext ();
}
}

Private int continuenext (){
If (it. hasnext ()){
Pagecontext. setattribute (name, it. Next (), pagecontext. page_scope );
Return eval_body_tag;
} Else {

Return skip_body;
}
}

@ Override
Public int doafterbody (){
Return continuenext ();
}

Public String getName (){
Return name;
}

Public void setName (String name ){
This. name = name;
}

Public int getCateid (){
Return cateid;
}

Public void setCateid (int cateid ){
This. cateid = cateid;
}

}

 

 

2. Create the tag library description file my_cms_tag.tld in the WEB-INF directory:

My_cms_tag.tld

<? Xml version = "1.0" encoding = "UTF-8"?>

<! DOCTYPE taglib PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.1 //" http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd ">
<Taglib>
<Tlibversion> 1.0 </tlibversion>
<Jspversion> 1.0 </jspversion>
<Shortname> cms </shortname>
<Uri> http://www.58tech.cn/mystruts/tags-cms </uri>

<! -- UserListTag start -->

<Tag>

<Name> userListTag </name>

<Tag-class> com. yanek. cms. tag. UserListTag </tag-class>


<Body-content> jsp </body-content>

<Variable>
<! -- <Name-given> user_info </name-given> -->
<Name-from-attribute> name </name-from-attribute>
<Variable-class> com. yanek. cms. vo. UserInfo </variable-class>
<Declare> true </declare>
<Scope> NESTED </scope>
</Variable>

<Attribute>
<Name> name </name>
<Required> true </required>
</Attribute>

<Attribute>
<Name> cateid </name>
<Required> true </required>
</Attribute>

</Tag>

<! -- UserListTag end -->

</Taglib>

3. web. xml configuration

<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>


<Taglib>
<Taglib-uri>/tags/my-cms </taglib-uri>
<Taglib-location>/WEB-INF/my_cms_tag.tld </taglib-location>
</Taglib>

</Web-app>

 

4. jsp call

<% @ Page language = "java" import = "java. util. *, com. yanek. cms. vo. *" pageEncoding = "UTF-8" %>
<% @ Taglib uri = "/tags/my-cms" prefix = "myTag" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basepath %>">

<Title> my JSP 'test. jsp 'starting page </title>

<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->

</Head>

<Body>

<Table width = '500px 'border = '1' align = 'center'>

<Tr>

<TD width = '000000'> username </TD>

<TD width = '000000'> age </TD>

<TD> email </TD>

</Tr>

<Mytag: userlisttag name = "user_info1" cateid = "1">
 
<Tr>

<TD> <% = user_info1.getusername () %> </TD>

<TD> <% = user_info1.getage () %> </TD>

<TD> <% = user_info1.getemail () %>

</TD>

</Tr>



</Mytag: userlisttag>
</Table>

<HR>

<Table width = '500px 'border = '1' align = 'center'>

<Tr>

<TD width = '000000'> username </TD>

<TD width = '000000'> age </TD>

<TD> email </TD>

</Tr>

<MyTag: userListTag name = "user_info2" cateid = "2">

<Tr>
<Td> <% = user_info2.getUserName () %> </td>

<Td> <% = user_info2.getAge () %> </td>

<Td> <% = user_info2.getEmail () %>

</Td>
</Tr>
</MyTag: userListTag>
</Table>

</Body>
</Html>

Object Class Definition

Package com. yanek. cms. vo;

Public class UserInfo {
 
Private int age;
Private String userName;
Private String email;
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
Public String getUserName (){
Return userName;
}
Public void setUserName (String userName ){
This. userName = userName;
}
Public String getEmail (){
Return email;
}
Public void setEmail (String email ){
This. email = email;
}
Public UserInfo (String userName, int age, String email ){
Super ();
This. age = age;
This. userName = userName;
This. email = email;
}
 
Public UserInfo (){

}

}

Running effect; http: // 127.0.0.1: 8080/TestCMS/page/userlist. jsp

 

 

: Http://download.csdn.net/detail/5iasp/5118606 (points not required)
 

Related Article

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.