如何使用分页和分页样式

来源:互联网 发布:se源码 编辑:程序博客网 时间:2024/06/06 07:41

分页组件的使用:

第一步:将util文件夹复制到项目 的SRC文件下面
第二步:将mypage.tld文件复制到web-inf文件夹下面
第三步:打开需要分页的页面,在page指令下方添加导入标签库的代码:
<%@taglib uri="/WEB-INF/MyPage.tld" prefix="pt"%>
在需要显示分页的地方,添加如下代码:
<pt:page pageIndex="当前第几页" url="当前页面地址?" pageMax="总的页数"/>

 

我们在action中所要使用的代码:

//分页所用
 private String page; //前台传过来的页指针
 private int pageIndex=1; //当前页
 private int pageMax;// 总页数

此处省略这三个属性的set和get方法

/**
  * 获取当前页面的方法
  * @return
  */
 public String getpage(){
  if (page != null && !page.equals(""))
   pageIndex=Integer.parseInt(page); // 当前页
  int size = 2; // 每页的记录数
  int count = biz.queryByAll().size();// 总记录数
  
  pageMax = count % size == 0 ? count / size : count / size + 1;// 总页数
  list = biz.queryByPage(pageIndex, size);
    return "ok";
 }

 

/**
  * 默认执行的方法,并把部门和角色的信息传入到页面
  * @return
  */
 public String execute()
 {
  queryAllDept();
  queryAllRole(); 
  list=biz.queryByAll();//查询出员工的全部记录
  getpage();   //调用分页的方法,我们这边如果在默认的execute()方法中如果没有调用,那么我们在<pt:page pageIndex="当前第几页"url="当前页面地址?"             pageMax="总的页数"/>中的url中要加上getpage方法比如:url="SysEmp_getpage?"
  
  return "ok";

 }

 

//分页样式
DIV.digg {padding: 6px;margin:13px 3px 15px 3px;text-align: right;}
DIV.digg A {border: #cfd0c8 1px solid; padding:2px 5px;margin: 2px;color: #000;font-size:12px;line-height:22px;text-decoration: none;}
DIV.digg A:hover {background-color: #aed256;color: #FFF;text-decoration: none;}
DIV.digg A:active{ color: #000;border: #cfd0c8 1px solid;}
DIV.digg SPAN.current {border: #cfd0c8 1px solid;padding: 2px 4px;font-weight: bold;margin: 2px;color: #fff;background-color: #A6D0E7;}
DIV.digg SPAN.disabled {border: #eee 1px solid;padding: 5px;margin: 2px;color: #ddd;}

 

然后在要分页的页面写如下即可:

<tr>
    <td height="40">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" cellspacing="1" bgcolor="#D1EBF9">
      <tr>
       <td ><div class="digg"><span class="current"><pt:page pageIndex="${pageIndex}"url="SysEmp?" pageMax="${pageMax}"/></span></div></td>
      </tr>
     </table>
    </td>
  </tr>

上面的红色部门为关键之处(其中url要特别注意,看是否要加上getpage方法,url是struts.xml文件中指向当前页面的action名字)