JavaEE学习总结(5) - 通用分页的实现,可搜索查询。

将分页信息封装成一个vo,存储分页的信息及记录信息

package com.zx.pojo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 封装分页信息
 * @author zhangxin
 *
 */
public class PageInfo {
    //每页记录数,总记录数,总页数,当前页号,url,搜索字段及内容,查询后的数据
    private int recordSize=5;
    private int recordCount;
    private int pageCount;
    private int currentPage=1;
    private String action;
    private String keyWord="";
    private Map<String,String> map;
    private List list;
    //获得总页数
    public int getPageCount(){
        return (recordCount-1)/recordSize+1;
    }

    public int getRecordSize() {
        return recordSize;
    }
    public void setRecordSize(int recordSize) {
        this.recordSize = recordSize;
    }
    public int getRecordCount() {
        return recordCount;
    }
    public void setRecordCount(int recordCount) {
        this.recordCount = recordCount;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public String getAction() {
        return action;
    }
    public void setAction(String action) {
        this.action = action;
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public PageInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "PageInfo [recordSize=" + recordSize + ", recordCount=" + recordCount + ", pageCount=" + pageCount
                + ", currentPage=" + currentPage + ", action=" + action + ", keyWord=" + keyWord + ", list=" + list
                + "]";
    }

    public PageInfo(HttpServletRequest request){
        Map<String,String> map=new HashMap<String, String>();
        //保证不查询的情况下不出现空指针异常
        map.put("","");
        this.setMap(map);
        this.setAction(request.getRequestURL().toString());
        request.setAttribute("pageInfo", this);
        String c=request.getParameter("currentPage");
        if(c!=null){
            this.setCurrentPage(Integer.parseInt(c));
        }   
    }
}

控制器接收到查询请求时,做如下处理 ,将request传给PageInfo构造当前页信息实例,并添加搜索的字段和关键字

@Autowired
    private CompanyService companyService;
    @RequestMapping("/companyList.do")
    public String companyList(HttpServletRequest request,Model model){
        PageInfo pageInfo=new PageInfo(request);
        String keyWord=request.getParameter("keyWord");
        if(keyWord==null){
            keyWord="";
        }
        Map<String,String> map=new HashMap<>();
        map.put("companyName", keyWord);
        pageInfo.setMap(map);
        companyService.getCompanyByPageWord(pageInfo);
        model.addAttribute(pageInfo);
        return "companyList";
    }

表示层展示,搜索的关键字要提交

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"
    src="${pageContext.request.contextPath }/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">

    $(function(){
        $("#company_page_btn").click(function(){
        var page=$("#company_search_txt").val();
        var p=/^[1-9][0-9]*$/;
        if(p.test(page)){
            if(page > 0 && page <= ${pageInfo.pageCount}){
                window.open("${pageContext.request.contextPath }/seed/companyList.do?currentPage="+page+"&keyWord=${pageInfo.map.companyName}","_self");
            }else{
                alert("跳轉頁非法!")
                $("#company_search_txt").val("");
            }
        }else{
            alert("跳轉頁非法!")
        }
        })
    });

</script>
</head>
<body>
    <table width="95%" border="0" align="center" cellpadding="0"
        cellspacing="0">
        <tr>
            <td height="6"><img
                src="${pageContext.request.contextPath }/images/spacer.gif"
                width="1" height="1" /></td>
        </tr>
        <tr>
            <td height="33"><table width="100%" border="0" align="center"
                    cellpadding="0" cellspacing="0" class="right-font08">
                    <tr>
                        <td width="50%"><span class="right-text09">${pageInfo.pageCount}</span>
                            页 | 第 <span class="right-text09">${pageInfo.currentPage} </span></td>
                        <td width="49%" align="right">[ <c:forEach
                                items="${pageInfo.map }" var="entry">
                                <a href="${pageInfo.action}?currentPage=1&keyWord=${entry.value}"
                                    class="right-font08">首页</a>
                            </c:forEach> | <c:if test="${pageInfo.currentPage-1>=1}">
                                <c:forEach items="${pageInfo.map }" var="entry">
                                    <a href="${pageInfo.action}?currentPage=${pageInfo.currentPage-1}&keyWord=${entry.value}"
                                        class="right-font08">上一页</a> | 
                                                    </c:forEach>
                            </c:if> <c:set var="a" value="${pageInfo.pageCount}"></c:set> <c:if
                                test="${pageInfo.currentPage+1<=a}">
                                <c:forEach items="${pageInfo.map }" var="entry">
                                    <a href="${pageInfo.action}?currentPage=${pageInfo.currentPage+1}&keyWord=${entry.value}"
                                        class="right-font08">下一页</a>
                                </c:forEach>| 
               </c:if> <c:forEach items="${pageInfo.map }" var="entry">
                                <a href="${pageInfo.action}?currentPage=${pageInfo.pageCount}&keyWord=${entry.value}"
                                    class="right-font08">末页</a>
                            </c:forEach>] 转至:
                        </td>
                        <td width="1%"><table width="20" border="0" cellspacing="0"
                                cellpadding="0">
                                <tr>
                                    <td width="1%"><input name="textfield3" type="text"
                                        class="right-textfield03" size="1" id="company_search_txt" /></td>
                                    <td width="87%"><input name="Submit23222" type="button"
                                        class="right-button06" id="company_page_btn" value=" " /></td>
                                </tr>
                            </table></td>
                    </tr>
                </table></td>
        </tr>
    </table>
</body>
</html>

多个字段应该也可以,map中继续添加字段,想想效率不高,代码处理也麻烦,不实用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值