自定义标签:分页标签

一、标签类源代码

public class PageNavigationBarTag extends BaseBodyTagSupport {
	private String pageBean;
	private String actionName;
	
	public String getPageBean() {
		return pageBean;
	}

	public void setPageBean(String pageBean) {
		this.pageBean = pageBean;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}
	
	public int doEndTag() throws JspException {
		if(CommonUtil.isEmpty(pageBean)){
			throw new JspException("pageBean must not be null");
		}
		
		Page page = null;
		Map params = null;
		try{
			page = (Page)getValueStack().findValue(pageBean);
			params = RequestUtil.getParameterMap(getRequest(), "");
		}catch(Exception e){
			throw new JspException(e);
		}
		
		Map dataModel = new HashMap();
		dataModel.put("page", page);
		dataModel.put("params", params);
		dataModel.put("actionName", CommonUtil.trim(actionName));
		
		try{
			String ret = render(pageContext.getServletContext(), dataModel, "taglib/PageNavigationBar.ftl");
			pageContext.getOut().println(ret);
			
		}catch(Exception ex){
			throw new JspException(ex);
		}
		
		return EVAL_PAGE;
	}
}

 

二、Page类源代码

public class Page {
	private int pageIndex = 0; //当前页号
	private int pageSize = 15; //每页记录数
	private int pageCount = 0; //总页数
	private int rowCount = 0; //总记录数
	private List result = null; //页数据

	public Page(){
		
	}
	
	public Page(List result, int rowCount, int pageIndex, int pageSize){
		this.result = result;
		this.rowCount = rowCount;
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		
		updatePageCount();
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public int getRowCount() {
		return rowCount;
	}

	private void updatePageCount() {
		if(rowCount <= 0){
			rowCount = 0;
		}else{
			int count = rowCount / pageSize;
			if(rowCount % pageSize > 0){
				count++;
			}
			pageCount = count;
		}
	}

	public List getResult() {
		return result;
	}
}

 

三、公共方法源代码

public static Map getParameterMap(HttpServletRequest request, String prefix){
	Map p = new HashMap();
	
	Map paramsMap = request.getParameterMap();
	for(Iterator it=paramsMap.keySet().iterator();it.hasNext();){
		String key = (String)it.next();
		String value = request.getParameter(key);
		if(CommonUtil.isEmpty(value)) continue;
		
		if(CommonUtil.isEmpty(prefix)){
			p.put(key, value);
		}else{
			if(key.startsWith(prefix)){
				p.put(key, value);
			}
		}
	}
	
	return p;
}

 

四、分页方法源代码

public Page findPageByCriteria(final DetachedCriteria dc, final int pageIndex, final int pageSize) {
	return (Page)getHibernateTemplate().execute(new HibernateCallback(){
		public Object doInHibernate(Session session) throws HibernateException, SQLException {
			Criteria c = dc.getExecutableCriteria(session);
			int firstResult = (pageIndex - 1) * pageSize;
			
			//记录总数
			int rowCount = ((Integer)c.setProjection(Projections.rowCount()).uniqueResult()).intValue();
			
			//复位
			c.setProjection(null);
			c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); //将结果类型转换为原实体类型
			
			//记录集
			List result = c.setFirstResult(firstResult).setMaxResults(pageSize).list();
			
			//页对象
			Page page = new Page(result, rowCount, pageIndex, pageSize);
			return page;
		}
	}, true);
}

 

五、FTL模板源代码

<script language="javascript">
	function chnageCurrentPage(_pageIndex){
		var frm = document.all["frmPageNavigationBar"];
		var objPageIndex = frm.pageIndex;
		if(objPageIndex==null){
			objPageIndex = document.createElement("<input type='hidden' name='pageIndex' value='" + _pageIndex + "'/>");
			frm.appendChild(objPageIndex);
		}else{
			objPageIndex.value = _pageIndex;
		}
		frm.submit();
	}
</script>

