简单分页插件
当在做项目时,很多地方需要分页,而又不想引用很繁琐复杂的分页插件,那么可以自己写一个。。
分页jsp:page_plugins.jsp
<%@ page contentType="text/html; charset=GBK"%> <%@page import="com.opensymphony.oscache.util.StringUtil"%> <%@page import="server.helper.PageListHelper"%> <%@page import="java.util.Map"%> <%@page import="java.util.Set"%> <%@page import="java.util.Enumeration"%> <% String str_iPageSize = (String)request.getAttribute(PageListHelper.SIGN_IPAGESIZE); // 每页显示条数多少 String str_iPageNo = (String)request.getAttribute(PageListHelper.SIGN_IPAGENO); // 当前页 String str_iCountAll = (String)request.getAttribute(PageListHelper.SIGN_ICOUNTALL); // 总记录数 String goto_page = (String)request.getAttribute("javax.servlet.forward.request_uri"); // 列表页面Url int iPageNo = Integer.parseInt(StringUtil.isEmpty(str_iPageNo)?"1":str_iPageNo); // 当前页 iPageNo int iPageSize = Integer.parseInt(StringUtil.isEmpty(str_iPageSize)?PageListHelper.DEFAULT_PAGESIZE+"":str_iPageSize ); //页面显示条数 int iCountAll = Integer.parseInt(StringUtil.isEmpty(str_iCountAll)?"0":str_iCountAll); //总记录 int o_pageall = iCountAll/iPageSize; // 总页数 if (iCountAll%iPageSize > 0) { o_pageall = o_pageall + 1; } //如果当前页大于总页数 当总页数为0 if(iPageNo > o_pageall) { iPageNo = 0; } %> <script type="text/javascript"> function showsize(){ var iPageSize=document.pageRecordForm.iPageSize.value; if(iPageSize.length==0||checknumber(iPageSize)==false){ alert("输入条数有误!"); return; } if(iPageSize<1||iPageSize>parseInt(document.pageRecordForm.iCountAll.value)){ alert("输入条数不在范围1-"+document.pageRecordForm.iCountAll.value+"内!"); return; } document.pageRecordForm.iPageNo.value=1; document.pageRecordForm.target="_self"; document.pageRecordForm.submit(); } // 第一页 function hrefFirstPageOnclick(){ document.pageRecordForm.iPageNo.value=1; document.pageRecordForm.target="_self"; document.pageRecordForm.submit(); } // 上一页 function hrefPreviousPageOnclick(){ document.pageRecordForm.iPageNo.value=parseInt(document.pageRecordForm.iPageNo.value)-1; document.pageRecordForm.target="_self"; document.pageRecordForm.submit(); } // 下一页 function hrefNextPageOnclick(){ document.pageRecordForm.iPageNo.value=parseInt(document.pageRecordForm.iPageNo.value)+1; document.pageRecordForm.target="_self"; document.pageRecordForm.submit(); } // 最后一页 function hrefLastPageOnclick(){ document.pageRecordForm.iPageNo.value=document.pageRecordForm.iPageAll.value; document.pageRecordForm.target="_self"; document.pageRecordForm.submit(); } // 转到 function goto(){ var iPageNo=document.pageRecordForm.iPageNo.value; if(document.pageRecordForm.iPageAll.value < 1) { alert("对不起,暂时还没有符合条件的记录,不能进行翻页."); return; } if(iPageNo.length==0||checknumber(iPageNo)==false){ alert("输入页码有误!"); return; } if(iPageNo<1||iPageNo>parseInt(document.pageRecordForm.iPageAll.value)){ alert("输入页码不在范围1-"+document.pageRecordForm.iPageAll.value+"内!"); return; } document.pageRecordForm.target="_self"; document.pageRecordForm.submit(); } function checknumber(strValue){ if(strValue.search(/^[0-9]+[.]?[0-9]*$/)==-1) return false; return true; } </script> <form name="pageRecordForm" id="pageRecordForm" method="post" onsubmit="return false;" action="<%=goto_page %>"> <% //所有查询条件 for (Enumeration<String> keyNames = request.getAttributeNames(); keyNames.hasMoreElements();) { String key = keyNames.nextElement(); if (PageListHelper.notHiddenKey(key)) { continue; } %> <input type="hidden" name="<%=key %>" value="<%=request.getAttribute(key) %>"/> <% } %> <table border="0" cellpadding="0" cellspacing="0" width="90%" bordercolorlight="#999999" bordercolordark="#ffffff" align="center"> <tr> <td colspan="20" align="center"> <br> 总记录数:<%=iCountAll%> <input type="hidden" name="iCountAll" value="<%=iCountAll%>"> 每页显示<input type="text" size="3" maxlength="3" name="iPageSize" value="<%=iPageSize%>" style="text-align:center;" onkeydown="if(event.keyCode==13) return showsize(this.form);">条记录 <br> 第<%=iPageNo%>页/共<%=o_pageall%>页 <input type="hidden" name="iPageAll" value="<%=o_pageall%>"> <% if(iPageNo==1 || iPageNo == 0){ %> 首页 <% }else{ %> <a href="#" name="hrefFirstPage" title="点击进入首页" onclick="hrefFirstPageOnclick();" class="black">首页</a> <% } if(iPageNo==1 || iPageNo == 0){ %> 上一页 <% }else{ %> <a href="#" name="hrefPreviousPage" title="点击进入上一页" onclick="hrefPreviousPageOnclick();" class="black">上一页</a> <% } if(iPageNo==o_pageall){ %> 下一页 <% }else{ %> <a href="#" name="hrefNextPage" title="点击进入下一页" onclick="hrefNextPageOnclick();" class="black">下一页</a> <% } if(iPageNo==o_pageall){ %> 尾页 <% }else{ %> <a href="#" name="hrefLastPage" title="点击进入尾页" onclick="hrefLastPageOnclick();" class="black">尾页</a> <% } %> 跳转至<input type="text" size="3" maxlength="3" name="iPageNo" value="<%=iPageNo%>" style="text-align:center;" onkeydown="if(event.keyCode==13) return goto();">页 </td> </tr> </table>
分页工具类:PageListHelper.java:
package server.helper; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import com.opensymphony.oscache.util.StringUtil; /**分页工具类 * * @create_date :2013-8-16 下午01:16:46 * */ public class PageListHelper { public static final String SIGN_IPAGESIZE = "iPageSize"; // 每页显示条数多少 public static final String SIGN_IPAGENO = "iPageNo"; // 当前页 public static final String SIGN_ICOUNTALL = "iCountAll"; // 总记录数 public static final String PAGE_START = "startIndex"; // public static final String PAGE_END = "endIndex"; // public static final String DEFAULT_PAGESIZE = "50"; // 默认显示条数 public static final String VINDICATE_PAGESIZE = "20"; //每页显示条个数 /**分页前,调用该页面,获取基本分页参数 * * @create_date :2013-8-16 下午01:22:40 * @param request * @return */ public static Map<String, Object> initPageParameter(HttpServletRequest request, Map<String, Object> parameterMap, Integer iCountAll) { String str_iPageSize = request.getParameter(PageListHelper.SIGN_IPAGESIZE); // 每页显示条数多少 String str_iPageNo = request.getParameter(PageListHelper.SIGN_IPAGENO); // 当前页 str_iPageSize = StringUtil.isEmpty(str_iPageSize)?PageListHelper.DEFAULT_PAGESIZE+"":str_iPageSize; str_iPageNo = StringUtil.isEmpty(str_iPageNo)?"1":str_iPageNo; Integer startIndex = (Integer.parseInt(str_iPageNo)-1) * Integer.parseInt(str_iPageSize);// 开始显示 Integer endIndex = startIndex + Integer.parseInt(str_iPageSize);// 结束条数 // 在dao中调用 parameterMap.put(PageListHelper.SIGN_IPAGESIZE, str_iPageSize); parameterMap.put(PageListHelper.SIGN_IPAGENO, str_iPageNo); parameterMap.put(PageListHelper.PAGE_START, startIndex); // 开始 当前页*页面显示条数 parameterMap.put(PageListHelper.PAGE_END, endIndex); // 结束 // 设置参数给分页插件接收 parameterMap.put(PageListHelper.SIGN_ICOUNTALL, iCountAll+""); // 设置所有parameterMap中的参数入request中 Set<String> keySet = parameterMap.keySet(); if (keySet != null && keySet.size() > 0) { for (String key : keySet) { request.setAttribute(key, parameterMap.get(key)); } } return parameterMap; } /**在分页插件中,参数map中key包含以下标识将不显示隐藏域 * * @create_date :2013-8-20 上午08:30:02 * @param key * @return */ public static boolean notHiddenKey(String key) { if (StringUtil.isEmpty(key)) { return false; } String[] PAGE_SIGNLIST = {SIGN_IPAGESIZE, SIGN_IPAGENO, SIGN_ICOUNTALL, PAGE_START,PAGE_END}; for (String keyString : PAGE_SIGNLIST){ if (key.equalsIgnoreCase(keyString)) { return true; } } return false; } }
引用调用:
每页显示记录条数设置
整个系统默认显示条数配置在类PageListHelper. DEFAULT_PAGESIZE = 50; // 默认显示条数
使用方法:
页面调用
在jsp列表添加:
<jsp:include flush="true" page="/page_plugins.jsp"/>
注意:不要添加在form表单内。
控制类调用
在调用dao查询集合列表时:这样写:
if ("common_dictionary_list.jsp".equals(tabid)) // 列表页面根据分类ID查询 { Map parameterMap = new HashMap(); String common_type = request.getParameter("common_type"); // 设置查询参数 parameterMap.put("common_type", common_type); parameterMap.put("tabid", "common_dictionary_list.jsp"); // 获取总记录数 int iCountAll = commonDictionaryDAO.getCommonDictionaryInfoListCount(parameterMap); // 设置分页参数 initPageParameter(request, 查询参数, 总记录数, 列表页跳转链接) PageListHelper.initPageParameter(request, parameterMap, iCountAll); // 根据分页参数查询集合 List<CommonDictionaryInfo> commonDictionaryInfoList = commonDictionaryDAO.getCommonDictionaryInfoList(parameterMap); request.setAttribute("commonDictionaryInfoList", commonDictionaryInfoList); // 用户ID request.getRequestDispatcher(tabid).forward(request, response); return ; }
DAO调用
/**查询_公用数据字典_集合 * @param userId * @return * @throws ServerException */ public List<CommonDictionaryInfo> getCommonDictionaryInfoList(Map param) throws ServerException { try { List<CommonDictionaryInfo> retList = sqlMapper.queryForList("common_dictionary_list", param); return retList; } catch (SQLException e) { throw new ServerException(e); } } /**查询_公用数据字典_集合_总记录 * * @param userId * @return * @throws ServerException */ public Integer getCommonDictionaryInfoListCount(Map param) throws ServerException { try { Integer count = (Integer) sqlMapper.queryForObject("common_dictionary_list_count", param); return count; } catch (SQLException e) { throw new ServerException(e); } }
SQL调用
<sql id="sql_select_common_dictionary"> select common_type_name, common_type, common_name, common_value, column_bak, remark, created_by, date_created, updated_by, date_updated from sibrms_common_dictionary <dynamic prepend="where"> <isNotEmpty prepend="and" property="common_value"> common_value = #common_value# </isNotEmpty> <isNotEmpty prepend="and" property="common_name"> common_name = #common_name# </isNotEmpty> <isNotEmpty prepend="and" property="common_type"> common_type = #common_type# </isNotEmpty> </dynamic> </sql> <!-- 公用数据字典表 start --> <!-- 公用数据字典表-查询 --> <select id="common_dictionary_list" parameterClass="java.util.Map" resultClass="server.report.model.CommonDictionaryInfo"> select * from (select sbci.*, rownum rn from ( <include refid="sql_select_common_dictionary" /> order by nlssort(common_type_name,'NLS_SORT=SCHINESE_PINYIN_M'),common_value ) sbci <isNotEmpty property="endIndex" > <![CDATA[ where rownum <= #endIndex# ]]> </isNotEmpty> ) <isNotEmpty property="startIndex" > <![CDATA[ where rn > #startIndex# ]]> </isNotEmpty> </select> <!-- 公用数据字典表-统计个数 --> <select id="common_dictionary_list_count" parameterClass="java.util.Map" resultClass="java.lang.Integer"> select count(0) as count from ( <include refid="sql_select_common_dictionary" /> ) </select>