欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-08-24 16:59:11
本文实例讲述了jsp通过自定义标签库实现数据列表显示的方法。分享给大家供大家参考,具体如下: 1. 定义标签库类 userlisttag.java packag...

本文实例讲述了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<userinfo> users = new arraylist<userinfo>();
 if (cateid == 1) {
  users.add(new userinfo("张三", 20, "zhangsan@163.com"));
  users.add(new userinfo("李四", 30, "lisi@sina.com"));
 } else {
  users.add(new userinfo("王五", 33, "wangwu@qq.com"));
  users.add(new userinfo("赵六", 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. 在web-inf目录下 新建标签库描述文件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>
    <required>true</required>
    </attribute>
    <attribute>
    <name>cateid</name>
    <required>true</required>
    </attribute>
</tag>
<!-- userlisttag end -->
</taglib>

3. web.xml配置

<?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调用

<%@ 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='20%'>username</td>
    <td width='20%'>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='20%'>username</td>
    <td width='20%'>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>

实体类定义

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通过自定义标签库实现数据列表显示的方法

完整实例代码代码点击此处。

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