在SpringMVC和Hibernate应用程序中的JSP页面中实现分页

在SpringMVC和Hibernate应用程序中的JSP页面中实现分页,spring,hibernate,jsp,spring-mvc,twitter-bootstrap,Spring,Hibernate,Jsp,Spring Mvc,Twitter Bootstrap,我正在尝试在JSP页面中实现分页。我正在使用SpringMVC和Hibernate。java代码部分还可以,但我在JSP页面中实现它时遇到了困难。我正在使用twitter引导 以下是我到目前为止所做的: <div class="container"> <table class="table table-hover"> <th>First Name</th> <th>Last Name</th

我正在尝试在JSP页面中实现分页。我正在使用SpringMVC和Hibernate。java代码部分还可以,但我在JSP页面中实现它时遇到了困难。我正在使用twitter引导

以下是我到目前为止所做的:

<div class="container">
    <table class="table table-hover">
        <th>First Name</th>
        <th>Last Name</th>
        <th>Age</th>
        <c:forEach items="${employees}" var="emp">
        <tr>
            <td>${emp.firstName}</td>
            <td>${emp.lastName}</td>
            <td>${emp.Age}</td>
        </tr>
        </c:forEach>
    </table>

    <div class="pagination">
        <ul>
            <li class="disabled"><a href="#">First</a></li>
            <li class="disabled"><a href="#">Prev</a></li>
            <li class="active"><a href="#">1</a></li>
            <li class="active"><a href="#">2</a></li>
            <li class="active"><a href="#">3</a></li>
            <li class="active"><a href="#">4</a></li>
            <li class="active"><a href="#">5</a></li>
            <li class="active"><a href="#">Next</a></li>
            <li class="active"><a href="#">Last</a></li>
        </ul>
    </div>
</div>
这是我的服务类中的相关代码:

public List<Employee> getEmployees(int page) {
        return employeeDao.getEmployeeList(page);
}
public List getEmployees(int页){
返回employeeDao.getEmployeeList(第页);
}
这是我的DAO类中的相关代码:

private static final int limitResultsPerPage = 3;

public List<Employee> getEmployeeList(int page) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "from Employee");
        q.setFirstResult(page * limitResultsPerPage); 
        q.setMaxResults(limitResultsPerPage);
        return (List<Employee>) q.list();
}
private static final int limitResultsPerPage=3;
公共列表getEmployeeList(内部页面){
Query q=sessionFactory.getCurrentSession().createQuery(
“来自雇员”);
q、 setFirstResult(第页*limitResultsPerPage);
q、 setMaxResults(limitResultsPerPage);
返回(列表)q.List();
}
显示该表的页面是list.jsp

以下是我的假设,我应该做什么,但不知道如何做(如果我走错了路,请纠正我):

修改我菜单中指向list.jsp的链接,将其修改为list.jsp?page=0,这样每当用户单击链接时,他都会到达第一页

当用户单击其中一个按钮时,我需要将页码传递给我的控制器,以便我能够在查询中返回正确的“员工”

如您所见,第一个和上一个按钮被禁用,因为我当前在第一页。所以我的问题是,我应该如何处理这些按钮的激活/停用,以及下一个和最后一个按钮


另外,如何“刷新”列表上的数字?例如,如果用户在第20页,我不会显示从1到19的按钮,至少回答您的一个问题:

您可以使用RequestParam注释将页码和其他参数从JSP传递到控制器,如下所示:

  @RequestMapping(value = "/list", method = RequestMethod.GET)
  public String getEmployees(@RequestParam(value = "page", required = false) Long page, ModelMap model) {
        //now page is available.
    model.addAttribute("employees", this.employeeService.getEmployees(page));
    return "listing";
  }
  list/?page=1
您的链接将如下所示:

  @RequestMapping(value = "/list", method = RequestMethod.GET)
  public String getEmployees(@RequestParam(value = "page", required = false) Long page, ModelMap model) {
        //now page is available.
    model.addAttribute("employees", this.employeeService.getEmployees(page));
    return "listing";
  }
  list/?page=1

分页是一个相当复杂的过程,但这里有一些想法。您可以在JSP页面上使用JSTL来实现逻辑。例如:

    <c:if test="${page > 1}">
       <li class="active"><a href="#">First</a></li>
    </c:if>
在JSP页面上,您可以执行以下循环:

    <c:forEach begin="${startpage}" end="${endpage}" var="p">
         <a href="#">${p}</a>
    </c:forEach>


等等。

文森特·拉姆达尼的解决方案是正确的:谢谢。Dukable:我根据Vincent Ramdhanie的解决方案使用了这段代码,它运行得非常好:类似这样的代码应该可以在您的代码中使用

@RequestMapping(value = "/list", method = RequestMethod.GET)
public String getEmployees(@RequestParam(value = "page", required = false) Long page, ModelMap model) {
    //now page is available.

    int startpage = (int) (page - 5 > 0?page - 5:1);
    int endpage = startpage + 10;

    model.addAttribute("employees", this.employeeService.getEmployees(page));

    model.addAttribute("startpage",startpage);
    model.addAttribute("endpage",endpage);

    return "listing";
}
在jsp中:

<div class="pagination">
    <ul>
        <li><c:forEach begin="${startpage}" end="${endpage}" var="p"><a href="<c:url value="/list" ><c:param name="page" value="${p}"/>${p}</c:url>">${p}</a></c:forEach></li>
    </ul>
</div>
`
  • &拉阔;
`

谢谢你,我确实听从了你的建议,没有问题:)你知道管理第一个、上一个、下一个和最后一个按钮的最佳做法是什么吗?谢谢你添加了更多的细节Vincent:)最后一个问题,我的链接名,我想使用${page}作为当前页面的编号没有解释,我可能遗漏了什么:
  • http://localhost:8080/Project/list/?page=1
    
    `<div class="box-tools pull-right">
                           <ul class="pagination pagination-sm inline">
                              <c:if test="${pageCtn gt 1}">
                                 <li>
                                    <c:choose>
                                       <c:when test="${currentPage gt 1}">
                                          <a
                                             href="${pageContext.request.contextPath}/member/showmemberpage/${currentPage-1}/pagination.do">&laquo;</a>
                                       </c:when>
                                       <c:otherwise>
                                          <a>&laquo;</a>
                                       </c:otherwise>
                                    </c:choose>
                                 </li>
                                 <li><a href="#">${currentPage} to ${pageCtn}</a></li>
                                 <li>
                                    <c:choose>
                                       <c:when test="${currentPage lt pageCtn}">
                                          <a
                                             href="${pageContext.request.contextPath}/member/showmemberpage/${currentPage+1}/pagination.do">&raquo;</a>
                                       </c:when>
                                       <c:otherwise>
                                          <a>&raquo;</a>
                                       </c:otherwise>
                                    </c:choose>
                                 </li>
                              </c:if>
                           </ul>
                        </div>`