Java spring + struts2 + ibatis +velocity中的分页的实现

1. 先在common层或domain层创建一个PageUtil类。

public class PageUtil {
    private int allPageSize;// 数据库总条数(外部传入)
    private int pageNum;// 当前第几页(外部传入)
    private int pageShowSize = 5;// 每页多少条数据
    private int allPageNum;// 一共多少页
    private int selectStart;// 数据库的起始搜索位置
    private int indexStart;// 前台页面的起始索引
    private int indexEnd;// 前台页面的结束索引
    private List<LogInfo> pageInfo;// 从数据库查询出来的数据

    public PageUtil() {
    }

    public PageUtil(int allPageSize, int pageNum,int pageShowSize) {
        this.allPageSize = allPageSize;
        this.pageNum = pageNum;
        this.pageShowSize = pageShowSize;
        if (allPageSize % pageShowSize == 0) {
            this.allPageNum = this.allPageSize / this.pageShowSize;
        } else {
            this.allPageNum = this.allPageSize / this.pageShowSize + 1;
        }
        this.selectStart = (this.pageNum - 1) * this.pageShowSize;
        if (this.allPageNum < 3) {
            this.indexStart = 1;
            this.indexEnd = this.allPageNum;
        } else {
            this.indexStart = this.pageNum - 2;// 前台页面的起始页是当前页数-2  要和前台的第一页控制一样第一页控制大于多少,这里就得减去多少
            this.indexEnd = this.pageNum + 2;// 前台页面的结束页是当前页数+2
            if (this.indexStart < 1) {
                this.indexStart = 1;
                this.indexEnd = 5;
            } else if (this.indexEnd > this.allPageNum) {
                this.indexStart = this.allPageNum - 5;
                this.indexEnd = this.allPageNum;
            }
        }
    }

    public int getAllPageSize() {
        return allPageSize;
    }

    public void setAllPageSize(int allPageSize) {
        this.allPageSize = allPageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageShowSize() {
        return pageShowSize;
    }

    public void setPageShowSize(int pageShowSize) {
        this.pageShowSize = pageShowSize;
    }

    public int getAllPageNum() {
        return allPageNum;
    }

    public void setAllPageNum(int allPageNum) {
        this.allPageNum = allPageNum;
    }

    public int getSelectStart() {
        return selectStart;
    }

    public void setSelectStart(int selectStart) {
        this.selectStart = selectStart;
    }

    public int getIndexStart() {
        return indexStart;
    }

    public void setIndexStart(int indexStart) {
        this.indexStart = indexStart;
    }

    public int getIndexEnd() {
        return indexEnd;
    }

    public void setIndexEnd(int indexEnd) {
        this.indexEnd = indexEnd;
    }

    public List<LogInfo> getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(List<LogInfo> pageInfo) {
        this.pageInfo = pageInfo;
    }
}

2. 对日志类进行分页显示。在domain层建立LogInfo类。

public class LogInfo {
    private int id;
    private int type;
    private String userName;
    private String accessTime;
    private String userIP;
    private String url;
    private int stayTime;
    private boolean loginStatus;

    public LogInfo() {
    }

