Struts 2 假分页

主要思想,将要分面的数据全部取出。放入bigList,再根据curentPage对大集合切割成分页要显示的数据smallList。

通过action请求在分面的jsp页面中输出smallList。调用分页控制pageController.jsp。

 

PageController.java

package articles.page;

import java.util.ArrayList;

//负责传入一个大的集合,根据页号返回所需要的数据
//计算总页数 =(总记录数+每页条数-1)/每页条数
//第N页显示第几条记录(记录从0开始算)? (N-1)*每页条数=<序号< N*每页条数

public class PageController {
	@SuppressWarnings("unchecked")
	private ArrayList bigList; // 大的集合,外界传入
	private int curentPageIndex = 1; // 当前页号,外界传入
	private int countPerpage = 5; // 每页条数,外界可以设定
	@SuppressWarnings("unchecked")
	private ArrayList smallList; // 小的集合,返回
	private int pageCount; // 页数
	private int recordCount; // 记录条数
	private int prePageIndex; // 上一页序号
	private int nextPageIndex; // 下一页序号
	private boolean firstPage; // 是否第一页
	private boolean lastPage; // 是否最后一页

	public void setCurentPage(String curentPage) {
		if (curentPage == null) {
			curentPage = "1";
		}
		int currentPageIndex = Integer.parseInt(curentPage);
		this.setCurentPageIndex(currentPageIndex);
	}

	@SuppressWarnings("unchecked")
	public void setCurentPageIndex(int curentPageIndex) {
		// 每当页数改变,都会调用这个函数,筛选代码可以写在这里
		this.curentPageIndex = curentPageIndex;

		// 上一页,下一页确定
		prePageIndex = curentPageIndex - 1;
		nextPageIndex = curentPageIndex + 1;
		// 是否第一页,最后一页
		if (curentPageIndex == 1) {
			firstPage = true;
		} else {
			firstPage = false;
		}
		if (curentPageIndex == pageCount || pageCount == 0) {
			lastPage = true;
		} else {
			lastPage = false;
		}
		// 筛选工作
		smallList = new ArrayList();
		for (int i = (curentPageIndex - 1) * countPerpage; i < curentPageIndex
				* countPerpage
				&& i < recordCount; i++) {
			smallList.add(bigList.get(i));
		}
	}

	@SuppressWarnings("unchecked")
	public void setBigList(ArrayList bigList) {
		this.bigList = bigList;
		// 计算条数
		recordCount = bigList.size();
		// 计算总页数 =(总记录数+每页条数-1)/每页条数
		pageCount = (recordCount + countPerpage - 1) / countPerpage;
	}

	public int getCountPerpage() {
		return countPerpage;
	}

	@SuppressWarnings("unchecked")
	public ArrayList getBigList() {
		return bigList;
	}

	public int getCurentPageIndex() {
		return curentPageIndex;
	}

	public void setCountPerpage(int countPerpage) {
		this.countPerpage = countPerpage;
	}

	@SuppressWarnings("unchecked")
	public ArrayList getSmallList() {
		return smallList;
	}

	@SuppressWarnings("unchecked")
	public void setSmallList(ArrayList smallList) {
		this.smallList = smallList;
	}

	public int getPageCount() {
		return pageCount;
	}

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

	public int getRecordCount() {
		return recordCount;
	}

	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
	}

	public int getPrePageIndex() {
		return prePageIndex;
	}

	public void setPrePageIndex(int prePageIndex) {
		this.prePageIndex = prePageIndex;
	}

	public int getNextPageIndex() {
		return nextPageIndex;
	}

	public void setNextPageIndex(int nextPageIndex) {
		this.nextPageIndex = nextPageIndex;
	}

	public boolean isFirstPage() {
		return firstPage;
	}

	public void setFirstPage(boolean firstPage) {
		this.firstPage = firstPage;
	}

	public boolean isLastPage() {
		return lastPage;
	}

	public void setLastPage(boolean lastPage) {
		this.lastPage = lastPage;
	}

}

 

