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

时间:2013-01-14 03:00:03

标签: spring hibernate jsp spring-mvc twitter-bootstrap

我正在尝试在JSP页面中实现分页。我正在使用Spring MVC和Hibernate。 java代码部分没问题但我在JSP页面中实现它有困难。我正在使用twitter bootstrap。

这是我到目前为止所做的事情:

<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>

这是我控制器中的相关代码:

@RequestMapping(value = "/list", method = RequestMethod.GET)
    public String getEmployees(ModelMap model) {
            **//I believe I should get the page number from the JSP here but how?**
        model.addAttribute("employees", this.employeeService.getEmployees(page));
        return "listing";
}

这是我的服务类中的相关代码:

public List<Employee> getEmployees(int page) {
        return employeeDao.getEmployeeList(page);
}

这是我的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();
}

我显示该表的页面是list.jsp

以下是我应该做什么但我不知道怎么做的假设(如果我采取错误的方式,请纠正我):

修改我的菜单中指向list.jsp的链接到list.jsp?page = 0,所以每次用户点击链接时,他都会到达第一页。

当用户点击其中一个按钮时,我需要将页码传递给我的控制器,这样我就可以设法通过查询返回正确的“员工”。

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

另外,如何“刷新”列表中的数字?例如,如果用户位于第20页,我将不会显示1到19的按钮?

3 个答案:

答案 0 :(得分:6)

至少回答你的一个问题:

您可以使用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

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

    <c:if test="${page > 1}">
       <li class="active"><a href="#">First</a></li>
    </c:if>

我建议您在Action中对要显示的页数进行一些计算。比如说你总是想要显示十个链接。在第1页上,您将显示页面1 ... 10,在第7页上,您将显示页面2 ... 12,依此类推。在操作中,您可以确定要显示的起始页和结束页。

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

在JSP页面上你可以做一个循环:

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

等等。

答案 1 :(得分:0)

Vincent Ramdhanie解决方案是正确的:谢谢你。 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>

您可以通过以下方式访问您的jsp:

http://localhost:8080/Project/list/?page=1

答案 2 :(得分:-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>`
相关问题