主頁(yè) > 知識(shí)庫(kù) > jsp通過(guò)自定義標(biāo)簽庫(kù)實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法

jsp通過(guò)自定義標(biāo)簽庫(kù)實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法

熱門(mén)標(biāo)簽:服務(wù)器配置 Linux服務(wù)器 Mysql連接數(shù)設(shè)置 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 阿里云 電子圍欄

本文實(shí)例講述了jsp通過(guò)自定義標(biāo)簽庫(kù)實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法。分享給大家供大家參考,具體如下:

1. 定義標(biāo)簽庫(kù)類(lèi) 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;// 一個(gè)屬性名
 private Iterator it;// 要迭代的對(duì)象
 private int cateid; // 用戶(hù)類(lèi)別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 {
 //這里根據(jù)用戶(hù)類(lèi)型,構(gòu)造不同的列表數(shù)據(jù),實(shí)現(xiàn)可以根據(jù)數(shù)據(jù)庫(kù)獲取
 ListUserInfo> users = new ArrayListUserInfo>();
 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目錄下 新建標(biāo)簽庫(kù)描述文件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調(diào)用

%@ 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>

實(shí)體類(lèi)定義

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

運(yùn)行效果如下圖 (URL輸入:http://127.0.0.1:8080/TestCMS/page/userlist.jsp)

完整實(shí)例代碼代碼點(diǎn)擊此處本站下載。

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • jsp自定義標(biāo)簽用法實(shí)例詳解
  • JSP使用自定義標(biāo)簽防止表單重復(fù)提交的方法
  • jsp中自定義標(biāo)簽用法實(shí)例分析
  • JSP自定義分頁(yè)標(biāo)簽TAG全過(guò)程
  • jsp自定義標(biāo)簽之ifelse與遍歷自定義標(biāo)簽示例
  • JSP自定義標(biāo)簽獲取用戶(hù)IP地址的方法
  • 基于JSP 自定義標(biāo)簽使用實(shí)例介紹
  • JSP自定義標(biāo)簽Taglib實(shí)現(xiàn)過(guò)程重點(diǎn)總結(jié)
  • jsp 自定義標(biāo)簽實(shí)例
  • JSP 自定義標(biāo)簽之一 簡(jiǎn)單實(shí)例
  • jsp 標(biāo)準(zhǔn)標(biāo)簽庫(kù)簡(jiǎn)析
  • 用定制標(biāo)簽庫(kù)和配置文件實(shí)現(xiàn)對(duì)JSP頁(yè)面元素的訪問(wèn)控制

標(biāo)簽:廣元 萍鄉(xiāng) 蚌埠 衡水 衢州 江蘇 棗莊 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp通過(guò)自定義標(biāo)簽庫(kù)實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266