最近在学习SSH,涉及到分页(废话啊,那个WEB程序没有涉及到分页),网上有很多方法,也看了一些,就写了一个简单的分页组件(就是抄的吧,本人菜鸟,望大神们见谅,希望对有些对分页头疼的人有些帮助)。
首先:主类:
/**
* 分页组件
* @author Administrator
*
*/
public class PageModel {
public final static int everyPage = 5 ;
//当前页
private int currentPage ;
//结果集,之所以使用通配符,是因为可以适配不同的对象
//比如说teacher类,student类.....
private List<?> target ;
//信息的总数
private long totalInfo ;
//返回总页数
public long getTotalPage(){
return (totalInfo + everyPage - 1) / everyPage ;
}
//返回第一页
public int getFirstPage(){
return 1 ;
}
//返回上一页
public int getPreviousPage(){
return currentPage <= 1? 1 : currentPage - 1 ;
}
//返回下一页
public long getNextPage(){
long result = getTotalPage() ;
if(currentPage >= result)
return result == 0 ? 1 : result ;
return currentPage + 1 ;
}
//返回尾页
public long getLastPage(){
return getTotalPage() == 0 ? 1 : getTotalPage() ;
}
public int getCurrentPage() {
return currentPage;
}
public List<?> getTarget() {
return target;
}
public long getTotalInfo() {
return totalInfo;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setTarget(List<?> target) {
this.target = target;
}
public void setTotalInfo(long totalInfo) {
this.totalInfo = totalInfo;
}
}
举个栗子,我们来简单调用一下:存在一个Mark类(成绩类):
@Entity
public class Mark implements java.io.Serializable {
private static final long serialVersionUID = -8157447190324016327L;
private int id ;
private double score ;
private Course course ;
private Student student ;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@ManyToOne
@JoinColumn(name="courseId")
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
@ManyToOne
@JoinColumn(name="studentId")
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
使用hibernate的session查找与mark相似的成绩时,我们可以返回一个PageModel,定义自己的cureentPage,代码如下:
public PageModel getMarkByCondition(int currentPage , Mark mark) throws Exception {
Example markExample = Example.create(mark).enableLike(MatchMode.ANYWHERE).excludeNone().ignoreCase() ;
@SuppressWarnings("unchecked")
List<Mark> marks = this.getSessionFactory().getCurrentSession().createCriteria(Mark.class).
add(markExample).
setFirstResult(PageModel.everyPage * (currentPage - 1)).
setMaxResults(PageModel.everyPage * currentPage).list() ;
PageModel pageModel = new PageModel() ;
pageModel.setCurrentPage(currentPage) ;
pageModel.setTarget(marks) ;
//这是另外一个方法,解决总体条数的,
pageModel.setTotalInfo(getAllMark().size());
return pageModel;
}
此时,在调用时,完全可以将所有的显示分页的jsp集合成一个公共的common.jsp,应用时只需在就可以了。来个小栗子:
在sturts2中action中返回一个获取全部用户信息的例子:
//获取用户信息
public String getUserInfo(){
pageModel = userDAO.findAllUserByPage(currentPage==0 ? 1 : currentPage) ;
//Constant类中元素
//public static final String PAGE_MODEL = "page_model" ;
//public static final String USER_PATH = "currentPath" ;
request.put(Constant.PAGE_MODEL, pageModel) ;
session.put(Constant.USER_PATH, "user/getUserInfo.action") ;
return "success" ;
}
那么在common.jsp中,我们可以这么写:
<table class="page_style">
<tr>
<td>共【${pageModel.totalInfo }】条记录</td>
<td>共【${pageModel.totalPage}】页</td>
<td>第【${currentPage}】页</td>
<td><a href="${currentPath}?currentPage=${pageModel.firstPage}">第一页</a>
</td>
<td><a href="${currentPath}?currentPage=${pageModel.previousPage}">上一页</a>
</td>
<td><a href="${currentPath}?currentPage=${pageModel.nextPage}">下一页</a>
</td>
<td><a href="${currentPath}?currentPage=${pageModel.lastPage}">尾页</a>
</td>
</tr>
</table>
得到的结果一般如下图:
现在,基本的分页功能就做好了,不用再担心自己不会了。。。