<form name="frmPageNavigationBar" method="post" action="${actionName}">
	<#if params?has_content><!-- 所有请求参数包含在页导航条的Form中 -->
		<#list params?keys as key>
			<input type="hidden" name="${key}" value="${params[key]}" />
		</#list>
	</#if>
	
	<table border="0" cellspacing="0" cellpadding="0" class="width9x" align="center">
		<tr>
			<td>第 ${page.pageIndex?string("####")} 页/共 ${page.pageCount?string("####")} 页。找到 ${page.rowCount?string("####")} 条记录。</td>
			<td> </td>
			<td align="right" nowrap width="150">
				<#if page.pageIndex gt 1>
					<a href="#" onclick="chnageCurrentPage(1);"><img src="images/pager/page-first.gif" border="0" alt="首页"></a>
					<a href="#" onclick="chnageCurrentPage(${page.pageIndex-1});"><img src="images/pager/page-prev.gif" border="0" alt="上一页"></a>
				<#else>
					<img src="images/pager/page-first-disabled.gif" border="0" alt="首页">
					<img src="images/pager/page-prev-disabled.gif" border="0" alt="上一页">
				</#if>
				
				<select name="pageIndexList" onchange="chnageCurrentPage(this.options[this.selectedIndex].value)";>
					<#list 1..page.pageCount as x>
						<option value="${x}">${x}</option>
					</#list>
				</select> 
				
				<#if page.pageIndex lt page.pageCount>
					<a href="#" onclick="chnageCurrentPage(${page.pageIndex+1});"><img src="images/pager/page-next.gif" border="0" alt="下一页"></a>
					<a href="#" onclick="chnageCurrentPage(${page.pageCount});"><img src="images/pager/page-last.gif" border="0" alt="末页"></a>
				<#else>
					<img src="images/pager/page-next-disabled.gif" border="0" alt="下一页">
					<img src="images/pager/page-last-disabled.gif" border="0" alt="末页">
				</#if>
			</td>
		</tr>
	</table>
</form>

 六、tld配置

<tag>
	<name>pageNavigationBar</name>
	<tag-class>com.cjm.web.taglib.PageNavigationBarTag</tag-class>
	<body-content>JSP</body-content>
	<attribute>
		<name>pageBean</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
	<attribute>
		<name>actionName</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
</tag>

  

七、业务方法源代码

DetachedCriteria dc = DetachedCriteria.forClass(User.class);
if(user != null){
	CriteriaUtil.eq(dc, "username", user.getUsername());
	CriteriaUtil.like(dc, "fullname", user.getFullname());
	CriteriaUtil.eq(dc, "mobilephone", user.getMobilephone());
	CriteriaUtil.eq(dc, "enabledTemp", user.getEnabledTemp());
	if(user.getOrg() != null) CriteriaUtil.eq(dc, "org.orgId", user.getOrg().getOrgId());
}
dc.addOrder(Order.asc("username"));

Page page = getBaseService().findPageByCriteria(dc, getPageIndex(), 15);
getValueStack().set("page", page);

 

八、JSP页面源代码

<s:iterator value="page.result" id="user" status="st">
	<tr <s:if test="#st.odd">class="bg1"</s:if><s:else>class="bg2"</s:else>>
		<td align="center"><input type="checkbox" name="chk_o_<s:property value='#st.count'/>" value="<s:property value='#user.username'/>"></td>
		<td align="left"> <s:property value="#user.username"/></td>
		<td align="left"> <s:property value="#user.fullname"/></td>
		<td align="left"> <s:property value="#user.org.name"/></td>
		<td align="left"> <s:property value="#user.mobilephone"/></td>
		<td> <s:property value="#user.gender"/></td>
		<td> <s:property value="#user.enabledTemp"/></td>
		<td align="center"><a href="#" onclick="winOpen('userView.action?userId=<s:property value="#user.username"/>'),void(0);">查看</a></td>
	</tr>
</s:iterator>

<cjm:pageNavigationBar pageBean="page" actionName="userList.action"/>

 

你可能感兴趣的:(JavaScript,C++,c,jsp,C#)