    public LogInfo(String userName, int type, String accessTime, String userIP, String url, int stayTime, boolean loginStatus) {
        this.userName = userName == null ? "" : userName;
        this.type = type;
        this.accessTime = accessTime == null ? "" : accessTime;
        this.userIP = userIP == null ? "" : userIP;
        this.url = url == null ? "" : url;
        this.stayTime = stayTime == 0 ? 0 : stayTime;
        this.loginStatus = loginStatus;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAccessTime() {
        return accessTime;
    }

    public void setAccessTime(String accessTime) {
        this.accessTime = accessTime;
    }

    public String getUserIP() {
        return userIP;
    }

    public void setUserIP(String userIP) {
        this.userIP = userIP;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getStayTime() {
        return stayTime;
    }

    public void setStayTime(int stayTime) {
        this.stayTime = stayTime;
    }

    public boolean isLoginStatus() {
        return loginStatus;
    }

    public void setLoginStatus(boolean loginStatus) {
        this.loginStatus = loginStatus;
    }
}

3. 在dao层建立LogInfoDao,LogInfoDaoImpl。

public interface LogInfoDao {
    public List<LogInfo> getLogInfoByPage(int startRow, int offset);
}
public class LogInfoDaoImpl extends BaseDao implements LogInfoDao {
        @Override
    public List<LogInfo> getLogInfoByPage(int startRow, int offset) {
        Map<String, Integer> paraMap = new HashMap<String, Integer>();
        paraMap.put("startRow", startRow);
        paraMap.put("endRow", offset);
        return queryForList("LogInfoDAO.getLogInfoByPage", paraMap);
    }
}

编写sqlmap-config.xml和logInfo_sqlMap.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig  PUBLIC "-//iBATIS.com//DTD SQL Map
Config 2.0/" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

    <settings cacheModelsEnabled="false" enhancementEnabled="true" lazyLoadingEnabled="false" errorTracingEnabled="true"
              maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="true"/>
        <sqlMap resource="sqlmap/ipList_sqlMap.xml"/>
</sqlMapConfig>

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "sql-map-2.dtd">
<!--http://ibatis.apache.org/dtd/-->
<sqlMap namespace="LogInfoDAO">
    <typeAlias alias="LogInfo" type="com.jd.ordersroll.domian.LogInfo"/>
     <select id="getLogInfoByPage" parameterClass="java.util.Map" resultClass="LogInfo">
        select * from loginfo limit #startRow#,#endRow#;
    </select>
</sqlMap>

 

 4.编写service层。LogInfoService 和LoginfoServiceImpl.

public interface LogInfoService {
 public PageUtil getLogInfoByPage(int page, int pageSize);
}
public class LogInfoServiceImpl implements LogInfoService {
    private LogInfoDao logInfoDao;

        @Override
    public PageUtil getLogInfoByPage(int pageNum, int pageSize) {
        int startRow = (pageNum - 1) * pageSize;
        int offset = pageSize;
        int allPageSize = logInfoDao.getAllLogInfo().size();
        PageUtil page = new PageUtil(allPageSize, pageNum, pageSize);
        page.setPageInfo(logInfoDao.getLogInfoByPage(startRow,offset));
        return page;
    }

    public void setLogInfoDao(LogInfoDao logInfoDao) {
        this.logInfoDao = logInfoDao;
    }
}

5.在web层action的调用

public class ManagerAction extends BaseAction {
    private final static Log log = LogFactory.getLog(ShowOrdersAction.class);
    private static final long serialVersionUID = 3800841343751060753L;
    private LogInfoService logInfoService;
    private HttpServletRequest request;

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String manager() {
        return "manager";
    }

    public String fail() {
        return "noPermission";
    }

    public String lookLogInfo() {
        String pageNum = request.getParameter("pageNum");
        int num = (null == pageNum || "0".equals(pageNum)) ? 1 : Integer.parseInt(pageNum);
        PageUtil page = logInfoService.getLogInfoByPage(num, 10);
        request.setAttribute("page", page);
        return "infoList";
    }

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    public void setLogInfoService(LogInfoService logInfoService) {
        this.logInfoService = logInfoService;
    }
}


在Sring-config中的相关项目运行环境的配置。

在vm文件中展示。

    <font color="#333"> 当前第</font><font color="red">[$page.pageNum]</font><font color="#333">页</font>
    #if($page.pageNum > 1)                 ##显示上一页处理
#set($pageNum=$page.pageNum - 1)     ##必须在这里先做减法运算后将值赋给变量,如果下面直接pageNum=$pageNum - 1输出的是$pageNum - 1 运算符两边空格勿忘!
<a title="上一页" href="lookLogInfo.action?pageNum=$pageNum&parentId=$!parentId">[上一页]</a>
    #end
    #if($page.pageNum >= 4)                  ##第一页控制  1...3 4 5
<a href="lookLogInfo.action?pageNum=1&parentId=$!parentId">1</a><span>...</span>
    #end
    #foreach($i in [$page.indexStart..$page.indexEnd])  ##这个类似于 <!-- <c:forEach var="pagenum" begin="${page.foreachbegin}" end="${page.foreachend}"> -->
#if($i == $page.pageNum)
        <font color="red">$i</font>
    #else
        <a href="lookLogInfo.action?pageNum=$i&parentId=$!parentId">$i</a>
    #end
    #end
    #if($page.pageNum < $page.allPageNum - 2)         ##最后一页的处理 17 18 19...20
<span>…</span><a href="lookLogInfo.action?pageNum=$page.allPageNum&parentId=$!parentId"
                 rel=start>$page.allPageNum</a>
    #end
    #if($page.pageNum != $page.allPageNum)            ##显示下一页处理
#set($pageNum=$page.pageNum + 1)
        <a title="上一页" href="lookLogInfo.action?pageNum=$pageNum&parentId=$!parentId">[下一页]</a>
    #end
    <font color="#333">总共<font color="red">$page.allPageNum<font color="#333">页<font color="#333">
        <input id="pageCount" type="text" size="1"><input type="button" onclick="doPage()" value="转到"/>
</div>
<script type="text/javascript">    function doPage() {
        var obj = document.getElementById("pageCount");
        var pagenum = obj.value;
        var allpagesize = ${page.allPageNum};
        if (!pagenum.match("\\d+")) {
            alert("请输入数字");
            return;
        } else if (pagenum < 1) {
            pagenum = 1;
            window.location.href = "lookLogInfo.action?pageNum=1&parentId=$!parentId";
        } else if (pagenum > allpagesize) {
            pagenum = allpagesize;
            window.location.href = "lookLogInfo.action?pageNum=$page.allPageNum&parentId=$!parentId";
        } else {
            window.location.href = "lookLogInfo.action?parentId=$!parentId&pageNum=" + pagenum;
        }
    }
</script>  

完毕。

posted on 2014-07-23 16:42  Thinking in study  阅读(395)  评论(0)    收藏  举报