zoukankan      html  css  js  c++  java
  • jsp分页技术

    1、以下为分页类:

    import java.io.Serializable;  
    import java.util.List;  
     
    import org.apache.commons.lang.builder.ToStringBuilder;  
    import org.apache.commons.lang.builder.ToStringStyle;  
     
    public class ResultFilter<T> implements Serializable{  
     
        private static final long serialVersionUID = 5472321653620726832L;  
     
        private final static int DEFAULT_NAVIGATOR_SIZE = 5;  
     
        //当前页  
        private int currentPage = 1;  
        //每页显示数量  
        private int pageSize = 5;  
          
        //总条数  
        private int totalCount;  
     
        private boolean havaNextPage;  
     
        private boolean havePrePage;  
     
        private int navigatorSize;  
          
        //存放查询结果用的list  
        private List<T> items;  
          
        public ResultFilter(){  
              
        }  
          
        public ResultFilter(int totalCount, int pageSize, int currentPage) {  
            this(totalCount, pageSize, currentPage, DEFAULT_NAVIGATOR_SIZE);  
        }  
     
        public ResultFilter(int totalCount, int pageSize, int currentPage,  
                            int navigatorSize) {  
            this.totalCount = totalCount;  
            this.pageSize = pageSize;  
            this.currentPage = currentPage;  
            this.navigatorSize = navigatorSize;  
        }  
          
        public int getPageCount() {  
            int pageCount = 0;  
            if (pageSize != 0) {  
                pageCount = totalCount / pageSize;  
                if (totalCount % pageSize != 0)  
                    pageCount++;  
            }  
     
            return pageCount;  
        }  
     
        public int getCurrentPage() {  
            currentPage = currentPage < getPageCount() ? currentPage :  
                          getPageCount();  
            currentPage = currentPage < 1 ? 1 : currentPage;  
     
            return currentPage;  
        }  
     
        public int getPageSize() {  
            return pageSize;  
        }  
     
        public int getTotalCount() {  
            return totalCount;  
        }  
     
     
        public boolean isHaveNextPage() {  
            havaNextPage = false;  
            if ((getPageCount() > 1) && (getPageCount() > getCurrentPage()))  
                havaNextPage = true;  
            return havaNextPage;  
        }  
     
        public boolean isHavePrePage() {  
            havePrePage = false;  
            if ((getPageCount() > 1) && (currentPage > 1))  
                havePrePage = true;  
            return havePrePage;  
        }  
     
        private int getNavigatorIndex(boolean isBegin) {  
            int beginNavigatorIndex = getCurrentPage() - navigatorSize / 2;  
            int endNavigatorIndex = getCurrentPage() + navigatorSize / 2;  
            beginNavigatorIndex = beginNavigatorIndex < 1 ? 1 : beginNavigatorIndex;  
            endNavigatorIndex = endNavigatorIndex < getPageCount() ?  
                                endNavigatorIndex :  
                                getPageCount();  
            while ((endNavigatorIndex - beginNavigatorIndex) < navigatorSize &&  
                   (beginNavigatorIndex != 1 || endNavigatorIndex != getPageCount())) {  
                if (beginNavigatorIndex > 1)  
                    beginNavigatorIndex--;  
                else if (endNavigatorIndex < getPageCount())  
                    endNavigatorIndex++;  
            }  
     
            if(isBegin)  
                return beginNavigatorIndex;  
            else  
                return endNavigatorIndex;  
        }  
     
        public int getBeginNavigatorIndex() {  
            return getNavigatorIndex(true);  
        }  
     
        public int getEndNavigatorIndex() {  
            return getNavigatorIndex(false);  
        }  
     
        public List<T> getItems() {  
            return items;  
        }  
     
        public void setItems(List<T> items) {  
            this.items = items;  
        }  
          
        public void setCurrentPage(int currentPage) {  
            this.currentPage = currentPage;  
        }  
     
        public void setPageSize(int pageSize) {  
            this.pageSize = pageSize;  
        }  
     
        public void setTotalCount(int totalCount) {  
            this.totalCount = totalCount;  
        }  
     
        @Override  
        public String toString() {  
            return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);  
        }  
     
    }

    2、在spring的controller中调用分页类:

    @RequestMapping(value="/xxxList")    //xxxList.jsp对应的controller
        public ModelAndView xxxList(@ModelAttribute ResultFilter<xxx> rf){
            systemService.listServiceAll(rf);                                               //调用步骤3 ,分页获取xxx对象
            return new ModelAndView("路径/xxxList", "rf", rf);      //此处跳转到步骤5所表示的网页,并传递分页参数rf,其中“路径/xxxList”

    //表示对应的jsp网页,比如myBook/bookList表示myBook目录下的bookList.jsp
        }


    3、连操作数据库的service层   获取全部service
        @Transactional(readOnly = false)
        public void listServiceAll(ResultFilter<Services> rf){
            int count=getServiceList().size();
            rf.setTotalCount(count);  
            rf.setItems(serviceDao.getListForPage("from Services", (rf.getCurrentPage() - 1 )* rf.getPageSize(), rf.getPageSize()));

    //调用步骤4,分页查询数据库
        }

    4、

    // -------------- 分页QL Query --------------//
        
        @SuppressWarnings("unchecked")
        public List<T> getListForPage(String hql,int begin,int num){
             Query query = getSession().createQuery(hql);
            
            return query.setFirstResult(begin).setMaxResults(num).list();
            
        }

    5、路径/xxxList 对应的jsp网页(注意第四行)

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ include file="/WEB-INF/views/include/taglib.jsp"%>
    <%@ taglib uri="my-taglib" prefix="my"%>               <!--使用了自定义标签,该标签在步骤6中定义-->


    <!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>XXXList</title>
    </head>
    <body>

    <form>    
            <table id="contentTable" >        
            <tbody>
                <c:url value="/xxxDetail" var="url1"></c:url>
                <c:url value="/xxxList" var="url1"></c:url>    <!--该jsp对应的controller-->
                <tr><c:forEach items="${rf.items}" var="xxx">
                    <td valign="top" style="229px;oveflow:hidden;">

                        <table>
                           <tr><td ><a name="click to check the detail" href="${url1}?xxxId=${xxx.id}"><img src="${ctxStatic}/images/123.jpg"></img></a></td></tr>
                         <tr><td >${xxx.attribute1}</td></tr>
                         <tr><td >${xxx.attribute2}</td></tr>
                         </table>
                    </td>
                    </c:forEach>                                
                </tr>        
            </tbody>
        </table>
        <div style="padding-top:30px;"></div>
        <my:paging curr="${rf.currentPage}" total="${rf.pageCount}" size="${rf.pageSize}" href="${url2}"/>      <!--使用了自定义标签-->
        </form>
    </body>
    </html>

    6、自定义标签‘my’

    <?xml version="1.0" encoding="UTF-8"?>  
    <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
            version="2.0">  
     
        <description>My Tag Library</description>  
        <tlib-version>1.0</tlib-version>  
        <short-name>my</short-name>  
        <uri>my-taglib</uri>  
     
        <tag>  
            <description>split page</description>  
            <name>paging</name>  
            <tag-class>aaa.bbb.ccc.PagingTag</tag-class>     <!--用到了步骤7中的PagingTag类 ,aaa.bbb.ccc是PagingTag的路径-->
            <body-content>empty</body-content>  
            <attribute>  
                <description>base href</description>  
                <name>href</name>  
                <required>false</required>  
                <rtexprvalue>true</rtexprvalue>  
            </attribute>  
            <attribute>  
                <description>curr page</description>  
                <name>curr</name>  
                <required>true</required>  
                <rtexprvalue>true</rtexprvalue>  
            </attribute>  
            <attribute>  
                <description>page size</description>  
                <name>size</name>  
                <required>true</required>  
                <rtexprvalue>true</rtexprvalue>  
            </attribute>  
            <attribute>  
                <description>total page</description>  
                <name>total</name>  
                <required>true</required>  
                <rtexprvalue>true</rtexprvalue>  
            </attribute>  
            <attribute>  
                <description>curr parameter name</description>  
                <name>cparam</name>  
                <required>false</required>  
                <rtexprvalue>false</rtexprvalue>  
            </attribute>  
            <attribute>  
                <description>page size parameter name</description>  
                <name>sparam</name>  
                <required>false</required>  
                <rtexprvalue>false</rtexprvalue>  
            </attribute>  
            <dynamic-attributes>false</dynamic-attributes>  
        </tag>  
          
          
          
        <tag>  
            <description>front split page</description>  
            <name>frontPaging</name>  
            <tag-class>com.aspire.cms.demo.framework.tag.FrontPagingTag</tag-class>  
            <body-content>empty</body-content>  
            <attribute>  
                <description>base href</description>  
                <name>href</name>  
                <required>false</required>  
                <rtexprvalue>true</rtexprvalue>  
            </attribute>  
            <attribute>  
                <description>result filter</description>  
                <name>rf</name>  
                <required>true</required>  
                <rtexprvalue>true</rtexprvalue>  
            </attribute>  
            <dynamic-attributes>false</dynamic-attributes>  
        </tag>  
     
    </taglib>

    7、自定义标签类

    package aaa.bbb.ccc;

    import java.io.IOException;  

    import javax.servlet.jsp.JspException;  
    import javax.servlet.jsp.JspWriter;  
    import javax.servlet.jsp.tagext.SimpleTagSupport;  
     
    import org.apache.commons.lang.StringUtils;  
     
    public class PagingTag extends SimpleTagSupport {  
          
        private String href;  
          
        //当前页  
        private String cparam;  
        //每页条数  
        private String sparam;  
     
        private int curr;//当前页  
          
        private int size;//每页条数  
          
        private int total;//总页数  
          
        @Override  
        public void doTag() throws JspException, IOException {  
            JspWriter out = getJspContext().getOut();  
              
            if(StringUtils.isEmpty(cparam)) {  
                cparam = "currentPage";  
            }  
            if(StringUtils.isEmpty(sparam)) {  
                sparam = "pageSize";  
            }  
              
            if(!href.endsWith("?") && !href.endsWith("&")) {  
                if(href.indexOf("?") == -1) {  
                    href = href + "?";  
                } else {  
                    href = href + "&";  
                }  
            }  
              
            if (curr <= 0) {  
                curr = 1;  
            } else if (curr > total) {  
                curr = total;  
            }  
              
            out.append("<span>");  
            // 首页  
            if (curr == 1) {  
                out.append("首页");  
            } else {  
                href(out, href, 1, "首页");  
            }  
            out.append(" | ");  
            // 上一页  
            if (curr == 1) {  
                out.append("上一页");  
            } else {  
                href(out, href, curr - 1, "上一页");  
            }  
            out.append(" | ");  
            // 下一页  
            if (curr == total) {  
                out.append("下一页");  
            } else {  
                href(out, href, curr + 1, "下一页");  
            }  
            out.append(" | ");  
            // 末页  
            if (curr == total) {  
                out.append("末页");  
            } else {  
                href(out, href, total, "末页");  
            }  
            out.append("</span>");  
            out.append("<span>第");  
            out.append(curr + "/" + total);  
            out.append("页</span>");  
              
              
            super.doTag();  
        }  
          
        private void href(JspWriter out, String href, int curr, String title) throws IOException {  
            out.append("<a href="").append(href).append(cparam).append("=").append("" + curr).append("&").append(sparam).append("=").append("" + size).append("">").append(title).append("</a>");  
        }  
     
        public int getCurr() {  
            return curr;  
        }  
     
        public void setCurr(int curr) {  
            this.curr = curr;  
        }  
     
        public int getTotal() {  
            return total;  
        }  
     
        public void setTotal(int total) {  
            this.total = total;  
        }  
     
        public String getHref() {  
            return href;  
        }  
     
        public void setHref(String href) {  
            this.href = href;  
        }  
     
        public String getCparam() {  
            return cparam;  
        }  
     
        public void setCparam(String cparam) {  
            this.cparam = cparam;  
        }  
     
        public String getSparam() {  
            return sparam;  
        }  
     
        public void setSparam(String sparam) {  
            this.sparam = sparam;  
        }  
     
        public int getSize() {  
            return size;  
        }  
     
        public void setSize(int size) {  
            this.size = size;  
        }  
     
    }

  • 相关阅读:
    form表单提交中文乱码(前台中文到JAVA后台乱码)问题及解决
    JSON工具类库: alibaba/fastjson 使用记录
    前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 "
    AJAX与后台交互传参的两种方式
    小米平板充不上电解决(非硬件问题+系统升级原因)
    对于异常的理解
    solr如何进行搜索实战,关键字全库搜索
    solr5.5.4 添加mysql数据,实现同步更新
    solr5.5.4 tomcat8部署
    log4j通过配置文件配置即,即可完成系统报错想向指定邮箱发送提醒消息,网上的很多方法都又问题
  • 原文地址:https://www.cnblogs.com/zhaochunhua/p/4065254.html
Copyright © 2011-2022 走看看