您的位置:首页 > 其它

基于SSH框架前后端分页

2016-04-26 15:46 447 查看
在网上一个包含页面段和后台的分页基本难得找到,这个分页是基于ssh框架的,看上去很复杂,但理解后非常简单在页面段的第二种版本能够带条件查询,只有按到下面的步骤做就一定会成功。如果在看这果过程中遇到其他问题,可以联系QQ:2930085110,我将提供源码和解答。

1.在action定义一个int 类型的page变量,并且写get(),set()方法

private int page;

public int getPage() {
return page;
}

public void setPage(int page) {
this.page = page;
}


2.在action定义一个PageBean 类型的pageBean变量,并且写get(),set()方法

private PageBean pageBean;

public PageBean getPageBean() {
return pageBean;
}

public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}


3.将函数pagnum()写在action里,在访问函数里面调用

@SuppressWarnings({ "unchecked", "rawtypes" })
private void pagnum() {
List listnum=new ArrayList();
if(page<=9){
if(pageBean.getTotalPage()<=9){
//page<9 &&page.getTotal()<=9
for(int i=1;i<=pageBean.getTotalPage();i++){
listnum.add(i);
}
}else{
//page<9 &&page.getTotal()>9
for(int i=1;i<=9;i++){
listnum.add(i);
}
}
}else{
if(page<=pageBean.getTotalPage()-4){
for(int i=-4;i<=4;i++){
listnum.add(page+i);
}
}else{
if(page<pageBean.getTotalPage()-3){
for(int i=-5;i<=3;i++){
listnum.add(page+i);
}
}else if( page<pageBean.getTotalPage()-2){
for(int i=-6;i<=2;i++){
listnum.add(page+i);
}
}else if( page<pageBean.getTotalPage()-1){
for(int i=-7;i<=1;i++){
listnum.add(page+i);
}
}else if( page<=pageBean.getTotalPage()){
for(int i=-8;i<=0;i++){
listnum.add(page+i);
}
}
}
}

ActionContext.getContext().put("pagnum", listnum);   //将页面要显示数字传回页面,key为pagnum(自定义),value为listnum
//this.page=1;  假如菜初始页面不能为0,就需要将这里加入
}


4.在action注入pageService

@Resource
private IPageService pageService;


**5.在需要分页访问的方法里写出hql语句,并调用pageService的方法**queryUserForPage(pageSize, page, hql)

1> String hql=”from Users where username=123”;

2> pagesize为每一页的记录数

page为当前页数

hql为数据库的hql查询语句

3> pageBean = pageService.queryUserForPage(8, page, hql);

4> List list =pageBean.getList(); //调用pageBean的getList(),返回查询对象的list集合

5> ActionContext.getContext().put(“storesList”,list); //将得到的对象集合传回页面,此处key为storesList(自定义),value为list

6> 调用pagnum()

6.在jsp页面端

第一种显示方法,这里的显示方法不是非常推荐使用,不能带条件

<!--显示分页内容-->
<div class="list">
<c:forEach  items="${storesList}" var="list">
${list.storesName}
</c:forEach>
</div>
<!--显示分页菜单栏-->
<div class="pager">
<s:if test="%{pageBean.currentPage == 1}">
首页  上一页
</s:if>
<s:else>
<a href="stores_stores.sityd?page=1">首页</a>
<a href="stores_stores.sityd?page=<s:property value="%{pageBean.currentPage-1}"/>">上一页</a>
</s:else>

<!--显示数字-->
<s:iterator value="#pagnum">
<a href="stores_stores.sityd?page=<s:property/>">
<s:property/>
</a>
</s:iterator>

<!-- 如果当前页不等总页数的最后一页 -->
<s:if test="%{pageBean.currentPage!= pageBean.totalPage}">
<a href="stores_stores.sityd?page=<s:property value="%{pageBean.currentPage+1}"/>">下一页</a>
<a href="stores_stores.sityd?page=<s:property value="pageBean.totalPage"/>">尾页</a>
</s:if>

<s:else>
下一页 尾页
</s:else>总页:<s:property value="pageBean.totalPage"/>

<!--显示当前页并且可以跳转-->
<form action="stores_stores.sityd" method="post">
<input type="number" name="page" min="1" max="<s:property value="pageBean.totalPage"/>" value="<s:property value="pageBean.currentPage"/>" style="width:35px;"/>
<input type="submit" value="跳转"/>
</form>
</div>


