opencms分页实现

自己写了一个通用的分页页面,需要有分页的页面引入这个分页页面,并传入几个参数就可以了.

分页代码(JSP代码),样式也写在本页中:
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<style>
.news_page{
	margin-top:10px;
	text-align:right;
	}
.news_page a{
	border:#ccc 1px solid;
	padding:2px 5px;
	margin:0 2px;
	}
.news_page a:hover{
	background-color:#FFF8ED;
	}
.news_page .current{
	background-color:#EBEAEA;}
</style>
<%!
private static final int getParam(ServletRequest request, String name, int defval){
    String param = request.getParameter(name);
    if(param==null){
    	param = String.valueOf(request.getAttribute(name));
    }
    int value = defval;
    if (param != null) {
		try {
			value = Integer.parseInt(param);
		}catch (NumberFormatException ignore) {
		}
    }
    return value;
}
%>
<% 	
	int showNum = 7;//单数,保证中间位置
	int showOffset = (showNum - 1)/2;//当前页数向左右两边的便宜量
	int total = getParam(request,"total",0);
	if(total < 0)
		total = 0;
	int pageIndex = getParam(request,"pageIndex",1);
	if(pageIndex < 1)
		pageIndex = 1;
	int pageSize = getParam(request,"pageSize",10);
	if(pageSize < 1)
		pageSize = 10;
	int pageTotal = 0;
	if(total%pageSize == 0)
		pageTotal = total/pageSize;
	else
		pageTotal = total/pageSize + 1;
	
	int pageBegin = 0;
	int pageEnd = 0;
	if(pageTotal <= showNum){
		pageBegin = 1;
		pageEnd = pageTotal;
	}else{
		int offsetBegin = 0;
		int begin = pageIndex - showOffset;
		if(begin > 0)
			pageBegin = begin;
		else{
			pageBegin = 1;
			offsetBegin = 1 - begin;
		}
		
		int offsetEnd = 0;
		int end = pageIndex + showOffset;
		if(end <= pageTotal)
			pageEnd = end;
		else{
			pageEnd = pageTotal;
			offsetEnd = end - pageTotal;
		}
		pageBegin = pageBegin - offsetEnd > 0 ? pageBegin - offsetEnd : 1;
		pageEnd = pageEnd + offsetBegin <= pageTotal ? pageEnd + offsetBegin : pageTotal;
			 
	}
	
	 %>
<div class="news_page">  
  <%if(pageIndex > 1){ %>
  <a href="<cms:link>${param.pageUrl}?pageIndex=<%=pageIndex-1 %>&pageSize=<%=pageSize %></cms:link>">上一页</a>
  <%if(pageBegin > 1){ %><a href="<cms:link>${param.pageUrl}?pageIndex=1&pageSize=<%=pageSize %></cms:link>">1</a><%} %>
  <%} %>  
  <%for(int i = pageBegin; i<=pageEnd;i++){ %> 
  <%if(i == pageIndex) {%><a href="#this" class="current"><%=i %></a><%}%>
  <%if(i != pageIndex) {%><a href="<cms:link>${param.pageUrl}?pageIndex=<%=i %>&pageSize=<%=pageSize %></cms:link>"><%=i %></a><%}%>
  <%} %>  
  <%if(pageIndex < pageTotal){ %>
  <%if(pageEnd < pageTotal){ %><a href="<cms:link>${param.pageUrl}?pageIndex=<%=pageTotal %>&pageSize=<%=pageSize %></cms:link>"><%=pageTotal %></a><%} %>
  <a href="<cms:link>${param.pageUrl}?pageIndex=<%=pageIndex+1 %>&pageSize=<%=pageSize %></cms:link>">下一页</a>
  <%} %>
</div>


页面调用一共3部分:
1.获得链接带过来的参数
2.把参数传到cms:contentload标签中,读取对应位置和条数的列表
3.引入分页页面,把参数传到分页页面中去

代码1.获得链接带过来的参数,使用JSTL标签,主要是两个参数.
pageSize:每页的条数
pageIndex:当前的页数
<c:set var="pageSize" value="15" />
<c:if test="${not empty param.pageSize}"><c:set var="pageSize" value="${param.pageSize}" /></c:if>
<c:set var="pageIndex" value="1" />
<c:if test="${not empty param.pageSize}"><c:set var="pageIndex" value="${param.pageIndex}" /></c:if>


代码2.把参数传到cms:contentload标签中,读取对应位置和条数的列表.
要通过<cms:contentinfo var="info"/>来获得总条数,以便下面传给分页页面
<cms:contentload collector="allInFolderPriorityDateAsc" param="/exhibition/prospects/prospects_%(number).html|gmprospect" editable="true" pageIndex="${pageIndex}" pageSize="${pageSize}">
<cms:contentinfo var="info"/>
  ..................
  ...............
</cms:contentload>


代码3.引入分页页面,把参数传到分页页面中去
total:记录总条数
pageSize:每页的条数
pageIndex:当前的页数
pageUrl:分页的链接
<cms:include file="%(link.weak:/system/modules/xxx/elements/page.jsp)" >
		<cms:param name="total">${info.resultSize }</cms:param>
		<cms:param name="pageIndex">${pageIndex}</cms:param>
		<cms:param name="pageSize">${pageSize}</cms:param>
		<cms:param name="pageUrl">/exhibition/prospects/index.html</cms:param>
	</cms:include>

你可能感兴趣的:(html,cms,jsp)