Jsp uses the custom tag library to display the data list.

Source: Internet
Author: User

Jsp uses the custom tag library to display the data list.

This example describes how to display the data list in jsp by using a custom tag library. We will share this with you for your reference. The details are as follows:

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; // private int cateid of the object to be iterated; // 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 {// different list data is constructed based on the user type, you can obtain the List <UserInfo> users = new ArrayList <UserInfo> (); if (cateid = 1) {users. add (new UserInfo ("Michael", 20, "Zhangsan@163.com"); users. add (new UserInfo ("", 30, "Lisi@sina.com");} else {users. add (new UserInfo ("Wang Wu", 33, "Wangwu@qq.com"); users. add (new UserInfo ("Zhao ", 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//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>    <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">

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

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

Click here to download the complete instance code.

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

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.