我的习惯是直接贴代码,语言表达能力不好,懒得写。但是我贴代码会贴全,这样就不会让其他人跟我一样,有时候找不到使用的方法,还有去百度或者调整半天。真心觉得给部分代码如果有问题,很烦人,有时候很难找到问题。
这里struts 中action的调用就不详细讲解了,不懂先去了解,再回来看。
注意:火狐浏览器点击分页按钮请求一次,会调用两次action,其他浏览器不会,找了很久,没有解决方法,如果有人知道,欢迎指点。
后台action类:
import java.util.List;
import java.util.Map;
@SuppressWarnings("serial")
public class UserListAction extends ActionSupport {
private UserService adminUserService;
private List<TbUser> userList = null;
private Page page = new Page();
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
log.info("查找用户列表");
// 每页显示条数
page.setEveryPage(3);
// 获取数据
Map<String, Object> map = adminUserService.findUserByPage(page);
userList = map.get("list") == null ? null : (List<TbUser>) map.get("list");
page = map.get("page") == null ? null : (Page) map.get("page");
return "success";
}
public UserService getAdminUserService() {
return adminUserService;
}
public void setAdminUserService(UserService adminUserService) {
this.adminUserService = adminUserService;
}
public List<TbUser> getUserList() {
return userList;
}
public void setUserList(List<TbUser> userList) {
this.userList = userList;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}
page类
public class Page {
/** 是否有上一页 */
private boolean hasPrePage;
/** 是否有下一页 */
private boolean hasNextPage;
/** 每页的数量 */
private int everyPage;
/** 总页数 */
private int totalPage;
/** 当前页 */
private int currentPage;
/** 起始点 */
private int beginIndex;
/** 总记录数 */
private int totalCount;
/** 开始的页数 */
private int startPage;
/** 结束的页数 */
private int endPage;
/**
* @return totalCount
*/
public int getTotalCount() {
return totalCount;
}
/**
* @param totalCount
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public Page() {
}
public Page(int everyPage) {
this.everyPage = everyPage;
}
public Page(boolean hasPrePage, boolean hasNextPage, int everyPage, int totalPage, int currentPage, int beginIndex, int totalCount, int startPage, int endPage) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.totalCount = totalCount;
this.startPage = startPage;
this.endPage = endPage;
}
public int getBeginIndex() {
return beginIndex;
}
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getEveryPage() {
return everyPage;
}
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
public boolean getHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean getHasPrePage() {
return hasPrePage;
}
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getStartPage() {
return startPage;
}
public void setStartPage(int startPage) {
this.startPage = startPage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
}
page工具类
public class PageUtil {
// 静态方法
public static Page createPage(Page page, int totalRecords) {
return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
}
// 创建Page对象
public static Page createPage(int everyPage, int currentPage, int totalRecords) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
int pageScope[] = getShowScope(currentPage, 10, totalPage);
return new Page(hasPrePage, hasNextPage, everyPage, totalPage, currentPage, beginIndex, totalRecords, pageScope[0], pageScope[1]);
}
// 返回每页显示数目
private static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
// 返回当前页
private static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
// 返回开始的索引
private static int getBeginIndex(int everyPage, int currentPage) {
return (currentPage - 1) * everyPage;
}
// 返回总页数
private static int getTotalPage(int everyPage, int totalRecords) {
int totalPage = 0;
if (totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1;
return totalPage;
}
// 返回是否有上一页。false:没有;true:有
private static boolean hasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
// 返回是否有下一页。false:没有;true:有
private static boolean hasNextPage(int currentPage, int totalPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
private static int[] getShowScope(int currentPage, int showNum, int totalPage) {
// 初始化开始页
int startPage = currentPage <= 6 ? 1 : currentPage - 5;
// 初始化结束页
int endPage = showNum + startPage - 1;
if (endPage > totalPage) {
endPage = totalPage;
}
startPage = endPage - showNum + 1;
if (startPage <= 0) {
startPage = 1;
}
return new int[] { startPage, endPage };
}
}
显示数据的Jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>用户列表</title>
</head>
<body>
<ul>
<s:iterator id="user" value="userList" status="statu">
<li>
<s:property value="#statu.count" />
<s:property value="#user.userId" />
<s:property value="#user.userName" />
</li>
</s:iterator>
</ul>
<div>
<!--页码-->
<!-- 分页组件 -->
<%@ include file="page.jsp"%>
<!--页码 End-->
</div>
</body>
</html>
通用page页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!-- 分页组件 -->
<s:if test="page.hasPrePage || page.hasNextPage">
<div class="turnpage">
<!-- 首页 -->
<s:url id="goto" includeParams="none">
<s:param name="page.currentPage" value="1" />
</s:url>
<s:a href="%{goto}" title="首页">
<span>首页</span>
</s:a>
<!-- end首页 -->
<!-- 上一页 -->
<s:if test="page.hasPrePage">
<s:url id="goto">
<s:param name="page.currentPage" value="%{page.currentPage - 1}" />
</s:url>
<s:a href="%{goto}" title="上一页" id="prePage" >
<span>上一页</span>
</s:a>
</s:if>
<s:else>
<span>上一页</span>
</s:else>
<!-- end上一页 -->
<%-- 页面显示 --%>
<s:if test="page.totalPage <= 10">
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first" value="1" />
<s:param name="last" value="page.totalPage" />
</s:bean>
<s:iterator value="#counter">
<s:url id="goto">
<s:param name="page.currentPage" value="top" />
</s:url>
<s:if test="top == page.currentPage">
<a class="cur"><span><s:property /></span></a>
</s:if>
<s:else>
<s:a href="%{goto}">
<span><s:property /></span>
</s:a>
</s:else>
</s:iterator>
</s:if>
<s:else>
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first" value="%{page.currentPage - 4 >= 1 ? (page.currentPage > page.totalPage - 5 ? ( page.totalPage - 9 ) :( page.currentPage - 4 )) : 1}" />
<s:param name="last" value="%{page.currentPage + 5 <= page.totalPage ? (page.currentPage < 5 ? ( page.currentPage + (10 - page.currentPage) ) : (page.currentPage + 5)) : page.totalPage}" />
</s:bean>
<s:iterator value="#counter">
<s:url id="goto">
<s:param name="page.currentPage" value="top" />
</s:url>
<s:if test="top == page.currentPage">
<a class="cur"><span><s:property /></span></a>
</s:if>
<s:else>
<s:a href="%{goto}">
<span><s:property /></span>
</s:a>
</s:else>
</s:iterator>
<!--如果还有更多,显示... -->
<s:if test="page.totalPage != #counter.last">
<span>...</span>
</s:if>
<s:url id="goto">
<s:param name="page.currentPage" value="page.totalPage" />
</s:url>
<s:a href="%{goto}" title="page.currentPage">
<span><s:property value="page.totalPage"/></span>
</s:a>
</s:else>
<!-- 下一页 -->
<s:if test="page.hasNextPage">
<s:url id="goto">
<s:param name="page.currentPage" value="%{page.currentPage + 1}" />
</s:url>
<s:a href="%{goto}" title="下一页">
<span>下一页</span>
</s:a>
</s:if>
<s:else>
<span>下一页</span>
</s:else>
<!-- end下一页 -->
<%-- 尾页 --%>
<s:url id="goto">
<s:param name="page.currentPage" value="page.totalPage" />
</s:url>
<s:a href="%{goto}" title="尾页">
<span>尾页</span>
</s:a>
<%-- end尾页 --%>
<!-- 信息
当前页: <s:property value="page.currentPage" /> / <s:property value="page.totalPage" />
每 页: <s:property value="page.everyPage" /> 条
共 计: <s:property value="page.totalCount" /> 条
end信息 -->
<%--如果总页数少于100页,显示一个select选择跳转框 --%>
<s:if test="page.totalPage <= 100">
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first" value="1" />
<s:param name="last" value="page.totalPage" />
</s:bean>
<select οnchange="window.location.href = this.options[this.selectedIndex].value;" size="1">
<s:iterator value="#counter">
<s:url id="goto">
<s:param name="page.currentPage" value="top" />
</s:url>
<s:if test="top == page.currentPage">
<option value="<s:property value="%{goto}"/>" selected="selected">
第 <s:property /> 页
</option>
</s:if>
<s:else>
<option value="<s:property value="%{goto}"/>">
第 <s:property /> 页
</option>
</s:else>
</s:iterator>
</select>
</s:if>
</div>
</s:if>