首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 开源技术 >> Struts >> 正文

我的struts分页算法的实现

在HTML中按下一页或者上一页的时候有如下代码:
<logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/List.do?action=nextPage">
nextPage
</html:link>
</logic:equal>
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/List.do?action=previousPage">
PreviousPage
</html:link>
</logic:equal>
然后在Action中作如下处理:  
  String currentPage = request.getParameter("currentPage");
  HttpSession session = request.getSession(); 
  EmployeeForm employeeForm = (EmployeeForm) form;
  String queryString = null;
  String queryCon = null;
  String action = employeeForm.getAction();
  List list = new ArrayList();
  PageBean pb = null;
  EmployeeDao employeeDao = new EmployeeDao();
  if(action == null || action.equals("null")){
   int totalRows = employeeDao.getTotalRows();
  
    pb = new PageBean(totalRows);
    session.removeAttribute("page");
    queryString = employeeForm.getQueryString();
    queryCon = employeeForm.getQueryCon();
    session.setAttribute("queryString",queryString);
    session.setAttribute("queryCon",queryCon);   
    list = employeeDao.getAllEmployee(queryString, queryCon,
      String.valueOf(pb.getPageStartRow()),
      String.valueOf(pb.getPageRecorders()));
       
  }else if(action.equals("nextPage")){
   queryString = (String)session.getAttribute("queryString");
   queryCon = (String)session.getAttribute("queryCon");      
   employeeForm.setQueryString(queryString);
   employeeForm.setQueryCon(queryCon);
   pb = (PageBean)session.getAttribute("page");
   pb.nextPage();
   list = employeeDao.getAllEmployee(queryString, queryCon,
     String.valueOf(pb.getPageStartRow()),
     String.valueOf(pb.getPageRecorders()));
  }else if(action.equals("previousPage")){
   queryString = (String)session.getAttribute("queryString");
   queryCon = (String)session.getAttribute("queryCon");
   employeeForm.setQueryString(queryString);
   employeeForm.setQueryCon(queryCon);
   pb = (PageBean)session.getAttribute("page");   
   pb.previousPage();
   list = employeeDao.getAllEmployee(queryString, queryCon,
     String.valueOf(pb.getPageStartRow()),
     String.valueOf(pb.getPageRecorders()));
  }
        
        pb.description();
        session.setAttribute("page",pb);        
  request.setAttribute("admin", "admin");
  request.setAttribute("employee", list);
  return mapping.findForward("showlist");
  然后在数据库查询中有如下代码:
/**
*查询总记录数
*/
 public int getTotalRows() {
  int totalRows = 0;
  String sql = "select count(*) from employee";//假设是员工表
  Database db = new Database();
  ResultSet rs = db.executeQuery(sql);
  try {
   while (rs.next()) {
    String id = (String) rs.getString(1);
    totalRows = Integer.parseInt(id);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  db.close();
  return totalRows;
 }
 
/*
*查询每一页需要查询的页码
*/
public List getAllEmployee(String queryString, String queryCon,String startRow,String num) {
  List list = new ArrayList();
  String sql = null;
  if (queryString == null || queryString.equals("")) {
   sql = "select * from employee,dept " +
     "where dept.Id = employee.deptId " +
     "order by employee.id asc"+ " limit "+startRow+","+num;
  } else {
   sql = "select * from employee,dept " +
     "where dept.Id = employee.deptId order by employee."
     + queryString + " " + queryCon + " limit "+startRow+","+num;
  }
  Employee employee = null;
  Database db = new Database();
  ResultSet rs = db.executeQuery(sql);
  try {
   while (rs.next()) {
    String id = (String) rs.getString("employee.id");
    String name = (String) rs.getString("employee.name");
    String deptId = (String) rs.getString("employee.deptId");
    String deptName = (String) rs.getString("dept.deptName");
    employee = new Employee();
    employee.setId(id);
    employee.setName(name);
    employee.setDeptId(deptId);
    employee.setDeptName(deptName);
    list.add(employee);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  db.close();
  return list;
 }
这里我用了hibernate进行数据库操作,你也可以用jdbc进行操作,情况类似。

上一页  [1] [2] [3] 下一页

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·Oracle外键及外键约束修改行为
 ·开源软件测试工具学习专题
 ·JSP Web开发 入门基础到高手进阶教程
 ·JavaFX—是Java桌面的新希望么?
 ·安全至上 .NET开发安全策略
 ·测试用例设计之道-测试用例学习专题
 ·面向Java开发人员的Scala指南
 ·Java设计模式之实例详解
 ·Oracle数据库11g 面向DBA和开发人员的重要新特性
 ·桌面应用软件编程 J2SE技术详解
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航