以前做的项目都是SSH框架,当然分页参数的传递可以通过' .../../.jsp?startIndex='+startIndex 传递,但在SpringMVC +Hibernate框架下后台不能获取startIndex的参数,形式是'.../../.htm?startIndex='+startIndex ,研究半
天,终于搞定,至于为什么不清楚?
1:application.xml 配置信息(部分)
<bean id="test3Service" class="test.Test3ServiceImpl"> <property name="sessionFactory" ref="sessionFactory"> </property> </bean>
2:application-security.xml 配置如下(部分)
<security:intercept-url pattern="/*.htm" access="IS_AUTHENTICATED_REMEMBERED"/>
3:spring-mvc-servlet.xml 配置如下(部分)
<bean name="/Test3.htm" class="test.Test3Controller"> <property name="test3Service" ref="test3Service"/> </bean>
4:Test3Service 接口
public interface Test1Service { public List<Expert> findExpertList(); }
5:Test3ServiceImpl 实现
public class Test3ServiceImpl extends HibernateDaoSupport implements Test3Service { @Override public PaginationSupport findPageByQuery(int pageSize, int startIndex) { Session session = this.getHibernateTemplate().getSessionFactory().openSession(); String hsql = " from Expert e order by id desc"; Query query = session.createQuery(hsql); int totalCount=query.list().size(); query.setFirstResult(startIndex); query.setMaxResults(pageSize); List items = query.list(); PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex); session.close(); return ps; } }
6:Test3Controller
public class Test3Controller implements Controller { private static Logger log = Logger.getLogger("Test3Controller"); private Test3Service test3Service; private PaginationSupport userPage; private int pageSize = 10; private int startIndex; private List experts; @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { log.info("Test3Controller 分页查询!"); String i = request.getParameter("startIndex"); if (i != null || "".equals(i)) { startIndex = Integer.parseInt(i); System.out.println("ii:" + i + "startIndex:" + startIndex); } userPage = test3Service.findPageByQuery(pageSize, startIndex); request.setAttribute("userPage", userPage); return new ModelAndView("Test3", "experts", userPage.getItems()); } public Test3Service getTest3Service() { return test3Service; } public void setTest3Service(Test3Service test3Service) { this.test3Service = test3Service; } public PaginationSupport getUserPage() { return userPage; } public void setUserPage(PaginationSupport userPage) { this.userPage = userPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getStartIndex() { return startIndex; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } public List getExperts() { return experts; } public void setExperts(List experts) { this.experts = experts; } }
7:Test3.jsp
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="GBK"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link type="text/css" rel="stylesheet" href="../css/panel.css"> <title>显示所有用户</title> <script type="text/javascript"> function setStartIndex(startIndex){ alert(startIndex); location.href='Test3.htm?startIndex='+startIndex; } </script> </head> <body> <table border="1" cellspacing="0" cellpadding="0" width="100%" align="center"> <tr> <td> <br> <table class="list" border="1" width="100%"> <tr align="center" height="25"> <td width="4%"> <strong>序号</strong> </td> <td width="7%"> <strong>专家描述</strong> </td> </tr> <c:forEach var="q" items="${experts}" varStatus="status"> <tr> <td> ${q.id} </td> <td> ${q.description} </td> </tr> </c:forEach> </table> </td> </tr> </table> <c:if test="${!empty userPage}"> 共${userPage.totalCount}记录 <c:choose> <c:when test="${userPage.startIndex ne '0'}"> <a href="Test3.htm?startIndex=0">首页</a> </c:when> <c:otherwise> 首页 </c:otherwise> </c:choose> <c:choose> <c:when test="${userPage.previousIndex lt userPage.startIndex}"> <!-- <a href="Test3.htm?startIndex=${userPage.previousIndex }">上一页</a> --> <a href="javascript:setStartIndex(${userPage.previousIndex })">上一页</a> </c:when> <c:otherwise> 上一页 </c:otherwise> </c:choose> <c:choose> <c:when test="${userPage.nextIndex>userPage.startIndex}"> <!-- - <a href="Test3.htm?startIndex=${userPage.nextIndex}">下一页</a> --> <a href="javascript:setStartIndex(${userPage.nextIndex})">下一页</a> </c:when> <c:otherwise> 下一页 </c:otherwise> </c:choose> <c:choose> <c:when test="${userPage.lastIndex eq userPage.startIndex}"> 最后一页 </c:when> <c:otherwise> <!-- <a href="Test3.htm?startIndex=${userPage.lastIndex}">最后一页</a> --> <a href="javascript:setStartIndex(${userPage.lastIndex})">最后一页</a> </c:otherwise> </c:choose> </c:if> </body> </html>