springmvc restful风格的分页问题

「已注销」 2016-10-07 04:24:09
如题,我要把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>
...全文
544 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
有全的代码没,接口和service的
「已注销」 2016-10-08
  • 打赏
  • 举报
回复
引用 3 楼 abcdefghiijklmnopqrs 的回复:
/list/${page}要写成这样
前台页面访问的时候,进不到通过http://localhost:8080/seckill/,但是通过http://localhost:8080/seckill/list就访问不到,list后面接参数的时候,报404错误。。。。这个是为什么呢?web.xml中的配置: <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <!--配置dispatchservlet--> <servlet> <servlet-name>seckill-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--spring mvc需要加载的配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>seckill-dispatcher</servlet-name> <!--默认匹配所有请求--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
  • 打赏
  • 举报
回复
/list/${page}要写成这样
「已注销」 2016-10-08
  • 打赏
  • 举报
回复
引用 1 楼 abcdefghiijklmnopqrs 的回复:
@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";
    }
那我的页面上请求的时候,写成/list/{page}这样写对吗??
「已注销」 2016-10-08
  • 打赏
  • 举报
回复
引用 3 楼 abcdefghiijklmnopqrs 的回复:
/list/${page}要写成这样
我已经知道怎么写了,谢谢!
  • 打赏
  • 举报
回复
@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";
    }

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