ssh分页问题

最近刚刚整合了ssh框架,在弄分页时下了很大的功夫才搞定,网上看了半天都太复杂,懒得弄,我就把我自己写的分享给大家吧。

jsp页面代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'UserManager.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script src="js/jquery-1.7.2.js" type="text/javascript"></script>
 <script type="text/javascript">
  
  /*
  function checkAll() {
   var selectFlags = document.getElementsByName("id");
   for(var i = 0; i < selectFlags.length; i++) {
    selectFlags[i].checked = document.getElementById("ifAll").checked;
   }
  }
  */
  
  $(function() {
           $("#ifAll").click(function() {
                 $('input[name="id"]').attr("checked",this.checked);
             });
             var $subBox = $("input[name='id']");
             $subBox.click(function(){
                 $("#ifAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
             });
         });
  
  function deleteUser() {
   var selectFlags = document.getElementsByName("id");
   var flag = false;
   for(var i = 0; i < selectFlags.length; i++) {
    if(selectFlags[i].checked) {
     flag = true;
     break;
    }
   }
   
   if(!flag) {
    alert("请选择要删除的数据!");
    return;
   }
   //删除提示
   if(window.confirm("确认删除?")) {
    with(document.getElementById("userform")) {
     action = "userManager.action";
     method= "post";
     submit();
    }
   }
  }
  
  function modifyUser() {
   var selectFlags = document.getElementsByName("id");
   var count = 0;
   for(var i = 0; i < selectFlags.length; i++) {
    if(selectFlags[i].checked) {
     count++;
    }
   }
   if(count == 0 ) {
    alert("请选择要修改的数据!");
    return;
   }
   
   if(count > 1) {
    alert("只能选择一条数据进行修改!");
    return;
   }
   
   with(document.getElementById("userform")) {
    action = "userManager!updateUser.action";
    method= "post";
    submit();
   }
  }
  
  function add() {
   window.self.location="admin/child/addUser.jsp";
  }
  
  function topPage() {
   
   with(document.getElementById("userform")) {
    action = "list.action?result=topPageNo";
    method= "post";
    submit();
   }
   
  }
  
  function previousPage() {
   with(document.getElementById("userform")) {
    action = "list.action?result=previousPage";
    method= "post";
    submit();
   }
  } 
  
  function nextPage() {
   with(document.getElementById("userform")) {
    action = "list.action?result=nextPage";
    method= "post";
    submit();
   }
  }
  
  function bottomPage() {
   with(document.getElementById("userform")) {
    action = "list.action?result=bottomPage";
    method= "post";
    submit();
   }
  }
</script>
  </head>
 
  <body>
    用户管理<br>
    <div>
    <s:form id="userform">
   
    <table border="1" width="60%">
   
    <h1>${message1 }</h1>
    <tr>
     <td>
      <input type="button" value="添加" onClick="add()">
     </td>
    </tr>
    <tr>
      <td>
    <input type="checkbox" id="ifAll">
   </td>
      <td>Id</td>
      <td>用户名</td>
      <td>用户密码</td>
      <td>用户级别</td>
      
     </tr>
    <s:iterator value="users">
   
     <tr>
      <td>
    <input type="checkbox" name="id"
      value="<s:property value="id"/>">
   </td>
      <td><s:property value="id"/></td>
      <td><s:property value="username"/></td>
      <td><s:property value="password"/></td>
      <td><s:property value="level"/></td>
      
     </tr>
    </s:iterator>
    <tr>
      <td>
       <input type="button" id="btnModifyUser" value="修改" onClick="modifyUser()">
      </td>
      <td colspan="3">
       <input type="button" id="btnDeleteUser" value="删除" onClick="deleteUser()" >
       &nbsp;&nbsp;&nbsp;&nbsp;
       <input type="button" id="firstpage" value="首页" onClick="topPage()" >
       &nbsp;&nbsp;&nbsp;&nbsp;
       <input type="button" id="earlypage" value="上一页" onClick="previousPage()" >
       &nbsp;&nbsp;&nbsp;&nbsp;
       <input type="button" id="latepage" value="下一页" onClick="nextPage()" >
       &nbsp;&nbsp;&nbsp;&nbsp;
       <input type="button" id="lastpage" value="尾页" onClick="bottomPage()" >
       <input type="hidden" name="pageNo" value="${pageNo}">
      </td>
      <td>
       第${pageNo}页
       &nbsp;&nbsp;&nbsp;&nbsp;
       共${totalPages}页
      
      </td>
    </tr>
    </table>
    </s:form>
    </div>
    <s:debug></s:debug>
   
  </body>
</html>

 


 

action中的代码为:

package com.suliao.login.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.suliao.login.dao.LoginDao;
import com.suliao.login.model.Page;
import com.suliao.login.model.User;
@Component("login")
@Scope("prototype")
public class LoginAction extends ActionSupport {
 
 private String username;
 private String password;
 
 private User user = new User();
 private LoginDao loginDao;
 private List<User> users;
 private int topPageNo;
 private int previousPage;
 private int nextPage;
 private int bottomPageNo;
 private int totalRecords;
 private int totalPages;
 
 private Page page = new Page();
 private String result;
 
 public int getTotalPages() {
  return totalPages;
 }
 public void setTotalPages(int totalPages) {
  this.totalPages = totalPages;
 }

 public String getResult() {
  return result;
 }
 public void setResult(String result) {
  this.result = result;
 }
 public Page getPage() {
  return page;
 }
 public void setPage(Page page) {
  this.page = page;
 }
 public int getTotalRecords() {
  return totalRecords;
 }
 public void setTotalRecords(int totalRecords) {
  this.totalRecords = totalRecords;
 }
 public int getTopPageNo() {
  return topPageNo;
 }
 public void setTopPageNo(int topPageNo) {
  this.topPageNo = topPageNo;
 }
 public int getPreviousPage() {
  return previousPage;
 }
 public void setPreviousPage(int previousPage) {
  this.previousPage = previousPage;
 }
 public int getNextPage() {
  return nextPage;
 }
 public void setNextPage(int nextPage) {
  this.nextPage = nextPage;
 }
 public int getBottomPageNo() {
  return bottomPageNo;
 }
 public void setBottomPageNo(int bottomPageNo) {
  this.bottomPageNo = bottomPageNo;
 }

 private int pageNo;
 private int pageSize;
 
 public int getPageNo() {
  return pageNo;
 }
 public void setPageNo(int pageNo) {
  this.pageNo = pageNo;
 }
 public int getPageSize() {
  return pageSize;
 }
 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
 }
 public List<User> getUsers() {
  return users;
 }
 public void setUsers(List<User> users) {
  this.users = users;
 }
 public User getUser() {
  return user;
 }
 public void setUser(User user) {
  this.user = user;
 }
 
 
 public LoginDao getLoginDao() {
  return loginDao;
 }
 @Resource
 public void setLoginDao(LoginDao loginDao) {
  this.loginDao = loginDao;
 }
 
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 
 
 @Override
 public String execute() throws Exception {
  user.setUsername(username);
  user.setPassword(password);
  boolean enter = loginDao.loginCheck(user);
  if(enter) {
   return SUCCESS;
  }else {
   return INPUT;
  }
  
 }
 
 public String list() throws Exception {
  page.setTotalRecords(loginDao.countUser());
  page.setPageSize(3);
  if(pageNo == 0) {
   pageNo = 1;
  }
  page.setPageNo(pageNo);
  if("topPageNo".equals(result)) {
   page.setPageNo(page.getTopPageNo());
  }
  if("previousPage".equals(result)) {
   page.setPageNo(page.getPreviousPage());
  }
  if("nextPage".equals(result)) {
   page.setPageNo(page.getNextPage());
  }
  if("bottomPage".equals(result)) {
   page.setPageNo(page.getBottomPageNo());
  }
  
  totalPages = page.getTotalPages();
  pageNo = page.getPageNo();
  users = loginDao.listAllUsers(page.getPageNo(), page.getPageSize());
  
  return "list";
 }
}

 

