Springmvc完成分页的功能

时间:2022-12-20 20:56:29

最近使用springmvc完成分页功能,遇到了一些问题,百度了一下发现都是一样的。因此贴出自己的解决思路。

一:控制层中

     @Value("#{configProperties['schoolPageSize']}")
      private String schoolPageSize;

      @RequestMapping(value="/schoolList")
    public ModelAndView schoolList(School school,String page){
        //每页显示的条数
        int pageSize=Integer.valueOf(schoolPageSize).intValue();
        List<School> schoolList=schoolService.findSchoolList(school);
        ModelAndView modelAndView = new ModelAndView();
        //查到的学校总数
        int SchoolNum=schoolList.size();
        //查到的总用户数
         modelAndView.addObject("SchoolNum",SchoolNum);
        int pageTimes;
         if(SchoolNum%pageSize == 0)
            {
                pageTimes =SchoolNum/pageSize;
            }else
            {
                pageTimes = SchoolNum/pageSize + 1;
            }
            modelAndView.addObject("pageTimes", pageTimes);
            //页面初始的时候page没有值
            if(null == page)
            {
                page = "1";
            }
            //每页开始的第几条记录            
            int startRow = (Integer.parseInt(page)-1) * pageSize;
            List<School> schools= this.schoolService.getschoolByPage(startRow, pageSize);
            modelAndView.addObject("currentPage", Integer.parseInt(page));
            modelAndView.addObject("schools", schools);
            modelAndView.setViewName("school/schoolList");
            return modelAndView;
    }

备注:  @Value("#{configProperties['schoolPageSize']}")需要完成配置

springmvc.xml中的配置

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:value.properties</value>
        </list>
    </property>
</bean>

value.properties的配置

schoolPageSize=5(注意没有“;”)

页面的代码

<div class="pagging">
        <div class="left">共${SchoolNum}条记录</div>
        <div class="right">
        <c:if test="${currentPage == 1}">
            <span class="disabled"><< 前一页</span>        
        </c:if>
        <c:if test="${currentPage != 1}">
            <a href="school/schoolList.action?page=${currentPage-1}"><< 前一页</a>
        </c:if>
        <c:if test="${currentPage == 1}">
        <span class="current">1</span>
        </c:if>
        <c:if test="${currentPage != 1}">
        <a href="school/schoolList.action?page=1">1</a>
        </c:if>
        <%
                session.getAttribute("pageTimes");
                for(int i=1;i<2;i++)
                {
                    request.setAttribute("page", i+1);
        %>            
        <c:if test="${currentPage == page}">
            <span class="current"><%=i+1%></span>       
        </c:if>
        <c:if test="${currentPage != page}">
             <a href="school/schoolList.action?page=<%=i+1%>"><%=i+1%></a>
        </c:if>
        <%} %>
        
        <c:if test="${currentPage == pageTimes}">
            <span class="disabled">后一页 >></span>        
        </c:if>
        <c:if test="${currentPage != pageTimes}">
        <a href="school/schoolList.action?page=${currentPage+1}">后一页 >></a>
        </c:if>
        </div>

 

相关文章