jsp通过自定义标签库实现数据列表显示的方法

本文实例讲述了jsp通过自定义标签库实现数据列表显示的方法。分享给大家供大家参考,具体如下:

1. 定义标签库类 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;// 一个属性名
 private Iterator it;// 要迭代的对象
 private int cateid; // 用户类别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 {
 //这里根据用户类型,构造不同的列表数据,实现可以根据数据库获取
 List users = new ArrayList();
 if (cateid == 1) {
  users.add(new UserInfo("张三", 20, "[email protected]"));
  users.add(new UserInfo("李四", 30, "[email protected]"));
 } else {
  users.add(new UserInfo("王五", 33, "[email protected]"));
  users.add(new UserInfo("赵六", 33, "[email protected]"));
 }
 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. 在WEB-INF目录下 新建标签库描述文件my_cms_tag.tld:

my_cms_tag.tld




1.0
1.0
cms
http://www.58tech.cn/mystruts/tags-cms

  
    userListTag
    com.yanek.cms.tag.UserListTag
    jsp
   
    
    name
    com.yanek.cms.vo.UserInfo
    true
    NESTED
   
    
    name
    true
    
    
    cateid
    true
    




3. web.xml配置



 
  index.jsp
 
 
   /tags/my-cms
   /WEB-INF/my_cms_tag.tld
 

4. jsp调用

<%@ 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+"/";
%>


 
  
  My JSP 'test.jsp' starting page
 
 
   
 
 
 
 
 
 
    
UserName Age Email
<%=user_info1.getUserName() %> <%=user_info1.getAge() %> <%=user_info1.getEmail() %>

UserName Age Email
<%=user_info2.getUserName() %> <%=user_info2.getAge() %> <%=user_info2.getEmail() %>

实体类定义

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

运行效果如下图 (URL输入:http://127.0.0.1:8080/TestCMS/page/userlist.jsp)

jsp通过自定义标签库实现数据列表显示的方法_第1张图片

完整实例代码代码点击此处本站下载。

希望本文所述对大家Android程序设计有所帮助。

你可能感兴趣的:(jsp通过自定义标签库实现数据列表显示的方法)