日期:2014-05-16 浏览次数:20405 次
话不多说了,直接上源码:
<SCRIPT type="text/javascript"> function frontPageCheck() { if(${page > 1}) { return true; } return false; } function backPageCheck() { if(${page < allPage}) { return true; } return false; } </SCRIPT>
<div class="pager"> <center> 总共有${allPage}页, 当前是第${page}页 <a href="${basePath}findAdmin?page=1"><font size="2" color="blue">首页</font> </a> <a href="${basePath}findAdmin?page=${page-1}" onclick="return frontPageCheck();"><font size="2" color="red">上一页</font> </a> <a href="${basePath}findAdmin?page=${page+1}" onclick="return backPageCheck();"><font size="2" color="red">下一页</font> </a> <a href="${basePath}findAdmin?page=${allPage}"><font size="2" color="blue">末页</font> </a> </center> </div>以上这段代码是对超链接中连接参数的设置,同理具体参数含义请参见下面的struts.xml配置文件和Action中定义
public class AdminAction { private AdminService adminService; …… /** 总共有多少页 */ private int allPage; /** 页面大小 */ private int pageSize = 3; /** 记录总数 */ private int allRows; /** 当前页面数, 默认为1 */ private int page = 1; /** 第一条记录的索引 */ private int offset; …… /** * 分页查询Admin * * @return 查询成功返回success,否则返回error * @throws Exception */ public String findAdmin() throws Exception { allRows = adminService.findRows(); offset = (page - 1) * pageSize; if (allRows % pageSize == 0) { allPage = allRows / pageSize; } else { allPage = allRows / pageSize + 1; } admins = adminService.getListForPage(offset, pageSize); if (admins != null) { return ActionSupport.SUCCESS; } return ActionSupport.ERROR; } }
为了一看究竟,还是再来看看adminService里面是怎么写的吧
public int findRows() { return adminDAO .findRows("select count(*) from Admin a group by a.adminNum"); }
public List<Admin> getListForPage(int offset, int pageSize) { return adminDAO.getListForPage("from Admin a order by a.adminNum asc", offset, pageSize); }
public List<Admin> getListForPage(final String hql, final int offset, final int lengh) { log.debug("finding ListForPage"); try { List list = getHibernateTemplate().executeFind( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { List list2 = session.createQuery(hql) .setFirstResult(offset) .setMaxResults(lengh).list(); return list2; } }); return list; } catch (RuntimeException re) { log.error("find ListForPage failed", re); throw re; } }
public int findRows() { return adminDAO .findRows("select count(*) from Admin a group by a.adminNum"); }
<!--有关管理员操作的所有Action --> <package name="zyc_admin" extends="struts-default" namespace="/admin"> …… <!-- 查询管理员 --> <action name="findAdmin" class="edu.nwsuaf.information.action.AdminAction" method="findAdmin"> <result name="success">/admin/indexAdmin.jsp</result>