当前位置 : 主页 > 网络编程 > PHP >

SSH 分页方法一,基于struts2

来源:互联网 收集:自由互联 发布时间:2023-09-06
SSH分页查询留言记录 Dao层: /** * 分页查询全部留言 */ public ListLy getMessageByPaging(int currentPage,int pageSize){ Session session=this.getHibernateTemplate().getSessionFactory().openSession(); Query query=session.creat


SSH分页查询留言记录



Dao层:

/**
* 分页查询全部留言
*/
public List<Ly> getMessageByPaging(int currentPage,int pageSize){
Session session=this.getHibernateTemplate().getSessionFactory().openSession();
Query query=session.createQuery("from Ly ");
int startRow=(currentPage-1)*pageSize;
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
List<Ly> message=query.list();
session.close();
return message;

}

/**
* 得到留言总数
*/
public int getTotle(){
List<Ly> message=this.getHibernateTemplate().find("from Ly");
return message.size();
}

/**
* 分页查询指定课程留言
*/
public List<Ly> getMessageByCoursePaging(int courseId,int currentPage,int pageSize){
Session session=this.getHibernateTemplate().getSessionFactory().openSession();
Query query=session.createQuery("from Ly as a where a.kcId.id=?");
query.setParameter(0, courseId);
int startRow=(currentPage-1)*pageSize;
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
List<Ly> message=query.list();
session.close();
return message;
}

/**
* 获取指定课程留言总数
*/
public int getTotleByCourse(int courseId){
List<Ly> mesByCourse = this.getHibernateTemplate().find("from Ly as a where a.kcId.id=?",courseId);
return mesByCourse.size();
}



Service层:

/**
* 分页查询
*/
public List<Ly> getMessageByPaging(int currentPage,int pageSize){
return lyDao.getMessageByPaging(currentPage, pageSize);
}

/**
* 得到留言总数
*/
public int getTotal(){
return lyDao.getTotle();
}

/**
* 分页查询指定课程留言
*/
public List<Ly> getMesByCoursePaging(int courseId,int currentPage,int pageSize){
return lyDao.getMessageByCoursePaging(courseId, currentPage, pageSize);
}

/**
* 得到指定课程留言总数
*/
public int getTotalByCourse(int courseId){
return lyDao.getTotleByCourse(courseId);
}



Action层:

private int currentPage;
private int totalPage;
private int totalSize;
private int pageSize = 10;



/**
* 获得全部留言分页实现
* @return
*/
public String browsePaging() {
if(currentPage==0){
currentPage = 1;
}
totalSize=messageService.getTotal();

int mod = totalSize%pageSize;
if(mod==0){
totalPage = totalSize/pageSize;
}else
totalPage = totalSize/pageSize+1;

setGetMessage(messageService.getMessageByPaging(currentPage, pageSize));
return "success";
}



/**
* 保存留言后分页实现
*/
public String saveMessagePaging(){
if(currentPage==0){
currentPage = 1;
}
totalSize=messageService.getTotalByCourse(courseId);

int mod = totalSize%pageSize;
if(mod==0){
totalPage = totalSize/pageSize;
}else
totalPage = totalSize/pageSize+1;

setGetMessage(messageService.getMesByCoursePaging(courseId, currentPage, pageSize));
return "success";
}







JSP页面:

<!--全部留言-->

<center>当前是第${currentPage}页,共有${totalPage}页
<a href="browsePaging.action?currentPage=1" >首页</a>
<s:if test="%{currentPage>1}"><a href="browsePaging.action?currentPage=${currentPage-1}" >上一页</a>
<a href="browsePaging.action?currentPage=${currentPage-1}" >${currentPage-1}</a></s:if>
<a href="browsePaging.action?currentPage=${currentPage}" >${currentPage}</a>
<s:if test="%{totalPage>currentPage}"><a href="browsePaging.action?currentPage=${currentPage+1}" >${currentPage+1}</a>
<a href="browsePaging.action?currentPage=${currentPage+1}" >下一页</a></s:if>
<a href="browsePaging.action?currentPage=${totalPage}" >尾页</a></center>



<!--根据课程得到的留言-->

<center>
当前是第${currentPage}页,共有${totalPage}页
<a href="saveMessagePaging.action?currentPage=1&courseId=${courseId}" >首页</a>
<s:if test="%{currentPage>1}"><a href="saveMessagePaging.action?currentPage=${currentPage-1}&courseId=${courseId}" >上一页</a>
<a href="saveMessagePaging.action?currentPage=${currentPage-1}&courseId=${courseId}" >${currentPage-1}</a></s:if>
<a href="saveMessagePaging.action?currentPage=${currentPage}&courseId=${courseId}" >${currentPage}</a>
<s:if test="%{totalPage>currentPage}"><a href="saveMessagePaging.action?currentPage=${currentPage+1}&courseId=${courseId}" >${currentPage+1}</a>
<a href="saveMessagePaging.action?currentPage=${currentPage+1}&courseId=${courseId}" >下一页</a></s:if>
<a href="saveMessagePaging.action?currentPage=${totalPage}&courseId=${courseId}" >尾页</a>
</center>


【文章转自日本多IP服务器 http://www.558idc.com/japzq.html提供,感恩】
网友评论