display table外部分页,支持外部排序
操作步骤:
第一部:
web.xml
<!--引入标签--> <jsp-config> <taglib> <taglib-uri>http://jsptags.com/tags/navigation/pager</taglib-uri> <taglib-location>/WEB-INF/displaytag-11.tld</taglib-location> </taglib> </jsp-config> <!--配置,分页后台业务处理servlet--> <servlet-mapping> <servlet-name>CustomerServlet</servlet-name> <url-pattern>/servlet/CustomerServlet</url-pattern> </servlet-mapping> <!--欢迎页面--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
欢迎页面 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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"> </head> <body> <h1><a href="<%=request.getContextPath()%>/servlet/CustomerServlet"><%=request.getContextPath()%>/servlet/CustomerServlet</a></h1> </body> </html>
result.jsp
result.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="display" uri="http://displaytag.sf.net" %> <% 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"> --> <link rel="stylesheet" href="<%=request.getContextPath()%>/css/print.css" type="text/css" media="print" /> <link rel="stylesheet" href="<%=request.getContextPath()%>/css/screen.css" type="text/css" media="screen,print" /> </head> <body> <display:table name="list" class="its" id="item" name="list" sort="external" pagesize="${pageSize}" partialList="true" size="resultSize" requestURI="/displaypagination/servlet/CustomerServlet"> <display:column property="id" title="I" ></display:column> <display:column property="numcuguid" title="ID" sortable="true" ></display:column> <display:column property="vc2cuname" title="vc2cuname" sortable="true"></display:column> <display:column property="vc2cucode" title="vc2cucode" sortable="true"></display:column> </display:table> </body> </html>
servlet
com.tht.customer.servlet.CustomerServlet.java
package com.tht.customer.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.displaytag.pagination.Pagination;
import org.displaytag.tags.TableTagParameters;
import org.displaytag.util.ParamEncoder;
import com.tht.common.db.base.BaseDao;
import com.tht.common.oracle.paging.PageTools;
import com.tht.customer.dao.CustomerDao;
import com.tht.customer.vo.CustomerVO;
public class CustomerServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public CustomerServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//int resultSize=100;//总页数
int pageSize=5;//每页显示的记录数
//得到当前页的字符串
String strP=request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
//请求的需要显示的页码数
int currentPage=0;
if(strP==null){
currentPage=1;
}else{
currentPage=Integer.parseInt(strP);
}
//当前页
System.out.println("currentPage:"+currentPage);
//排序功能
int sortIndex=-1;//排序索引列,第一列为0
int orderIndex=-1;//1为升序,2为降序
String strSort= request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_SORT)));
if(strSort==null){
sortIndex=-1;
}else{
sortIndex=Integer.parseInt(strSort);
}
System.out.println("sortIndex:"+sortIndex);
String strOrder= request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_ORDER)));
if(strOrder==null){
orderIndex=-1;
}else{
orderIndex=Integer.parseInt(strOrder);
}
System.out.println("orderIndex:"+orderIndex);
//总记录数
int totalCount=new BaseDao<CustomerServlet>().getTotalCount("select count(*) from ct_cuinfo");
PageTools pagination=new PageTools(currentPage,pageSize,totalCount);
pagination.setSortIndex(sortIndex);
pagination.setOrderIndex(orderIndex);
CustomerDao customerDao=new CustomerDao();
List<CustomerVO> list=customerDao.getList(pagination);
System.out.println("==============servlet============");
for(CustomerVO vo:list){
System.out.println(vo.getId());
}
request.setAttribute("list", list);
request.setAttribute( "pageSize" , pageSize); // 每页显示的条数
request.setAttribute( "resultSize" , totalCount); // 将总记录数保存成Intger实例保存在request中
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}