Code Bye

springmvc restful风格的分页问题

如题,本人要把page当做参数,怎么传到url中?url的匹配使用的restful风格的url。
controller中的代码
@RequestMapping(name=”/list”,method = RequestMethod.GET)
public String list(Integer page, Model model){
page=1;
int rowsPerPage=4;
int start=(page-1)*rowsPerPage+1;
int end=start+rowsPerPage;
int pageSize=0;
int totalRows=secKillService.getSecKillListTotal();
if(totalRows%rowsPerPage==0){
pageSize=totalRows/rowsPerPage;
}else{
pageSize=(totalRows/rowsPerPage)+1;
}
model.addAttribute(“page”,page);
model.addAttribute(“totalPages”,pageSize);
List<Seckill> list=secKillService.getSecKillList(page-1,rowsPerPage);
model.addAttribute(“list”,list);
return “list”;
}
页面中的分页代码已经写好,怎么使page传入进去呢?
页面部分的分页代码:
<div id=”pages”>
<a href=””>首页</a>
<c:choose>
<c:when test=”${page>1}”>
<a href=”/seckill/${page-1}”>上一页</a>
</c:when>
<c:otherwise><a href=”#”>上一页</a></c:otherwise>
</c:choose>
<c:forEach begin=”1″ end=”${totalPages}” var=”p”>
<c:choose>
<c:when test=”${page==p}”>
<a href=”/list/${page}” class=”current-page”>${p}</a>
</c:when>
<c:otherwise>
<a href=”/list/${page}” class=””>${p}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test=”${page<totalPages}”>
<a href=”/seckill/${page+1}”>下一页</a>
</c:when>
<c:otherwise><a href=”#”>下一页</a></c:otherwise>
</c:choose>
<a href=”/seckill/${totalPages}”>末页</a>
</div>
解决方案

35

@RequestMapping(name="/list/{page}",method = RequestMethod.GET)
    public String list(@PathVariable("page") Integer page, Model model){
        page=1;
        int rowsPerPage=4;
        int start=(page-1)*rowsPerPage+1;
        int end=start+rowsPerPage;
        int pageSize=0;
        int totalRows=secKillService.getSecKillListTotal();
        if(totalRows%rowsPerPage==0){
            pageSize=totalRows/rowsPerPage;
        }else{
            pageSize=(totalRows/rowsPerPage)+1;
        }
        model.addAttribute("page",page);
        model.addAttribute("totalPages",pageSize);
        List<Seckill> list=secKillService.getSecKillList(page-1,rowsPerPage);
        model.addAttribute("list",list);
        return "list";
    }

5

/list/${page}要写成这样

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明springmvc restful风格的分页问题