ssmフレームワーク実装ページング(limit)

14487 ワード

pojo層新規クラスPageBean
package com.qut.pojo;

import java.util.List;

public class PageBean{
    private int pageNumber; //   
    private int pageCount; //   
    private Integer pageIndex; //   
    private int pageSize; //      
    private Listlist; //      
    public int getPageNumber() {
        return pageNumber;
    }
    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }
    public int getPageCount() {
        return pageCount;
    }
    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
    public Integer getPageIndex() {
        return pageIndex;
    }
    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
}

エンティティークラスDaoレイヤに2つのメソッドを追加
    //        
    public List showlist(int index);

    //      
    public int pagecount();

xml 2つの文を追加
  <select id="countIndex" parameterType="com.qut.pojo.Position" resultType="int">
        select count(*) from Position where 1=1
        <if test='p_name!=null and p_name!=""'>
            and p_name = #{p_name}
        if>
        <if test='p_no!=0'>
            and p_no = #{p_no}
        if>
        <if test='p_type!=null and p_type!=""'>
            and p_type = #{p_type}
        if>
    select>

    <select id="showlist" parameterType="int" resultType="com.qut.pojo.Position">
        select *
        from Position limit #{index},10
    select>

サービス層
    public List showlist(int index);

    public int pagecount();

ServiceImplレイヤ
        public List showlist(int index) {
            // TODO Auto-generated method stub
            return poMapper.showlist(index);
        }

        public int pagecount() {
            // TODO Auto-generated method stub
            return poMapper.pagecount();
        }

Controllerレイヤ
    @RequestMapping("/page")
    public ModelAndView showlist(HttpServletRequest request){

            ModelAndView view = new ModelAndView(); 

            Integer pageIndex = 1;
            int pageSize = 4;
            PageBean pageUtil = new PageBean();
            List list = new ArrayList();
            if(request.getParameter("pageIndex")!=null){
                pageIndex = Integer.parseInt((String)request.getParameter("pageIndex"));
            }
            pageUtil.setPageIndex(pageIndex);
            int number = ps.pagecount();
            pageUtil.setPageNumber(number);
            pageUtil.setPageSize(pageSize);
            if((int)pageUtil.getPageNumber() % (int)pageUtil.getPageSize() ==0){
                pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
                        .getPageNumber() / pageUtil.getPageSize())));
            }else{
                pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
                        .getPageNumber() / pageUtil.getPageSize()))+1);
            }

            int index = (pageIndex-1)*pageSize;
            list = ps.showlist(index);
            pageUtil.setList(list);

            view.addObject("pageUtil",pageUtil);
            view.setViewName("show");
            return view;
        }

jsp遍歴コード
<c:forEach items="${pageUtil.list}" var="c" begin="0" end="4">

    <span class="STYLE1">${c.p_name}span>
    <span class="STYLE1">${c.p_type}span>
    <span class="STYLE1">${c.p_num}span> 
c:forEach>

jspは合計何ページを表示して、今何ページ目で、トップページ、前のページ、次のページと次のページのコード
      <font size="2">  ${pageUtil.pageCount}  font> <font size="2">   
            ${pageUtil.pageIndex}  font> <a href="page.action?pageIndex=1&doaction=woaiyuxue">  a>  
        <c:choose>  
            <c:when test="${pageUtil.pageIndex - 1 > 0}">  
                <a href="page.action?pageIndex=${pageUtil.pageIndex - 1}&doaction=woaiyuxue">   a>  
            c:when>  

        c:choose>  
        <c:choose>  
            <c:when test="${pageUtil.pageCount==0}">  
                <a href="page.action?pageIndex=${pageUtil.pageIndex }&doaction=woaiyuxue">   a>  
            c:when>  
            <c:when test="${pageUtil.pageIndex + 1 <= pageUtil.pageCount}">  
                <a href="page.action?pageIndex=${pageUtil.pageIndex+1}&doaction=woaiyuxue">   a>  
            c:when>  

        c:choose>  
        <c:choose>  
            <c:when test="${page.pageCount==0}">  
                <a href="page.action?pageIndex=${pageUtil.pageIndex}&doaction=woaiyuxue">  a>  
            c:when>  
            <c:otherwise>  
                <a href="page.action?pageIndex=${pageUtil.pageCount}&doaction=woaiyuxue">  a>  
            c:otherwise>  
        c:choose>