找到display tag 所需jar
编写实体类UserInfo.java
package com.liu.po; public class UserInfo { private Integer userId; private String userName; private String userSex; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } public UserInfo(Integer userId, String userName, String userSex) { this.userId = userId; this.userName = userName; this.userSex = userSex; } }
3.编写UserServlet.java
package com.liu.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.liu.po.UserInfo; /** * Servlet implementation class UserServlet */ @WebServlet("/userlist") public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UserServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<UserInfo> list = new ArrayList<UserInfo>(); for (int i = 0; i < 1000; i++) { UserInfo user = new UserInfo(i, "小三" + i, "女"); list.add(user); } request.setAttribute("list", list); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
4.编写index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@ taglib prefix="d" uri="http://displaytag.sf.net" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"> </head> <body> <div align="center"> <a href="userlist">查询</a> <d:table name="list" class="table" pagesize="10" export="true" requestURI="userlist"> <d:column property="userId" title="编号" sortable="true"></d:column> <d:column property="userName" title="姓名"></d:column> <d:column property="userSex" title="性别"></d:column> </d:table> </div> </body> </html>
5.添加汉化资源文件displaytag.properties,必须放在src目录下
basic.empty.showtable=true basic.show.header=true # page | list sort.amount=list export.amount=list export.decorated=true paging.banner.group_size=8 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=false export.csv.label=<span class="export csv">CSV </span> export.csv.include_header=false export.csv.filename= export.excel=true export.excel.label=<span class="export excel">Excel </span> export.excel.include_header=true export.excel.filename= export.xml=false export.xml.label=<span class="export xml">XML </span> export.xml.filename= export.pdf=false export.pdf.label=<span class="export pdf">PDF </span> export.pdf.include_header=true export.pdf.filename= export.rtf=false export.rtf.label=<span class="export rtf">RTF </span> export.rtf.include_header=true export.rtf.filename= # messages basic.msg.empty_list=\u6ca1\u6709\u53ef\u4ee5\u663e\u793a\u7684\u6570\u636e. basic.msg.empty_list_row=<tr class="empty"><td colspan="{0}">\u6ca1\u6709\u53ef\u4ee5\u663e\u793a\u7684\u6570\u636e.</td></tr> error.msg.invalid_page=\u975e\u6cd5\u9875\u9762 export.banner=<div class="exportlinks">\u8f93\u51fa\u683c\u5f0f: {0}</div> export.banner.sepchar= | paging.banner.item_name= paging.banner.items_name= paging.banner.no_items_found=<span class="pagebanner">\u6ca1\u6709\u8BB0\u5F55.</span> paging.banner.one_item_found=<span class="pagebanner">\u603b\u5171\u4e00\u6761\u8bb0\u5f55.</span> paging.banner.all_items_found=<span class="pagebanner">\u603b\u5171{0}{1}\u6761\u8bb0\u5f55.</span> paging.banner.some_items_found=<span class="pagebanner">\u603b\u5171{0}{1}\u6761\u6570\u636e</span> paging.banner.full=<span class="pagelinks"><a href="{1}">\u9996\u9875</a> <a href="{2}">\u4e0a\u4e00\u9875</a> <a href="{3}">\u4e0b\u4e00\u9875</a> <a href="{4}">\u5c3e\u9875</a> <select class="pagelinks" name="page" onchange="location=page.value">{0}</select></span> paging.banner.first=<span class="pagelinks">\u9996\u9875 \u4e0a\u4e00\u9875 <a href="{3}">\u4e0b\u4e00\u9875</a> <a href="{4}">\u5c3e\u9875</a> <select class="pagelinks" name="page" onchange="location=page.value">{0}</select></span> paging.banner.last=<span class="pagelinks"><a href="{1}">\u9996\u9875</a> <a href="{2}">\u4e0a\u4e00\u9875</a> \u4e0b\u4e00\u9875 \u5c3e\u9875 <select class="pagelinks" name="page" onchange="location=page.value">{0}</select></span> paging.banner.onepage=<span class="pagelinks">\u5171\u4e00\u9875</span> paging.banner.page.selected=<option value="#" selected>{0}</option> #<strong>{0}</strong> paging.banner.page.link=<option value="{1}">{0}</option> # <input size='2'> <input type='button' value='Go'> #<a href="{1}" title="Go to page {0}">{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
6.最终效果