显示List的类
import java.io.IOException; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.IterationTag; import javax.servlet.jsp.tagext.TagSupport; /** * 分页显示List对象数据 * */ public class PageList extends TagSupport { /** * 表格样式 */ String tableStyle = " border=‘1px solid gray‘"; /** * 第一行格式 * @see 标题列,左对齐好看点 */ String firstLineStyle = " style=‘vertical-align:middle; text-align:left;‘"; /** * 鼠标悬停事件:查看一行时,添加背景色 */ String onMounse = "onmousemove=‘javascript:function addBack(x)" + "{x.style.background=\"orange\" };addBack(this)‘ onmouseout=‘javascription:function noBack(x)" +"{x.style.background=\"\" };noBack(this)‘"; private int currentNum = -1; private int endNum = -1; public int getCurrentNum() { return currentNum; } /** * 保存在request对象中的数据集合的关键字 */ private String dataKey; public void setDataKey(String dataKey) { this.dataKey = dataKey; } @SuppressWarnings({ "static-access", "rawtypes" }) @Override public int doStartTag() throws JspException { Object obj = pageContext.getAttribute(dataKey); if (null == obj) { return super.SKIP_BODY; } List pageList = (List) obj; if (pageList.isEmpty()) { return super.SKIP_BODY; } currentNum = -1; endNum = pageList.size(); JspWriter out = pageContext.getOut(); StringBuffer sb = new StringBuffer(); sb.append("<table width=‘400px‘ cellspacing=‘0‘ cellpadding=‘4‘ " + tableStyle + ">"); sb.append("<tr " + firstLineStyle + ">"); try { out.write(sb.toString()); } catch (IOException e) { e.printStackTrace(); } // 执行当前标签里的内容 return EVAL_BODY_INCLUDE; } /** * Tag.SKIP_PAGE:表示立刻停止执行网页 * Tag.EVAL_PAGE:表示按正常的流程继续执行JSP网页 */ public int doEndTag() throws JspException { JspWriter out = pageContext.getOut(); try { out.print("</table> "); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return EVAL_PAGE; } @SuppressWarnings("unchecked") public int doAfterBody() throws JspException { currentNum++; // 自增,0表示已经运行了一次 JspWriter out = pageContext.getOut(); if (currentNum < endNum) { List<Object> objs = (List<Object>) pageContext.findAttribute(dataKey); Object obj = objs.get(getCurrentNum()); pageContext.setAttribute("dateKey", obj); try { out.print("</tr><tr " + onMounse + ">"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return IterationTag.EVAL_BODY_AGAIN; } else { try { out.print("</tr>"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return IterationTag.SKIP_BODY; } } /** * 第一行数据前添加title(表头) * @return */ public boolean isTitle() { if (currentNum == -1) { return true; } return false; } }
显示List里对象数据的类
import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.Tag; import javax.servlet.jsp.tagext.TagSupport; import org.apache.commons.lang3.StringUtils; /** * 分页显示List对象数据:显示每个对象具体内容 * */ public class ListColumn extends TagSupport { /** * 显示的列头(表头) */ private String header; /** * 鼠标悬停时显示的内容 */ private String title; private String name; // 是一个class结构,通过反射技术得到对象的数据类型,不提供setter访问器 private Class entity = null; /** * 当前标签为显示对象的具体字段,不单独使用 */ @Override public int doStartTag() throws JspException { Tag tag = this.getParent(); if (!(tag instanceof PageList)) { throw new JspException("Tag can‘t be used alone.To use it with tagList"); } PageList pTag = (PageList) this.getParent(); JspWriter out = pageContext.getOut(); StringBuffer buffer = new StringBuffer(); try { String currTitle = ""; if (StringUtils.isNotEmpty(title)) { currTitle = "\" title=" + title + "\"";//getTexts( } if (pTag.isTitle()) { // 输出标题 buffer.append("<th>" + header + "</th>"); } else { // 输出身体 buffer.append("<td " + currTitle + ">"); // body交给页面添加内容 // 设置 数据到身体 Object object = pageContext.getAttribute("dateKey"); // 通过反射得到obj对象的数据类型 entity = object.getClass(); String value = ""; // 得到getter访问器的名称 String getMethodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); // 使用反射技术得到该类型(entity)中定义的方法对象 Method method = entity.getDeclaredMethod(getMethodName, null); if (method != null) { // 调用method对象的invoke方法,从而得到该方法的返回值 value = method.invoke(object, null).toString(); } buffer.append(value); } out.print(buffer.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } return EVAL_BODY_INCLUDE; } public void setHeader(String header) { this.header = header; } public void setTitle(String title) { this.title = title; } public void setName(String name) { this.name = name; } public int doEndTag() throws JspException { PageList pTag = (PageList) this.getParent(); JspWriter out = pageContext.getOut(); try { if (pTag.isTitle()) { // 是否是标题 // out.print( "<td>"+title+"</td>"); } else { // 输出身体 out.print("</td>"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return EVAL_PAGE; } }
tld.xml自定义标签描述文件
修改tag-class为真实包路径
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>page</short-name> <uri>tags/page</uri> <!-- 父标签 --> <tag> <name>list</name> <!-- 修改为真实包路径 --> <tag-class>xxx.PageList</tag-class> <!-- scriptless 主体可以有内容, 而jsp容器会去处理里面的jsp元素, 换句话就是可以是文本, EL表达式, 标准动作甚至另一个自定义标记. --> <body-content>scriptless</body-content> <attribute> <name>dataKey</name> <required>true</required> <!-- rtexprvalue 是否允许表达式 不允许时${1+1}出错 --> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <!-- 子标签 --> <tag> <name>column</name> <!-- 修改为真实包路径 --> <tag-class>xxx.ListColumn</tag-class> <body-content>scriptless</body-content> <attribute> <name>header</name> <required>true</required> <!-- rtexprvalue 是否允许表达式 不允许时${1+1}出错 --> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>title</name> <required>false</required> <!-- rtexprvalue 是否允许表达式 不允许时${1+1}出错 --> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>name</name> <required>true</required> <!-- rtexprvalue 是否允许表达式 不允许时${1+1}出错 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="xxx.user.*"%> <%@ taglib prefix="page" uri="/tags/page"%> <html> <body> <% User user = new User(); user.setName("name"); List<User> users = new ArrayList<User>(); users.add(user); pageContext.setAttribute("users", users); %> <page:list dataKey="users"> <page:column name="name" header="用户名"></page:column> <%--<page:column name="xxx" header="xxx"></page:column>--%> </page:list> </body> </html>
原文:https://www.cnblogs.com/leonlipfsj/p/12313655.html