国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
jsp 分頁程序詳解
 private int currentPage = 1; // 當(dāng)前頁

 private int totalPages = 0; // 總頁數(shù)

 private int pageRecorders = 5;// 每頁5條數(shù)據(jù) 

 private int totalRows = 0; // 總數(shù)據(jù)數(shù)

 private int pageStartRow = 0;// 每頁的起始數(shù)

 private int pageEndRow = 0; // 每頁顯示數(shù)據(jù)的終止數(shù)

 private boolean hasNextPage = false; // 是否有下一頁

 private boolean hasPreviousPage = false; // 是否有前一頁
 
 private int nextPage = 0;//下一頁的頁碼
 
 private int previousPage = 0;//上一頁的頁碼

然后這些屬性之間是有聯(lián)系的,我們可以在構(gòu)造函數(shù)的時(shí)候就初始化一些屬性
有兩種方法:
一,根據(jù)總的頁數(shù),(假設(shè)當(dāng)前頁為1)
 public PageBean(int totalRows){
  this.totalRows = totalRows;
  this.currentPage = 1;
  hasPreviousPage = false;
  if ((totalRows % pageRecorders) == 0) {
   totalPages = totalRows / pageRecorders;
  } else {
   totalPages = totalRows / pageRecorders + 1;
  }
  if (totalRows >= pageRecorders) {
   hasNextPage = true;
   nextPage = 2;
   this.pageEndRow = pageRecorders;
  } else {
   this.pageEndRow = totalRows;
   hasNextPage = false;
   nextPage = 1;
  }
   this.pageStartRow = 0;  
   previousPage = 1;
 }
然后在按下一頁或者上一頁的時(shí)候需要如下函數(shù)處理:
public void nextPage() {
  if(hasNextPage == true)
  currentPage = currentPage + 1;
  if ((currentPage - 1) > 0) {
   hasPreviousPage = true;
  } else {
   hasPreviousPage = false;
  }
  if (currentPage >= totalPages) {
   hasNextPage = false;
   this.nextPage = currentPage;
  } else {
   hasNextPage = true;
   nextPage = currentPage+1;
  }
  this.pageStartRow = (currentPage -1) * pageRecorders;
  if(hasNextPage == true)
  this.pageEndRow = pageStartRow + 5;
  else{
   this.pageEndRow =this.totalPages;
  }
  previousPage = currentPage - 1;
 }
 
 public void previousPage() {
  if(hasPreviousPage == true)
  currentPage = currentPage - 1;
  if (currentPage == 0) {
   currentPage = 1;
  }
  if (currentPage >= totalPages) {
   hasNextPage = false;
  } else {
   hasNextPage = true;
  }
  nextPage = currentPage + 1;
  if ((currentPage - 1) > 0) {
   hasPreviousPage = true;
   previousPage = currentPage - 1;
  } else {
   hasPreviousPage = false;
   previousPage = currentPage;
  }
  
  this.pageStartRow = (currentPage -1) * pageRecorders;
  if(hasNextPage == true)
  this.pageEndRow = pageStartRow + 5;
  else{
   this.pageEndRow =this.totalPages;
  }
 }
在HTML中按下一頁或者上一頁的時(shí)候有如下代碼:
<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");
  然后在數(shù)據(jù)庫查詢中有如下代碼:
/**
*查詢總記錄數(shù)
*/
 public int getTotalRows() {
  int totalRows = 0;
  String sql = "select count(*) from employee";//假設(shè)是員工表
  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進(jìn)行數(shù)據(jù)庫操作,你也可以用jdbc進(jìn)行操作,情況類似。

二,根據(jù)總的頁數(shù),當(dāng)前頁
這樣的話構(gòu)造函數(shù)應(yīng)該寫成:
public PageBean(int totalRows,int currentPage) {
  this.totalRows = totalRows;
  this.currentPage = currentPage;
  if(currentPage < 2)
   hasPreviousPage = false;
  else
   hasPreviousPage = true;
  if ((totalRows % pageRecorders) == 0) {
   totalPages = totalRows / pageRecorders;
  } else {
   totalPages = totalRows / pageRecorders + 1;
  }
  if (currentPage < totalPages) {
   hasNextPage = true;
   nextPage = currentPage + 1;
   pageStartRow = (currentPage - 1)*pageRecorders;
   this.pageEndRow = pageStartRow + pageRecorders;
  } else if(currentPage == totalPages){
   pageStartRow = (currentPage - 1)*pageRecorders;
   this.pageEndRow = totalRows;
   hasNextPage = false;
   nextPage = currentPage;
  }
  if(currentPage < 2){
   previousPage = currentPage;
   hasPreviousPage = false;
  }else if(currentPage > 1){
   previousPage = currentPage-1;
   hasPreviousPage = true;
  } 
 }

在action中應(yīng)該寫成
if(currentPage == null){
    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{
    pb = new PageBean(totalRows,Integer.parseInt(currentPage));
    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()));
   }
  session.setAttribute("page",pb);        
  request.setAttribute("admin", "admin");
  request.setAttribute("employee", list);
  return mapping.findForward("showlist");

在jsp中應(yīng)該寫成:
<logic:equal name="page" property="hasNextPage" value="true">
<a href="List.do?currentPage=<bean:write name="page" property="nextPage"/>">
nextPage
</a>
</logic:equal>
<logic:equal name="page" property="hasPreviousPage" value="true">
 | 
<a href="/test/List.do?currentPage=<bean:write name="page" property="previousPage"/>">
PreviousPage
</a>
</logic:equal>
數(shù)據(jù)庫查詢部分依然適用。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
WEB開發(fā)中Struts分頁算法
Java in Action - Struts分頁顯示
Matrix
Flex 2 DataGrid 分頁
SSH分頁技術(shù)
Jsp中分頁功能的實(shí)現(xiàn)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服