ssh分页1

分页工具类:
package com.leatherstore.other;

public class Pager {
private int nowPager;//当前页
private int pageSize;//每页显示的行数
private int totalRecords;//总信息数
private int totalPage;//总页数


public Pager() {
super();
}

public Pager getPage(String pageNum ,int totalRecords,int pageSize){
this.pageSize=pageSize;
this.totalRecords=totalRecords;
if(totalRecords%pageSize==0){//判断总页数
totalPage=totalRecords/pageSize;
}else{
totalPage=totalRecords/pageSize+1;
}
if(totalPage==0){//如果总页数等于0,总页数等于1
totalPage=1;
}
if(pageNum==null || "".equals(pageNum)){//判断pageNum为空显示当前页为第一页
nowPager=1;
}else{
try {
nowPager=Integer.parseInt(pageNum);//当前页就等于参数对应pageNum
} catch (NumberFormatException e) {
nowPager=1; //出现异常当前页也等于1
}
}
if(nowPager<1){//如果当前页小于1 当前也就等于1
nowPager=1;
}
if(nowPager>totalPage){//如果大于总页数 就等于总页数
nowPager=totalPage;
}
return this;

}

public int getNowPager() {
return nowPager;
}
public void setNowPager(int nowPager) {
this.nowPager = nowPager;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}


}


分页方法:

Java code
package com.leatherstore.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leatherstore.dao.FwxxDao;
import com.leatherstore.entity.Fwxx;
import com.leatherstore.other.Pager;

public class FwxxDaoImpl extends HibernateDaoSupport implements FwxxDao {

public int getFwxxCount() {
String hql = "select count(*) from Fwxx";
Query query = this.getSession().createQuery(hql);
return ((Number) query.uniqueResult()).intValue();
}

@SuppressWarnings("unchecked")
public List<Fwxx> getFwxxList(Pager page) {
String hql = "from Fwxx order by fwid asc ";
Query query = this.getSession().createQuery(hql);
query.setFirstResult((page.getNowPager() - 1) * page.getPageSize());// 从第几条开始取
query.setMaxResults(page.getPageSize());// 取多少条
return query.list();
}

}


bizImpl层:
package com.leatherstore.biz.impl;

import java.util.List;

import com.leatherstore.biz.FwxxBiz;
import com.leatherstore.dao.FwxxDao;
import com.leatherstore.entity.Fwxx;
import com.leatherstore.other.Pager;

public class FwxxBizImpl implements FwxxBiz {

private FwxxDao fwxxDao;

public void setFwxxDao(FwxxDao fwxxDao) {
this.fwxxDao = fwxxDao;
}

public int getFwxxCount() {
return fwxxDao.getFwxxCount();
}

public List<Fwxx> getFwxxList(Pager page) {
return fwxxDao.getFwxxList(page);
}

}


action:
package com.leatherstore.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.leatherstore.biz.FwxxBiz;
import com.leatherstore.entity.Fwxx;
import com.leatherstore.other.Pager;
import com.leatherstore.struts.form.FwxxForm;

public class FwxxAction extends Action {
/*
* Generated Methods
*/
private FwxxBiz fwxxBiz;

public void setFwxxBiz(FwxxBiz fwxxBiz) {
this.fwxxBiz = fwxxBiz;
}
//分页属性
private String pageNum;
private String pageSize;
private int totalRecords;

public String getPageNum() {
return pageNum;
}

public void setPageNum(String pageNum) {
this.pageNum = pageNum;
}

public String getPageSize() {
return pageSize;
}

public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}

public int getTotalRecords() {
return totalRecords;
}

public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
FwxxForm fwxxForm = (FwxxForm) form;
Pager page=null;
try {
page =new Pager().getPage(pageNum, fwxxBiz.getFwxxCount(), Integer.parseInt(pageSize));
} catch (Exception e) {
page =new Pager().getPage(pageNum, fwxxBiz.getFwxxCount(), 4);
}
List<Fwxx> fwxxList=fwxxBiz.getFwxxList(page);
request.setAttribute("fwxx", fwxxList);
request.setAttribute("page", page);
return mapping.findForward("showFwxx");
}

页面
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.hand {
cursor: pointer;
}</style>
<script type="text/javascript">
//首页
function firstPage(){
var num = document.getElementById("hiddenPageNum");
if("" != null){
num.value = 1;
document.forms[0].submit();
}
}

//上一页
function prePage(){
var num = document.getElementById("hiddenPageNum");
if("" != null){
num.value = num.value - 1;
document.forms[0].submit();
}
}

//下一页
function nextPage(){
var num = document.getElementById("hiddenPageNum");
if("" != null){
num.value = num.value - 0 + 1;
document.forms[0].submit();
}
}

//末页
function lastPage(){
var num = document.getElementById("hiddenPageNum");
if("" != null){
num.value = ${page.totalPage};
document.forms[0].submit();
}
}

</script>
</head>

<body>
<form action="/sshpager/fwxx.do">
<table>
<tr>
<td>
Title
</td>
<td>
Zj
</td>
<td>
Lxr
</td>
<td>
Date
</td>
</tr>
<logic:iterate id="fwxx" name="fwxx">
<tr>
<td>
${fwxx.title }
</td>
<td>
${fwxx.zj }
</td>
<td>
${fwxx.lxr }
</td>
<td>
${fwxx.date }
</td>
</tr>
</logic:iterate>
</table>
<div>
<span class="hand" οnclick="firstPage()">首页</span>
<span class="hand" οnclick="prePage()">上一页</span>
<span class="hand" οnclick="nextPage()">下一页</span>
<span class="hand" οnclick="lastPage()">末页</span>
${page.nowPager}/${page.totalPage}
</div>
<input type="hidden" name="pageNum" value="${page.nowPager }" id="hiddenPageNum">
</form>
</body>
</html>


Title Zj Lxr Date
北大附近招合租 1000.0 周星星 2007-07-03 09:38:09.0
健翔桥一居出租了 1200.0 刘景杨 2007-07-03 10:01:00.0
出租2居 2100.0 伊先生 2007-07-03 01:04:05.0
便宜出租前门四合院 2500.0 赵大宝 2007-07-03 01:10:29.0

首页 上一页 下一页 末页 1/3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值