page类中的代码为:

package com.suliao.login.model;

import java.util.List;

public class Page<T> {
 //保存结果集
  private List<T> list;
  
  //保存记录数
  private int totalRecords;
  
  //每页多少条
  private int pageSize;
  
  //第几页
  private int pageNo;
  
  public int getPageNo() {
   return pageNo;
  }

  public void setPageNo(int pageNo) {
   this.pageNo = pageNo;
  }

  /*
   * 返回总页数
   */
  public int getTotalPages() {
   return (totalRecords + pageSize - 1) / pageSize;
  }
  /**
   * 首页
   * @return
   */
  public int getTopPageNo() {
   return 1;
  }
  /**
   * 上一页
   * @return
   */
  public int getPreviousPage() {
   if(this.pageNo <= 1) {
    return 1;
   }else {
    return pageNo - 1;
   }
  }
  /**
   * 下一页
   */
  public int getNextPage() {
   if(this.pageNo >= this.getBottomPageNo()) {
    return getBottomPageNo();
   }else {
    return pageNo + 1;
   }
  }
  /**最后一页
   * @return
   */
  public int getBottomPageNo() {
   return getTotalPages();
  }
  
  public int getPageSize() {
   return pageSize;
  }

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

  public List<T> getList() {
   return list;
  }

  public void setList(List<T> list) {
   this.list = list;
  }

  public int getTotalRecords() {
   return totalRecords;
  }

  public void setTotalRecords(int totalRecords) {
   this.totalRecords = totalRecords;
  }
  
}

 

其他的配置文件在这里就不在罗嗦了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值