SSH分页,要忘记的

SSH分页代码
1.分页类:
package com.siskin.util;

public class Pager {
private int currentPage;//当前页数
private int size = 10;//每页显示多少记录
private int totalCounts;//总记录数
private int totalpages;//总页数
    private int firstPage;
    private int lastPage;
    private int prePage;
    private int NextPage;
    private int startposition;//开始记录位置
   
public int getStartposition() {
   return (getCurrentPage()-1)*size;
}

public void setStartposition(int startposition) {
   this.startposition = startposition;
}

public int getFirstPage() {
   return 1;
}

public void setFirstPage(int firstPage) {
   this.firstPage = firstPage;
}

public int getLastPage() {
   return getTotalpages();
}

public void setLastPage(int lastPage) {
   this.lastPage = lastPage;
}

public int getPrePage() {
   if(getCurrentPage()-1<=0)
    return 1;
   return getCurrentPage()-1;
}

public void setPrePage(int prePage) {
   this.prePage = prePage;
}

public int getNextPage() {
   if(getCurrentPage()+1>getTotalpages())
    return getTotalpages();
   return getCurrentPage()+1;
}

public void setNextPage(int nextPage) {
   NextPage = nextPage;
}

public Pager(int currentPage, int totalCounts) {

   this.currentPage = currentPage;
   this.totalCounts = totalCounts;
}

public int getCurrentPage() {
   return currentPage;
}

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

public int getSize() {
   return size;
}

public void setSize(int size) {
   this.size = size;
}

public int getTotalCounts() {
   return totalCounts;
}

public void setTotalCounts(int totalCounts) {
   this.totalCounts = totalCounts;
}

public int getTotalpages() {
   if(totalCounts % size==0)
    return totalCounts/size;
   return totalCounts/size+1;
  
}

public void setTotalpages(int totalpages) {
   this.totalpages = totalpages;
}

}
2.IAPService接口
package com.siskin.service.iface.ap;

import java.util.List;

import com.siskin.data.ho.Ap;

public interface IApService {
void saveAp(Ap ap);
List queryALLAP();
Ap queryAp(long id);
List queryApByName(String apName);
void updateAp(Ap ap);
void deleteAp(long id);
int getTotalCount();
List queryPages(int start,int offset);
List queryApByComName(String comName);
}

3.实现类
package com.siskin.service.ap;


import java.util.List;

import sun.awt.geom.AreaOp.AddOp;

import com.siskin.dao.ap.iface.IApDao;
import com.siskin.data.ho.Ap;
import com.siskin.service.iface.ap.IApService;

public class ApService implements IApService {
IApDao apDao;

public void saveAp(Ap ap) {
   apDao.saveAp(ap);
  
}

public IApDao getApDao() {
   return apDao;
}

public void setApDao(IApDao apDao) {
   this.apDao = apDao;
}

public List queryALLAP() {
   return apDao.queryALLAp();
}

public void deleteAp(long id) {
   apDao.deleteAp(id);
  
}

public Ap queryAp(long id) {
   return apDao.queryAp(id);
}

public void updateAp(Ap ap) {
   apDao.updateAp(ap);
  
}

public List queryApByName(String apName) {
   // TODO Auto-generated method stub
   return apDao.queryApByName(apName);
}

public int getTotalCount() {
   return apDao.getTotalCount();
}

public List queryPages(int start, int offset) {
   return apDao.queryPages(start, offset);
}

public List queryApByComName(String comName) {
   // TODO Auto-generated method stub
   return apDao.queryApByComName(comName);
}

}

4.Dao接口
package com.siskin.dao.ap.iface;
import java.util.List;

import com.siskin.data.ho.Ap;
public interface IApDao {
void saveAp(Ap ap);
Ap queryAp(Long apId);
List queryApByName(String apName);
List queryApByComName(String comName);
List queryALLAp();
void updateAp(Ap ap);
void deleteAp(Long apId);
int getTotalCount();
List queryPages(int start,int offset);

}
5.实现类:
package com.siskin.dao.ap;

import java.util.List;

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

import com.siskin.dao.ap.iface.IApDao;
import com.siskin.data.ho.Ap;

