JSP through the custom tag library to implement the data list display method _jsp programming

Source: Internet
Author: User
Tags class definition tld

This article is an example of a JSP through a custom tag library to achieve a list of data display methods. Share to everyone for your reference, specific as follows:

1. Define 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;//a property name private iterator it;//to iterate objects private in T Cateid; User class ID @Override public int doendtag () throws Jspexception {try {if (bodycontent!= null) {Bodycontent.writeo
  UT (Bodycontent.getenclosingwriter ());
 } catch (IOException e) {e.printstacktrace ();
 return eval_page; @Override public int doStartTag () throws Jspexception {//here, according to the user type, constructs different list data that can be obtained list<userinfo> users based on the database
 = new Arraylist<userinfo> ();
  if (Cateid = = 1) {Users.add (New UserInfo ("John", "Zhangsan@163.com"));
 Users.add (New UserInfo ("Dick", "Lisi@sina.com"));
  else {users.add (new UserInfo ("Harry", "Wangwu@qq.com")); Users.add (New UserInfo("Zhao Liu", "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;
 The public void Setcateid (int cateid) {This.cateid = Cateid;

 }
}

2. Create a new Tag library description file in the Web-inf directory My_cms_tag.tld:

My_cms_tag.tld

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.1//en" "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> <r Equired>true</required> </attribute> <attribute> <name>cateid</name> <required>tru

 E</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/" 
 Java ee "
 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" >  

Entity 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 () {
 }
}

The operation effect is as follows (URL input: http://127.0.0.1:8080/TestCMS/page/userlist.jsp)

Full instance code code click here to download the site.

I hope this article will help you with the Android program.

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.