第二种页面显示方法,他的优点可以带条件分页查询,并且使用比较方便


<!--显示分页菜单栏-->
<div class="pager">
<ul>
<s:if test="%{pageBean.currentPage == 1}">
<a href="javascript:;" class="pageLink"><li>首页</li></a>
<a href="javascript:;" class="pageLink"><li>上一页</li></a>
</s:if>

<s:else>
<a onclick="pageAlter(1)" class="pageLink"><li>首页</li></a>
<a onclick="pageAlter(<s:property value="%{pageBean.currentPage-1}"/>)"  class="pageLink"><li>上一页</li></a>
</s:else>

<!--显示数字-->
<s:iterator value="#pagnum">
<a onclick="pageAlter(<s:property/>)" >
<li><s:property /></li>
</a>
</s:iterator>
<%-- <span class="selectPageLink">1</span> --%>
<!-- 如果当前页不等总页数的最后一页 -->
<s:if test="%{pageBean.currentPage!= pageBean.totalPage}">
<a onclick="pageAlter(<s:property value="%{pageBean.currentPage+1}"/>)" ><li>下一页</li></a>
<a onclick="pageAlter(<s:property value="pageBean.totalPage"/>)" ><li>尾页</li></a>
</s:if>

<s:else>
<a href="javascript:;" class="pageLink"><li>下一页</li></a>
<a href="javascript:;" class="pageLink"><li>尾页</li></a>
</s:else>

<form action="stores_stores.sityd" method="post" style="line-height:40px; display: inline;">
<input type="number" name="page" min="1" max="<s:property value="pageBean.totalPage"/>" value="<s:property value="pageBean.currentPage"/>" class="pager_input"/>
//<input type="text" name="storesCategoryAddress" value="<s:property value='#storesPage.storesCategoryAddress'/>" />
<input type="submit" value="跳转" class="pager_btn"/>
<li style="width:auto;border:none;">总共  <s:property value="pageBean.totalPage"/> 页</li>
</form>
</ul>
</div>

<form id="form_page" action="storesManage_storesManage.sityd" method="post">
<input id="page_id" type="hidden" name="page" value="" />
//<input type="text" name="storesCategoryAddress" value="<s:property value='#storesPage.storesCategoryAddress'/>" />
<input  type="hidden" name="flag" value="find" />
</form>


分页JS(注意。只有第二种版本才能用js和下面的css)

$(function(){
$(".pager ul").find("li").each(function(){
if($(this).html()==$("input[name='page']").val()){
$(this).css({"background":"#f60","color":"white"});
}
});
})
function pageAlter(page){
$("#page_id").val(page);
$("#form_page").submit();
}


**分页css**


.pager{
width:100%;
height:40px;
margin-top:20px;
text-align:center;
font: 14px/1 'Microsoft Yahei',Tahoma,'Simsun';
color: #444;
zoom: 1;
word-wrap: break-word;
border-top:1px solid #ccc;
}
.pager ul {
display: block;
width: auto;
height: 30px;
margin-top: 6px;
float: right;
}
.pager ul a{
color:#444;
}
.pager ul li{
display: block;
width: 45px;
height: 28px;
float: left;
border: 1px solid #f60;
list-style: none;
line-height: 28px;
margin-left: 6px;
color: #f60;
text-align:center;
}
.pager_input{
width: 40px;
display: block;
float: left;
outline: none;
height: 28px;
margin-left: 6px;
/* outline-color: #f60; */
border: 1px solid #f60;
}
.pager_btn{
width: 45px;
display: block;
float: left;
outline: none;
height: 30px;
font: 14px/1 'Microsoft Yahei',Tahoma,'Simsun';
color: white;
zoom: 1;
word-wrap: break-word;
cursor: pointer;
margin-left: 6px;
border: 1px solid #f60;
background-color: #f60;
}


7.service 层代码

import com.sityd.pagging.PageBean;
//service接口
public interface IPageService {
public PageBean queryUserForPage(int pageSize, int Page, String hql);
}

//接口实现
package com.sityd.pagging;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sityd.model.Stores;
import com.sityd.pagging.PageBean;