public class ApDao extends HibernateDaoSupport implements IApDao {
/**
* 删除单个Ap对象
*/
public void deleteAp(Long apId) {
   Ap obj = queryAp(apId);
   this.getHibernateTemplate().delete(obj);
}

/**
* 查询全部Ap对象
*/
public List queryALLAp() {
   String hql = "from Ap";
   return this.getHibernateTemplate().find(hql);
}

/**
* 查询单个Ap对象
*/
public Ap queryAp(Long apId) {
   String hql = "from Ap as a where a.apId=" + apId;
   List list = this.getHibernateTemplate().find(hql);
   return (list.size() != 0) ? ((Ap) list.get(0)) : null;
}

/**
* 保存Ap对象
*/
public void saveAp(Ap ap) {
   this.getHibernateTemplate().save(ap);

}

/**
* 更新单个Ap对象
*/

public void updateAp(Ap ap) {
   this.getHibernateTemplate().update(ap);

}

public List queryApByName(String apName) {
   String hql = "from Ap as b where b.apAccount='" + apName + "'";
   List list = this.getHibernateTemplate().find(hql);
   return list;

}

public int getTotalCount() {
   String hql = "from Ap";
   List list = this.getHibernateTemplate().find(hql);
   return list.size();

}

public List queryPages(int start, int offset) {
   Session session=getSession();
   String hql="from Ap";
   Query query=session.createQuery(hql).setFirstResult(start).setMaxResults(offset);
   List list=query.list();
   return list;
  
}

public List queryApByComName(String comName) {
   String hql="from Ap as a where a.comName=?";
   List list=this.getHibernateTemplate().find(hql,new Object[]{comName});
   return list;
}

}

5.apAction
package com.siskin.action.ap;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.siskin.data.ho.Ap;
import com.siskin.service.iface.ap.IApService;
import com.siskin.service.sp.SpService;
import com.siskin.util.Pager;

