SH分页
版本1.0
1. action当中申明及跳转
分页技术
private int totalElements;//总记录数
private int pageNow;//当前页数
private int pageSize = 15;//页面记录数
private int pageCount;//总页数
private List list;//list接值
private int i; //计算
分页结束
public String rolefenye() {
//分页技术
try {
totalElements = ServiceFactory.getRole().countRows();//取得数据库里的总的记录数
pageCount = (totalElements + pageSize - 1) / pageSize;//计算出总页数
ActionContext.getContext().getSession().put("pageCount", pageCount);//页面总数传到页面上
//判断上一页下一页页数
if (pageNow < 1) {
pageNow = 1;
}
if (pageNow > pageCount) {
pageNow = pageCount;
}
i = (pageNow - 1) * pageSize;
list = ServiceFactory.getRole().pageQuery(i, pageSize);//查询出表和最大数
ActionContext.getContext().getSession().put("pageNow", pageSize);//当前值传到页面上
ActionContext.getContext().getSession().put("lists", list);//查询出来的值传到页面上
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
2. serviceImpl实现方法
public int countRows() throws SQLException {
// 数据库里记录总数
int count = User_ManageFactory.getDaoRole().countRows();
return count;
}
public List pageQuery(int i,int pageSize) throws SQLException {
// 返回数据库的第一条和最大条数
List list = User_ManageFactory.getDaoRole().pageQuery(i, pageSize);
return list;
}
3. DaoImpl 实现类中
public int countRows() throws SQLException {
// 数据库记录总数
String sql = "from RoleName";
Query query = getSession().createQuery(sql);
return query.list().size();
}
@SuppressWarnings("unchecked")
public List pageQuery(int i,int pageSize) throws SQLException {
//返回第一个 和最大数
Query query = getSession().createQuery("from RoleName");
query.setFirstResult(i);
query.setMaxResults(pageSize);
return query.list();
}}
4. 页面接值
共页
当前页