6

我正在尝试在我的 JSP 页面中实现分页。我正在使用 Spring MVC 和 Hibernate。java 代码部分没问题,但我在我的 JSP 页面中实现它时遇到了困难。我正在使用推特引导程序。

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

<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,这样用户每次点击链接时,都会到达第一页。

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

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

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

4

3 回答 3

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>

等等。

于 2013-01-14T03:11:19.487 回答
0

文森特·拉姆丹尼的解决方案是正确的:谢谢。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
于 2016-01-04T21:43:12.990 回答
-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>`
于 2020-04-23T14:57:41.290 回答