/**
* 分页对象
*/
public final class Paging {
private static final String t_2 = "%s LIMIT %s, %s";
/** 获取分页SQL语句 */
public String paginal(String sql) {
int s = (index - 1) * size;
if (s < 0)
s = 0;
return format(t_2, sql, s, size);
}
private int index = 1; // 页索引
private int size = 15; // 页大小
private int totalRecordsNumber = -1; // 总记录
public int getPageCount() { // 总页数
return (int) Math.ceil((double) totalRecordsNumber / size);
}
public int getPageIndex() {
return totalRecordsNumber > 0 ? index : 0;
}
public void setPageIndex(int index) {
this.index = index > 1 ? index : 1;
}
public int getPageSize() {
return size;
}
public void setPageSize(int size) {
this.size = size > 0 ? size : this.size;
}
public int getTotalRecordsNumber() {
return totalRecordsNumber;
}
public void setTotalRecordsNumber(int totalRecordsNumber) {
this.totalRecordsNumber = totalRecordsNumber;
}
}
/**
* Struct2 Action
*
* /user.jhtml
*/
public class DemoAction {
@Autowired UserDAO dao;
/** 分页查询列表 */
public void execute() {
Request req = ActionContext.getContext().getRequest();
// 创建对象
Paging page = new Paging();
page.setPageIndex(req.getParameterInt("pageIndex", 0)) // 获取页索引
page.setPageSize(req.getParameterInt("pageSize", -1)); // 获取页大小
// 放入内存
req.setAttribute("page", page);
// 分页查询
List<GpUser> list = dao.listPage(page);
// 放入内存
req.setAttribute("list", list);
}
}
/**
* SpringJDBC
*/
public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {
/**
* 分页查询人
*/
public List<User> listPage(Paging page) {
String sql = "select t1.* from user t1";
// 设置总记录数
page.setTotalRecordsNumber(
// 查询总记录
getJdbcTemplate().queryForInt("select count(*) from ("+sql+")")
);
// 生成分页SQL
String pageSql = page.paginal(sql);
return getJdbcTemplate().query(pageSql, new RowMapper<User>() {
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User e = new User();
e.setUserId(rs.getString("USERID"));
...
return e;
}
});
}
}
<%-- JSP页面 --%>
<form name="search-form"
action="${pageContext.request.contextPath}/user.jhtml" method="post">
<input type="hidden" id="pageIndex" name="pageIndex" value="1" />
<input type="hidden" id="pageSize" name="pageSize"
value="${requestScope.page.pageSize}" />
<c:forEach var="foo" items="${requestScope.list}" varStatus="status">
${foo.userId}
....
</c:forEach>
</form>
<script type="text/javascript">
function page(pageIndex, pageSize) {
$('input#pageIndex').val(pageIndex);
$('input#pageSize').val(pageSize);
window['search-form'].submit();
}
</script>
<!-- 分页 -->
<div class="paging">
<span>共<b>${page.totalRecordsNumber}</b>条记录,每页</span>
<select onchange="page('1', this.value)">
<c:if test="${10 eq page.pageSize}">
<option value="10" selected="selected">10</option>
<option value="15">15</option>
<option value="25">25</option>
<option value="50">50</option>
</c:if>
<c:if test="${25 eq page.pageSize}">
<option value="10">10</option>
<option value="15">15</option>
<option value="25" selected="selected">25</option>
<option value="50">50</option>
</c:if>
<c:if test="${50 eq page.pageSize}">
<option value="10">10</option>
<option value="15">15</option>
<option value="25">25</option>
<option value="50" selected="selected">50</option>
</c:if>
</select>
<span>条,页面:<b>${page.pageIndex}/${page.pageCount}</b></span>
<c:if test="${page.pageIndex le 1}">
<span>首页</span>
<span>上一页</span>
</c:if>
<c:if test="${page.pageIndex gt 1}">
<a href="javascript:;"
onclick="page('1', '${page.pageSize}')">首页</a>
<a href="javascript:;"
onclick="page('${page.pageIndex-1}', '${page.pageSize}')">上一页</a>
</c:if>
<c:if test="${page.pageIndex ge page.pageCount}">
<span>下一页</span>
<span>尾页</span>
</c:if>
<c:if test="${page.pageIndex lt page.pageCount}">
<a href="javascript:;"
onclick="page('${page.pageIndex+1}', '${age.pageSize}')">下一页</a>
<a href="javascript:;"
onclick="page('${page.pageCount}', '${page.pageSize}')">尾页</a>
</c:if>
</div><!-- /.paging -->