@Service("pageService")
public class PageService implements com.sityd.pagging.IPageService {
@Resource
private IPageDao pageDao;

public PageBean queryUserForPage(int pageSize, int Page, String hql) {

int allRow = pageDao.getAllRowCount(hql);// /总记录数
int totalPage = PageBean.countTotalPage(pageSize, allRow);// 总页数
final int currentPage = PageBean.countCurrentPage(Page);
final int offset = PageBean.countOffset(pageSize, currentPage,totalPage,allRow);// 当前页开始记录
int length = pageSize;// 每页记录数
System.out.println("偏移位置:"+offset);
// page为当前页
if(offset==0&&0!=(allRow%pageSize)){
length=allRow%pageSize;
}
@SuppressWarnings("rawtypes")
List list = pageDao.queryForPage(hql, offset, length);// "一页"的记录

Collections.reverse(list);
// 把分页信息保存到Bean中
PageBean pageBean = new PageBean();
pageBean.setPageSize(pageSize);
pageBean.setCurrentPage(currentPage);
pageBean.setAllRow(allRow);
pageBean.setTotalPage(totalPage);
pageBean.setList(list);
pageBean.init();
return pageBean;
}
}


8.dao层代码

//dao层接口
package com.sityd.pagging;

import java.util.List;

public interface IPageDao {
public int getAllRowCount(String hql);

@SuppressWarnings("rawtypes")
public List queryForPage(final String hql, final int offset,
final int length );
}
//dao层实现
package com.sityd.pagging;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import com.sityd.util.BaseDao;

@SuppressWarnings("rawtypes")
@Service("pageDao")
public class PageDao extends BaseDao implements IPageDao {

public int getAllRowCount(String hql) {
return this.getHibernateTemplate().find(hql).size();
}
public List queryForPage(final String hql, final int offset,final int length) {
List list = this.getHibernateTemplate().executeFind(
new HibernateCallback() {

public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query q = session.createQuery(hql); // 返回的就是一个对像
q.setFirstResult(offset);
q.setMaxResults(length);
List list = q.list();
return list;
}
});
return list;
}
}


9.pageBan代码

package com.sityd.pagging;

import java.util.List;

public class PageBean {
@SuppressWarnings("rawtypes")
private List list;// 要返回的某一页的记录列表
private int allRow;// 总记录数
private int totalPage;// 总页数
private int currentPage;// 当前页
private int pageSize;// 每页记录数
private boolean isFirstPage;// 是否为第一页
private boolean isLastPage;// 是否为最后一页
private boolean hasPreviousPage;// 是否有前一页
private boolean hasNextPage;// 是否有下一页

@SuppressWarnings("rawtypes")
public List getList() {
return list;
}

@SuppressWarnings("rawtypes")
public void setList(List list) {
this.list = list;
}

public int getPageSize() {
return pageSize;
}

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

public int getAllRow() {
return allRow;
}

public void setAllRow(int allRow) {
this.allRow = allRow;
}

public int getTotalPage() {
return totalPage;
}

public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

/*
*
* 初始化分页信息
*/
public void init() {
this.isFirstPage = isFirstPage();
this.isLastPage = isLastPage();
this.hasPreviousPage = isHasPreviousPage();
this.hasNextPage = isHasNextPage();
}

public boolean isFirstPage() {
return isFirstPage;
}

public void setFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
}

public boolean isLastPage() {
return isLastPage;
}

public void setLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
}

public boolean isHasPreviousPage() {
return hasPreviousPage;
}

public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}

public boolean isHasNextPage() {
return hasNextPage;
}

public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}

/**
* 计算总页数,静态方法,供外部直接通过类名调用
*
* @param pageSize
*            每页记录数
* @param allRow
*            总记录数
* @return 总页数
*/
public static int countTotalPage(final int pageSize, final int allRow) {
int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow
/ pageSize + 1;
return totalPage;
}

/**
* 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替   * @param page 传入的参数(可能为空,即0,则返回1)   @return
* 当前页  
*/
public static int countOffset(final int pageSize, final int currentPage,final int totalPage,final int allRow) {
int rest = allRow%pageSize;
if(rest==0){
final int offset =pageSize * (totalPage-currentPage);
return offset;
}else{
if(currentPage==totalPage){
return 0;
}else{
int offset = (pageSize * (totalPage-currentPage-1))+rest;
return offset;
}
}
}

/**
*    * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替    * @param page
* 传入的参数(可能为空,即0,则返回1)    * @return 当前页   
*/
public static int countCurrentPage(int page) {
final int curPage = (page == 0 ? 1 : page);
return curPage;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: