DisplayTag是一个开源的自定义标签库(Custom Tag lib),他提供了直接而有效的格式化web视图层数据的有效手段。你可以在现在流行的web应用的MVC模式中集成DisplayTag到View层,其提供的强大表格格式化功能一定会令你爱不释手。或许上面说的有些夸张了,但是DisplayTag在表格的格式化方面表现确实出色,当然,他也只能显示表格,视图层的大部分工作不就是使用表格来格式化数据嘛!
项目结构图,如图一:
第一步,新建com.action.ProductsAction.java这个类, 用来得到一个List<Product>的list对象,实际开发中,这个list将是从数据库中获得
ProductsAction.java:
package com.action; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.bean.Product; import com.opensymphony.xwork2.ActionSupport; public class ProductsAction extends ActionSupport { private List<Product>list; public List<Product> getList() { return list; } public void setList(List<Product> list) { this.list = list; } public String execute() { list=new ArrayList<Product>(); for (int i = 0; i < 80; i++) { Product product=new Product(); product.setEmail("http://email"+"--"+i); product.setMoney(10000.00+i); product.setName("商品"+"--"+i); product.setNum("isbn"+"--"+i); product.setTime(new Date()); product.setUrl("www.google.com"); list.add(product); } System.out.println("数据共有"+list.size()+"条"); return SUCCESS; } }
第二步,新建Product的javabean:
package com.bean; import java.util.Date; public class Product { private String num; private String name; private String email; private double money; private String url; private Date time; //getter,setter略 }
第三步,新建properties资源文件,并配置struts.xml
displaytag_zh_CN.properties: basic.empty.showtable=false basic.show.header=true # page | list sort.amount=list # \u5bfc\u51fa\u8303\u56f4\u8bbe\u7f6e\uff0clist\u4e3a\u5168\u90e8\uff0cpage\u4e3a\u9875\u9762 export.amount=list export.decorated=true paging.banner.group_size=8 # \u4fee\u6539\u5206\u9875\u5bfc\u822a\u6761\u663e\u793a\u7684\u4f4d\u7f6e\uff0ctop\u4e3a\u9876\u90e8\uff0cbottom\u4e3a\u5e95\u90e8\uff0cboth\u4e3a\u9876\u90e8\u548c\u5e95\u90e8 paging.banner.placement=bottom css.tr.even=even css.tr.odd=odd css.th.sorted=sorted css.th.ascending=order1 css.th.descending=order2 css.table= css.th.sortable=sortable # factory classes for extensions factory.requestHelper=org.displaytag.util.DefaultRequestHelperFactory # factory class for decorators factory.decorator=org.displaytag.decorator.DefaultDecoratorFactory # locale provider (Jstl provider by default) locale.provider=org.displaytag.localization.I18nJstlAdapter # locale.resolver (nothing by default, simply use locale from request) #locale.resolver= export.types=csv excel xml pdf export.csv.class=org.displaytag.export.CsvView export.excel.class=org.displaytag.export.ExcelView export.xml.class=org.displaytag.export.XmlView export.pdf.class=org.displaytag.export.PdfView export.csv=true export.csv.label=<span class="export csv">CSV </span> export.csv.include_header=false export.csv.filename=instence.csv export.excel=true export.excel.label=<span class="export excel">Excel </span> export.excel.include_header=true export.excel.filename=instence.xls export.xml=true export.xml.label=<span class="export xml">XML </span> export.xml.filename=instence.xml export.pdf=true export.pdf.label=<span class="export pdf">PDF </span> export.pdf.include_header=true export.pdf.filename=instence.pdf export.rtf=true export.rtf.label=<span class="export rtf">RTF </span> export.rtf.include_header=true export.rtf.filename=instence.rtf # \u5206\u9875\u5bfc\u822a\u6761\u8bbe\u7f6e basic.msg.empty_list=\u6CA1\u6709\u8BB0\u5F55\u53EF\u663E\u793A basic.msg.empty_list_row=<tr class\="empty"><td colspan\="{0}">\u6CA1\u6709\u8BB0\u5F55\u53EF\u663E\u793A</td></tr> error.msg.invalid_page=invalid page # \u5bfc\u51fa\u5bfc\u822a\u6761\u8bbe\u7f6e export.banner=<div class\="exportlinks">\u5BFC\u51FA\u81F3\: {0}</div> # \u5206\u5272\u7b26 export.banner.sepchar= || paging.banner.item_name=\u6761\u8BB0\u5F55 paging.banner.items_name=\u6761\u8BB0\u5F55 paging.banner.no_items_found=<span class\="pagebanner">\u6CA1\u6709\u8BB0\u5F55\u88AB\u627E\u5230.</span> paging.banner.one_item_found=<span class\="pagebanner">\u53EA\u6709\u4E00\u6761\u8BB0\u5F55\u88AB\u627E\u5230.</span> paging.banner.all_items_found=<span class\="pagebanner">{0} {1} \u88AB\u627E\u5230, \u672C\u9875\u663E\u793A\u5168\u90E8\u8BB0\u5F55.</span> paging.banner.some_items_found=<span class\="pagebanner">{0} {1} \u88AB\u627E\u5230, \u672C\u9875\u663E\u793A\u7B2C {2} \u5230\u7B2C {3} \u6761\u8BB0\u5F55.</span> paging.banner.full=<span class\="pagelinks">[<a href\="{1}">\u9996\u9875</a>/<a href\="{2}">\u4E0A\u4E00\u9875</a>] {0} [<a href\="{3}">\u4E0B\u4E00\u9875</a>/<a href\="{4}">\u5C3E\u9875</a>]</span> paging.banner.first=<span class\="pagelinks">[\u9996\u9875/\u4E0A\u4E00\u9875] {0} [<a href\="{3}">\u4E0B\u4E00\u9875</a>/<a href\="{4}">\u5C3E\u9875</a>]</span> paging.banner.last=<span class\="pagelinks">[<a href\="{1}">\u9996\u9875</a>/<a href\="{2}">\u4E0A\u4E00\u9875</a>] {0} [\u4E0B\u4E00\u9875/\u5C3E\u9875]</span> paging.banner.onepage=<span class="pagelinks">{0}</span> paging.banner.page.selected=<strong>{0}</strong> paging.banner.page.link=<a href\="{1}" title\="\u5230\u7B2C{0}\u9875">{0}</a> paging.banner.page.separator=, # external sort and pagination pagination.sort.param=sort pagination.sortdirection.param=dir pagination.pagenumber.param=page pagination.searchid.param=searchid pagination.sort.asc.value=asc pagination.sort.desc.value=desc pagination.sort.skippagenumber=true # unused save.excel.banner=<a href="{0}" rel="external">save ({1} bytes)</a> save.excel.filename=export.xls
struts.xml:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name ="StudentInfo" extends ="struts-default"> <action name="productInfo" class="com.action.ProductsAction"> <result name="success">/productInfo.jsp</result> </action> </package > </struts>
第四步,新建index.jsp和productInfo.jsp
Index.jsp:
<body> <a href="productInfo.action">查询所有商品信息</a> </body>
productInfo.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="display" uri="http://displaytag.sf.net/el" %> <% 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 'index.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> <display:table pagesize="9" export="true" requestURI="productInfo.action" name="list"> <display:column property="num" title="商品编号" group="1" sortable="true"/> <display:column property="name" title="商品名称" group="2" sortable="true"/> <display:column property="email" title="反馈地址" autolink="true"/> <display:column property="money" title="商品价格" group="3" sortable="true"/> <display:column property="url" title="官方网站" autolink="true"/> <display:column property="time" title="出场时间" format="{0,time,yyyy-mm-dd}"/> <display:column/> </display:table> </body> </html>
第五步,配置web.xml并部署运行项目
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"> <filter> <filter-name>s2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <!-- 配置displaytag的过滤器 --> <filter> <filter-name>exportFilter</filter-name> <filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class> </filter> <filter-mapping> <filter-name>s2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
部署项目到tomcat下,在浏览器键入:
http://localhost:8080/displaytag