*.action

public class ArticleAction extends ActionSupport {
	private Article article;
	private ArticleFacade articleFacade;
	private List listOperate;
	private PageController pc;

	public ArticleAction() {
		articleFacade = new ArticleFacadeImpl();
	}

	public List getListOperate() {
		return listOperate;
	}

	public PageController getPc() {
		return pc;
	}

	public void setPc(PageController pc) {
		this.pc = pc;
	}

	
public String operateList() {
		operatelogFacade = new OperatelogFacadeImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User user1 = (User) session.getAttribute("user");
		
		pc = (PageController) request.getAttribute("pc");
		if (pc == null) {
			pc = new PageController();
			List listOperateBig = operatelogFacade.selectOperatelog(user1
					.getUserID());
			pc.setBigList((ArrayList) listOperateBig);
			request.setAttribute("pc", pc);
		}
		
		pc.setCurentPage(request.getParameter("PageIndex"));
		listOperate = pc.getSmallList();

		if ("1".equals(user1.getSystemRole())) {
			return "authorOperate";
		} else if ("2".equals(user1.getSystemRole())) {
			return "auditOperate";
		}
		return "error";
	}
}

 

struts.xml

		<action name="authorOperatelog"
			class="articles.action.ArticleAction" method="operateList">
			<result name="authorOperate">/author/author_operatelog.jsp</result>
			<interceptor-ref name="sessionInterceptor"></interceptor-ref>
		</action>

 author_operatelog.jsp

<%@ page language="java" contentType="text/html;charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>刊物文章管理系统</title>
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	</head>
	<body bgcolor="#ceeef4">
		<div align="center">
			<p>

				<br>
				<font size="5" color="#000099"><b>操作日志</b> </font>
			</p>
			<table cellspacing="0" cellpadding="5" border="1" width="90%">
				<tr bgcolor="#bebef4">
					<td width="20%">
						<b><font size="4">操作类型</font> </b>
					</td>
					<td width="50%">
						<b><font size="4">操作对象</font> </b>
					</td>
					<td width="30%">
						<b><font size="4">操作时间</font> </b>
					</td>
				</tr>
				<s:iterator value="listOperate" status="stat">
					<tr <s:if test="#stat.even"> bgcolor="#D7EBFF" </s:if>>
						<td>
							<s:property value="operateType" />
						</td>
						<td>
							<s:property value="destination" />
						</td>
						<td>
							<s:property value="operateDatetime" />
						</td>
					</tr>
				</s:iterator>
			</table>
			<s:set name="myAction" value="%{'authorOperatelog'}" scope="action" />
			<s:include value="../pageController.jsp" />
		</div>
	</body>
</html>

 

 

pageController.jsp

<%@ page language="java" contentType="text/html;charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:if test="pc.pageCount == 0">
</s:if>
<s:else>
	<p align="center">
		<a
			href='<s:url action="%{#attr.myAction}"> 
			<s:param name="PageIndex" value="1"/></s:url>'>[首页]</a>

		<s:if test="pc.firstPage == false">
			<a
				href='<s:url action="%{#attr.myAction}"> 
			<s:param name="PageIndex" value="pc.prePageIndex"/></s:url>'>[上一页]</a>
		</s:if>
		
		<s:if test="pc.lastPage == false">
			<a
				href='<s:url action="%{#attr.myAction}"> 
			<s:param name="PageIndex" value="pc.nextPageIndex"/></s:url>'>[下一页]</a>
		</s:if>

		<a
			href='<s:url action="%{#attr.myAction}"> 
			<s:param name="PageIndex" value="pc.pageCount"/></s:url>'>[尾页]</a>
		第[
		<s:property value="pc.curentPageIndex" />
		]页 共[
		<s:property value="pc.pageCount" />
		]页
	</p>
</s:else>

 

 

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