我正在为我的表单使用spring分页框架。我从这里测试分页框架: http://www.developer.com/java/web/article.php/10935_3830886_3/A-Pagination-Technique-Using-Spring.htm。我的表单的网址显示如下页码:
第1页:http://...stuff../mySite/paging/paging/0 第2页:http://...stuff../paging.do?action=list&p=1 第3页:http://...stuff../paging.do?action=list&p=2 等
分页不一致并导致我的代码出现问题(请注意区别btwn first pg和2nd pg url)并保持我的url干净,我更喜欢将页码存储为隐藏的jsp参数。可能吗?你有什么建议吗?感谢。
我的控制器:
@Controller
@RequestMapping(value="/paging")
@ApplicationComponent(component="Paging")
public class PaginationController
{
private final Log logger = LogFactory.getLog(getClass());
@Autowired
private ItemDao itemDao;
@RequestMapping(value="/paging/{p}")
public ModelAndView list(@PathVariable("p") int page) throws Exception
{
ModelAndView mav = new ModelAndView("paging/paging");
System.out.println("In the paging controller");
// get data in a list from jsp
List searchResults = itemDao.getAllItems();
// initialize PagedListHolder with the list,
PagedListHolder pagedListHolder = new PagedListHolder(searchResults);
pagedListHolder.setPage(page);
int pageSize = 5;
pagedListHolder.setPageSize(pageSize);
mav.addObject("pagedListHolder", pagedListHolder);
return mav;
}
}
这是JSP:
<jsp:useBean id="pagedListHolder" scope="request" type="org.springframework.beans.support.PagedListHolder"/>
<%-- // create link for pages, "~" will be replaced with the proper page number --%>
<c:url value="/paging.do" var="pagedLink">
<c:param name="action" value="list"/>
<c:param name="p" value="~"/>
</c:url>
<tg:paging pagedListHolder="${pagedListHolder}" pagedLink="${pagedLink}"/>
<%-- // show only current page data --%>
<table width="200px" border="1">
<tr>
<th width="20px">No.</th>
<th>Print Items from pagedListHolder list</th>
</tr>
<c:forEach items="${pagedListHolder.pageList}" var="item">
<tr>
<td>${item.key}</td>
<td style="color:blue;font-weight:bold;text-align:right">${item.data}</td>
</tr>
</c:forEach>