在 Spring MVC 和 Hibernate 应用程序中的 JSP 页面中实现分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14311816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implement pagination in JSP page in a Spring MVC and Hibernate application
提问by dukable
I am trying to implement pagination in my JSP page. I am using Spring MVC and Hibernate. The java code part is okay but I am having difficulties implementing it in my JSP page. I am using twitter bootstrap.
我正在尝试在我的 JSP 页面中实现分页。我正在使用 Spring MVC 和 Hibernate。java 代码部分没问题,但我在我的 JSP 页面中难以实现它。我正在使用推特引导程序。
Here is what I did until now:
这是我到目前为止所做的:
<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>
This is the related code in my controller:
这是我的控制器中的相关代码:
@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";
}
This is the related code in my Service class:
这是我的服务类中的相关代码:
public List<Employee> getEmployees(int page) {
return employeeDao.getEmployeeList(page);
}
This is the related code in my DAO class:
这是我的 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();
}
The page where I am displaying that table is list.jsp
我显示该表的页面是 list.jsp
Here are my assumptions of what I should do but dont know how (please correct me if I am taking the wrong way):
以下是我对我应该做什么但不知道如何做的假设(如果我采取了错误的方式,请纠正我):
Modify my link that point to list.jsp in my menu, to list.jsp?page=0, so everytime the user clicks on the link, he will arrive in the first page.
修改我的菜单中指向list.jsp的链接,改为list.jsp?page=0,这样用户每次点击链接都会到达第一页。
When the user clicks one of the button, I need to pass the page number to my controller so I can manage to return the right "employees" with my query.
当用户单击其中一个按钮时,我需要将页码传递给我的控制器,以便我可以设法通过我的查询返回正确的“员工”。
As you can see, the First and Previous buttons are deactivated as I am currently on the first page. So my question is, how should I handle the activation/deactivation of those buttons, as well as the Next and Last ones?
如您所见,First 和 Previous 按钮已停用,因为我目前位于第一页。所以我的问题是,我应该如何处理这些按钮以及 Next 和 Last 按钮的激活/停用?
Also, how to "refresh" the numbers on the list? For example, if the user is at the page 20, I won't display buttons from 1 to 19?
另外,如何“刷新”列表中的数字?例如,如果用户在第 20 页,我不会显示 1 到 19 的按钮?
回答by Vincent Ramdhanie
To answer one of your questions at least:
至少回答您的问题之一:
You can pass the page number and other parameters from the JSP to your controller with the RequestParam annotation like this:
您可以使用 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";
}
And your link will look something like this:
您的链接将如下所示:
list/?page=1
Pagination is a fairly complicated process but here are a few ideas. You can use JSTL on the JSP page to implement the logic. For instance:
分页是一个相当复杂的过程,但这里有一些想法。您可以在 JSP 页面上使用 JSTL 来实现逻辑。例如:
<c:if test="${page > 1}">
<li class="active"><a href="#">First</a></li>
</c:if>
I suggest that you do some calculations in the Action for the number of pages that you want to display. Say for instance that you always want to display ten links. On page 1, you will display pages 1...10, on page 7 you will display pages 2...12 and so on. In the action you can determine the starting page and the ending page to display.
我建议您在 Action 中对要显示的页数进行一些计算。比如说你总是想显示十个链接。在第 1 页上,您将显示第 1...10 页,在第 7 页上您将显示第 2...12 页,依此类推。在操作中,您可以确定要显示的起始页和结束页。
int startpage = page - 5 > 0?page - 5:1;
int endpage = startpage + 10;
On the JSP page you can do a loop maybe:
在 JSP 页面上,你可以做一个循环:
<c:forEach begin="${startpage}" end="${endpage}" var="p">
<a href="#">${p}</a>
</c:forEach>
and so on.
等等。
回答by Serch Durán
Vincent Ramdhanie solution is correct: thanks for that. Dukable: I used this code based on Vincent Ramdhanie's solution and it works really well: something like this should work in your code.
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";
}
And in your jsp:
在你的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>
You can access to your jsp on this way:
您可以通过以下方式访问您的jsp:
http://localhost:8080/Project/list/?page=1
回答by user13391764
`<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">«</a> </c:when> <c:otherwise> <a>«</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">»</a> </c:when> <c:otherwise> <a>»</a> </c:otherwise> </c:choose> </li> </c:if> </ul> </div>`
`<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">«</a> </c:when> <c:otherwise> <a>«</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">»</a> </c:when> <c:otherwise> <a>»</a> </c:otherwise> </c:choose> </li> </c:if> </ul> </div>`

