displaytag1.2配置

一:jar包下载
http://displaytag.sourceforge.net/1.2/download.html


二: 开始吧
2.1 ,我们只用displaytag来显示List类型的数据,如果要分页显示的话,我们采用内存分页 法,要达到这个要求是很容易的
(1) 必须的jar包,共9个
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-lang-2.3.jar
commons-logging-1.4.2.jar
displaytag-1.2.jar
log4j-1.2.13.jar
slf4j-api-1.4.2.jar
slf4j-log4j12-1.4.2.jar
standard-1.0.6.jar
注: 除了displaytag-1.2.jar外,其它都是java web工程常用到的jar包
(2) 如果您的servlert是 2.4或tomcat4以下版本
A :解压displaytag-1.2.jar包,在META_INF/下找到 displaytag.tld文件,
拷贝到:你的工程/WEB_INF/下
B :在web.xml中配置
<taglib> <taglib-uri>/displaytag</taglib-uri> <taglib-location>/WEB-INF/displaytag.tld</taglib-location> </taglib>
C :在 使用到displaytag的jsp页面里加入:
<%@ taglib uri="/display" prefix="d" %>
D :真正使用时,用<d:table></d:table>就行了
(3) 如果你的 tomcat是5,6以上版本,即servlet是2.4以上的只要在jsp页面中加入以下就可以用了:
<%@ taglib uri="http://displaytag.sf.net/el" prefix="d" %>
注意

1:uri一定要与 displaytag-1.2.jar包里的/META_INF/下找到displaytag.tld文件的uri一至,才起作用

2:这里再解释一下,只要在standard.jar/meta-inf/下的xx.tld文件,会被tomcat5以上版本自动加载,我测试了一下,只要是任何一个jar包的meta-inf里有xx.tld文件,就可以直接在jsp页面中引入该标签了,uri一定要匹配。
(4) 现 在你就可以测试显示数据了


2.2 ,我们要实现真正的分页显示了

(1)首先准备一个PageList类,给出代码

package util; import java.util.List; import org.displaytag.pagination.PaginatedList; import org.displaytag.properties.SortOrderEnum; /** * displaytag分页列表 * @author collonn */ public class PageList implements PaginatedList { /** * 每页列表,即每页显示的记录数 */ private List list; /** * 当前页码,即当前第几页 */ private int pageNumber = 1; /** * 每页记录数 pagesize */ private int objectsPerPage = 15; /** * 总记录数 */ private int fullListSize = 0; /** *从第几条记录开始,用来定位offset */ private int offset = 0; private String sortCriterion; private SortOrderEnum sortDirection; private String searchId; public List getList() { return list; } public void setList(List list) { this.list = list; } public int getPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; } public int getObjectsPerPage() { return objectsPerPage; } public void setObjectsPerPage(int objectsPerPage) { this.objectsPerPage = objectsPerPage; } public int getFullListSize() { return fullListSize; } public void setFullListSize(int fullListSize) { this.fullListSize = fullListSize; } public String getSortCriterion() { return sortCriterion; } public void setSortCriterion(String sortCriterion) { this.sortCriterion = sortCriterion; } public SortOrderEnum getSortDirection() { return sortDirection; } public void setSortDirection(SortOrderEnum sortDirection) { this.sortDirection = sortDirection; } public String getSearchId() { return searchId; } public void setSearchId(String searchId) { this.searchId = searchId; } public int getOffset() { return (this.getPageNumber() - 1) * this.getObjectsPerPage(); } }

 

(2)我们以mysql为例来做分页测试,给出sql语句如下:

select id, name, des, createTime, modifyTime from service order by id limit ?,?

 

(3)jsp页面中,我们这样写

<d:table name="resultList" requestURI="/test">

 

(4)给出servlet的doPost()代码,公供参考

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pageNumber = request.getParameter("page"); if(pageNumber == null){ pageNumber = "1"; } PageList pageList = new PageList(); pageList.setPageNumber(Integer.parseInt(pageNumber)); pageList.setObjectsPerPage(4); pageList.setFullListSize(11); System.out.println("----------->currentPage:" + pageNumber); Connection connection = DataSourceUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; List list = null; String sql = "select id, name, des, createTime, modifyTime from service order by id limit ?,?"; try { pst = connection.prepareStatement(sql); pst.setInt(1, pageList.getOffset()); pst.setInt(2, 4); rs = pst.executeQuery(); Result result = new Result(rs); list = result.getResult(); pageList.setList(list); System.out.println("----------->total:" + list.size()); request.setAttribute("resultList", pageList); request.getRequestDispatcher("/result.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); }finally{ try { rs.close(); pst.close(); connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

(5)OK,就这几个要点,自己测试去吧

 

2.3 ,中文问题和displaytag.properties配置说明

(1):分页的中文问题

中文代码:

paging.banner.first = <span class="pagelinks">[首页/上页] {0}[<a href="{3}" mce_href="{3}">下页</a>/<a href="{4}" mce_href="{4}">尾页</a>]</span> paging.banner.full = <span class="pagelinks">[<a href="{1}" mce_href="{1}">首页</a>/<a href="{2}" mce_href="{2}">上页</a>]{0}[<a href="{3}" mce_href="{3}">下页/<a href="{4}" mce_href="{4}">尾页</a>]</span> paging.banner.last = <span class="pagelinks">[<a href="{1}" mce_href="{1}">首页</a>/<a href="{2}" mce_href="{2}">上页</a>]{0} [下页/尾页]</span> paging.banner.some_items_found = <span class="pagebanner">共<b>{0}</b>条,第<b>{2}</b>到<b>{3}</b>条 .</span>

 

unicode代码

paging.banner.first = <span class="pagelinks">[/u9996/u9875//u4e0a/u9875] {0}[<a href="{3}" mce_href="{3}">/u4e0b/u9875</a>/<a href="{4}" mce_href="{4}">/u5c3e/u9875</a>]</span> paging.banner.full = <span class="pagelinks">[<a href="{1}" mce_href="{1}">/u9996/u9875</a>/<a href="{2}" mce_href="{2}">/u4e0a/u9875</a>]{0}[<a href="{3}" mce_href="{3}">/u4e0b/u9875/<a href="{4}" mce_href="{4}">/u5c3e/u9875</a>]</span> paging.banner.last = <span class="pagelinks">[<a href="{1}" mce_href="{1}">/u9996/u9875</a>/<a href="{2}" mce_href="{2}">/u4e0a/u9875</a>]{0} [/u4e0b/u9875//u5c3e/u9875]</span> paging.banner.some_items_found = <span class="pagebanner">/u5171<b>{0}</b>/u6761/uff0c/u7b2c<b>{2}</b>/u5230<b>{3}</b>/u6761 .</span>

 

三:Fighting And Keep Moving

你可能感兴趣的:(tomcat,jsp,servlet,String,list,Class)