public class ApAction extends ActionSupport {
IApService apService;
Ap ap;
int page=1;

public int getPage() {
   return page;
}

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

public IApService getApService() {
   return apService;
}

public void setApService(IApService apService) {
   this.apService = apService;
}

public Ap getAp() {
   return ap;
}

public void setAp(Ap ap) {
   this.ap = ap;
}

/**
* 操作AP(增加或修改)
*
* @return string
*/
public String modifyAp() {
   List list = apService.queryApByName(ap.getApAccount());
   if (list.size() != 0) {
    return ERROR;
   }
  
   List comNames=apService.queryApByComName(ap.getComName());
   if(comNames.size()!=0)
   {
    return "comName";
   }

   apService.saveAp(ap);
   return SUCCESS;
/* else {
    if (ap.getApId() == null) {
     apService.saveAp(ap);
     return SUCCESS;
    }
   }*/
}

public String modifyAp1() {
   apService.updateAp(ap);
   return SUCCESS;
}

/**
* 显示所有AP
*
* @return string;
*/
public String toApModifySelect() {
   HttpServletRequest request = ServletActionContext.getRequest();
   List apList = apService.queryALLAP();
   request.setAttribute("apList", apList);
   return SUCCESS;
}


/**
* 分页显示
*/

public String pagerList()
{
   HttpServletRequest request=ServletActionContext.getRequest();
   int totalCounts=apService.getTotalCount();//得到AP总的记录数
   System.out.println("数据库总记录数:"+totalCounts);
   System.out.println(page);
   Pager p=new Pager(page,totalCounts);
   System.out.println("当前页:"+page);
   System.out.println("第一页:"+p.getFirstPage());
   System.out.println("前一页:"+p.getPrePage());
   System.out.println("后一页:"+p.getNextPage());
   System.out.println("最后一页:"+p.getLastPage());
   System.out.println("总页数:"+p.getTotalpages());
   System.out.println("每页显示:"+p.getSize());
  
   List pagerList=apService.queryPages(p.getStartposition(),p.getSize());
   request.setAttribute("p", p);
   request.setAttribute("apList", pagerList);
   return SUCCESS;
}

/**
* 查询单个AP
*/
public String queryAp() {
   HttpServletRequest request = ServletActionContext.getRequest();
   Ap apTemp = apService.queryAp(ap.getApId());
   request.setAttribute("ap", apTemp);
   return SUCCESS;
}

/**
* 删除单个AP
*/
public String deleteAp() {
   apService.deleteAp(ap.getApId());
   return SUCCESS;
}

/**
*
* 修改密码
*/
public String toApModifyPassword(){
String password=ap.getPassword();
List list=apService.queryApByName(ap.getApAccount());
if(list.size()==0)
{
   return ERROR;
}
Ap ap=(Ap)list.get(0);
ap.setPassword(password);
apService.updateAp(ap);

return SUCCESS;

}

}
6.Struts_ap.xml
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ap" extends="struts-default"
   namespace="/jsp/ap">
  
   <!-- 增加功能 -->
   <action name="addAp" class="apAction"
    method="modifyAp">
    <result name="success">apSuccess.jsp</result>
    <result name="error">apNameAlreadyExist.jsp</result>
    <result name="comName">comNameAlreadyExist.jsp</result>
   </action>
  
   <!-- 修改功能 -->
   <action name="toApModifySelect" class="apAction"
    method="pagerList">
    <result name="success">apModifySelect.jsp</result>
   </action>
  
   <action name="modAp_init" class="apAction"
    method="queryAp">
    <result name="success">apAdd1.jsp</result>
   </action>
  
   <action name="modifyAp" class="apAction" method="modifyAp1">
    <result name="success">apModifySuccess.jsp</result>
   </action>
  
   <!-- 修改完成后查看结果 -->
   <action name="toApAdd" class="apAction"
    method="queryAp">
    <result name="success">apAdd.jsp</result>
   </action>
  
   <!-- 删除功能 -->
   <action name="toApModifyDelete" class="apAction"
    method="pagerList">
    <result name="success">apModifyDelete.jsp</result>
   </action>
   <action name="modAp_init_del" class="apAction"
    method="queryAp">
    <result name="success">apDeleteInfo.jsp</result>
   </action>
   <action name="deleteAp" class="apAction"
    method="deleteAp">
    <result name="success">apDeleteSuccess.jsp</result>
   </action>
  
   <!-- 重置密码 -->
   <action name="tomodifypassword" class="apAction"
    method="toApModifyPassword">
    <result name="success">apModifyPasswordSuccess.jsp</result>
    <result name="error">apNameAlreadyExist1.jsp</result>
   </action>
  
  
   <!-- 查询Ap -->
   <action name="tolookupAll" class="apAction" method="pagerList">
   <result name="success">apLookupSelect.jsp</result>
   </action>
  
   <action name="lookupAp" class="apAction"
    method="queryAp">
   <result name="success">apLookup.jsp</result>
   </action>
  
  
   <!-- 分页 -->
  
   <action name="toPageList" class="apAction" method="pagerList">
   <result name="success">apModifySelect.jsp</result>
   </action>
</package>
</struts>
7.JSP文件
<SCRIPT type="text/javascript">
    function jump(input)
    {
      if(input.value==${p.currentPage})
      {
      return;
      }
      var url="tolookupAll.action?page="+input.value;
      document.location=url;
    }
</SCRIPT>

<a href="tolookupAll.action?page=1">首页</a>

<c:if test="${p.currentPage>1}">
<a href="tolookupAll.action?page=${p.currentPage-1}"> [前页]</a>
</c:if>

<c:if test="${p.currentPage<=1}">
[前页]
</c:if>

<c:if test="${p.currentPage<p.totalpages}">
<a href="tolookupAll.action?page=${p.currentPage+1}"> [后页]</a>
</c:if>

<c:if test="${p.currentPage>=p.totalpages}">
[后页]
</c:if>

<a href="tolookupAll.action?page=${p.totalpages }">尾页</a>

第<font color="red">${p.currentPage }</font>页/共<font color="red">${p.totalpages }</font>页
转到<SELECT οnchange="jump(this)">
    <c:forEach var="i" begin="1" end="${p.totalpages}">
    <option value="${i}"
     <c:if test="${p.currentPage==i}">
        selected
     </c:if>
    
    >第${i}页</option>
    </c:forEach>
</SELECT>
输入页码:<input type="text" size="4" value="${p.currentPage}" id="jumpbox"><input type="button" value="跳 转"
οnclick="jump(document.getElementById('jumpbox'))"
>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值