标签:
近期由于学习需要,学了自定义标签;一直在思索一个问题:如何是的自定义的表格标签可以被多次使用,而不是用一个写一个,减少重复代码量,提高代码重用性。该自定义标签,由于表头和表格内容都是由用户自定义的,具有很好的拓展性,可以适应几乎所有的表格。
如果思路不对或者有更好的建议,欢迎相告。
话不多说,直接上代码:
PageTag.java
package com.blk.tag; import java.io.IOException; import java.lang.reflect.Field; import java.util.List; import javax.servlet.jsp.JspContext; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.JspTag; import javax.servlet.jsp.tagext.SimpleTag; public class PageTag implements SimpleTag{ private JspFragment jspBody; private JspContext pc; private List<String> title; // 标题 private List<Object> content; // 表格内容 public void setTitle(List<String> title) { this.title = title; } public void setContent(List<Object> content) { this.content = content; } public void doTag() throws JspException, IOException { JspWriter jw = pc.getOut(); jw.print("<table cellspacing=‘0‘>"); // 标题行数据 jw.print("<tr>"); for (String str:title) { jw.print(" <td>"+str+"</td>"); } jw.print("</tr>"); // 获取Emp对象的class属性 Class type = content.get(0).getClass(); // 获取Emp的属性 Field[] fs = type.getDeclaredFields(); try { for (Object obj :content) { jw.print("<tr>"); for (Field f:fs) { // 设置该属性可访问 f.setAccessible(true); jw.print(" <td>"+f.get(obj)+"</td>"); } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } jw.print("</table>"); } public JspTag getParent() { return null; } public void setJspBody(JspFragment jspBody) { this.jspBody = jspBody; } public void setJspContext(JspContext pc) { this.pc = pc; } public void setParent(JspTag parent) { } }
page.tld
<?xml version="1.0" encoding="UTF-8" ?> <taglib 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-jsptaglibrary_2_1.xsd" version="2.1"> <description>JSTL 1.1 core library</description> <display-name>JSTL core</display-name> <tlib-version>1.1</tlib-version> <short-name>page</short-name> <uri>http://www.hyb.com/my</uri> <tag> <name>page</name> <tag-class>com.blk.tag.PageTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>title</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>content</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://www.hyb.com/my" prefix="page"%> <% 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 ‘MyJsp.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"> <style> table tr td { border:1px solid black; height:30px; width:90px; text-align:center; } </style> </head> <body> <page:page title="${title}" content="${content }"></page:page> </body> </html>
标签:
原文地址:http://www.cnblogs.com/IceGhostHYB/p/5730196.html