现在的位置: 首页 > 综合 > 正文

自定义标签的分页使用(复杂)

2013年10月18日 ⁄ 综合 ⁄ 共 6090字 ⁄ 字号 评论关闭

 分页使用
1。
<mytag:pagingDisplay />
2。
<tag>
  <name>pagingDisplay</name>
  <tagclass>nm.tag.DisplayTag</tagclass>
  <bodycontent>empty</bodycontent>
  <info> A demo </info>
 </tag>
3。
package nm.tag;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import nm.Notice;
public final class DisplayTag extends TagSupport {
 private static final long serialVersionUID = 1L;
 public int doEndTag() throws JspException {
  JspWriter out = pageContext.getOut();
  HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
  String strPage = request.getParameter("page");
  int page;
  if (strPage == null || strPage.equals(""))
   page = 1;
  else {
   page = Integer.parseInt(strPage);
  }

  try {
   int pageSize = 2;  //设置每页有几条数据
   Vector noticeVector = Notice.search(pageSize, page);   
   out.println("<table width=/"80%/" border=/"1/" align=/"center/"");
   out.println("<tr align=/"center/" bgcolor=/"#CC99CC/">");
   out.println("<th width=/"35%/">标题</th>");
   out.println("<th width=/"35%/">内容</th>");
   out.println("<th width=/"15%/">修改</th>");
   out.println("<th width=/"15%/">删除</th>");
   out.println("</tr>");
   String title = null;
   String content = null;
   Notice bean = null;
   if(noticeVector!=null&&noticeVector.size()!=0){
   for (int i = 0; i < noticeVector.size(); i++) {
    bean = (Notice) noticeVector.elementAt(i);
    title = bean.getTitle();
    //title = new String(title.getBytes("ISO-8859-1"), "GB2312");

    content = bean.getContent();
   // content = new String(content.getBytes("ISO-8859-1"), "GB2312");

    out.println("<tr align=/"center/" bgcolor=/"#CCFF99/">");
    out.println("<td height=/"80/">" + title + "</td>");
    out.println("<td>" + content + "</td>");
    out.println("<td>"
      + "<a href=/"checkpower.do?functiontype=noticeedit&id="
      + bean.getID() + "/">Edit</a> " + "</td>");
    out
      .println("<td>"
        + "<a href=/"checkpower.do?functiontype=noticedelete&id="
        + bean.getID() + "/">Delete</a> " + "</td>");
    out.println("</tr>");
    
   }
   out.println("<tr align=/"right/" bgcolor=/"#CCFF66/">");
   out.println("<th colspan=/"4/">");
   if(page!=-1){
   out.println("<a href=/"noticelistjsp.do?page=-1/">首页</a> ");
   }
   out.println("<a href=/"noticelistjsp.do?page=-2/">上一页</a> ");
  
   out.println("<a href=/"noticelistjsp.do?page=-3/">下一页</a> ");
   if(page!=-4){
   out.println("<a href=/"noticelistjsp.do?page=-4/">尾页</a></th>");
   }
   out.println("</tr>");
   }

   out.println("</table>");
  } catch (Exception ex) {
   throw new JspTagException("IOException:" + ex.toString());
  }

  return super.doEndTag();
 }
}

4。
package nm;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Pageable {
 private int pageSize;
 private int totalRows;
 private int totalPages;
 private static int currentPage;
 private int rowsCount;
 public Pageable(ResultSet rs) {
  try {
   rs.last();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  try {
   this.setTotalRows(rs.getRow());
  } catch (SQLException e1) {
   e1.printStackTrace();
  }
  try {
   rs.beforeFirst();
  } catch (SQLException e2) {
   e2.printStackTrace();
  }
 }

 public void setPageSize(int pageSize) {
  if (pageSize > 0) {
   this.pageSize = pageSize;
  } else {
   this.pageSize = 1;
  }

  this.setTotalPages();
 }

 public void gotoPage(int page) {
  switch (page) {
  case -1:
   this.setCurrentPage(1);
   break;
  case -2:
   if (this.getCurrentPage() != 1) {
    this.setCurrentPage(this.getCurrentPage() - 1);
   } else {
    this.setCurrentPage(1);
   }
   break;
  case -3:
   if (this.getCurrentPage() != this.getTotalPages()) {
    this.setCurrentPage(this.getCurrentPage() + 1);
   } else {
    this.setCurrentPage(this.getTotalPages());
   }
   break;
  case -4:
   this.setCurrentPage(this.getTotalPages());
   break;
  default:
   this.setCurrentPage(page);
  }
 }

 
 public void setCurrentPage(int page) {

  if (page <= 0)
   Pageable.currentPage = 1;
  
  if (page > this.getTotalPages())
   Pageable.currentPage = this.getTotalPages();
  else
   Pageable.currentPage = page;

  
  this.setRowsCount((Pageable.currentPage - 1) * this.getPageSize() + 1);
 }

 public int getCurrentPageRowsCount() {
  if (this.getPageSize() == 0)
   return this.getTotalRows();
  if (this.getTotalRows() == 0)
   return 0;
  if (this.getCurrentPage() != this.getTotalPages())
   return this.getPageSize();

  return this.getTotalRows() - (this.getTotalPages() - 1)
    * this.getPageSize();
 }

 public int getPageSize() {
  return this.pageSize;
 }

 
 public int getTotalRows() {
  return totalRows;
 }

 public void setTotalRows(int totalRows) {
  this.totalRows = totalRows;
 }

 
 public int getRowsCount() {
  return rowsCount;
 }

 public void setRowsCount(int rowsCount) {
  this.rowsCount = rowsCount;
 }

 public int getCurrentPage() {
  return currentPage;
 }

 
 public int getTotalPages() {
  return this.totalPages;
 }

 
 public void setTotalPages() {
  if (this.getTotalRows() == 0) {
   this.totalPages = 0;
  } else if (this.getPageSize() == 0) {
   this.totalPages = 1;
  } else {
   if (this.getTotalRows() % this.getPageSize() != 0)
    this.totalPages = this.getTotalRows() / this.getPageSize() + 1;
   else
    this.totalPages = this.getTotalRows() / this.getPageSize();
  }
 }

 public void pageFirst() throws java.sql.SQLException {
  this.setRowsCount((this.getCurrentPage() - 1) * this.getPageSize() + 1);
 }

 public void pageLast() throws java.sql.SQLException {
  this.setRowsCount((this.getCurrentPage() - 1) * this.getPageSize()
    + this.getCurrentPageRowsCount());
 }

}

 

————————————————————————————

Pageable pgb = new Pageable(rs);
pgb.setPageSize(pageSize);
pgb.gotoPage(page);
if(rs!=null){
   rs.absolute(pgb.getRowsCount());
   }

do {
    if(pgb.getCurrentPageRowsCount()!=0){
     
    id = Integer.parseInt(rs.getString("ID"));
    title = rs.getString("Title");
    content = rs.getString("Content");
    noticeVector.add(new Notice(id, title, content));
    }

    i++;
   } while (rs.next() && i < pgb.getCurrentPageRowsCount());
   

(GOOD)简化一下,我只想使用分页辅助类Pageable.java,不想使用自定义标签。
1。Pageable.java
2
如下
Pageable pgb = new Pageable(rs);
pgb.setPageSize(10);
pgb.gotoPage(2);
if(rs!=null){
rs.absolute(pgb.getRowsCount());
}
do {
if(pgb.getCurrentPageRowsCount()!=0){
     id = Integer.parseInt(rs.getString("ID"));
    title = rs.getString("Title");
    content = rs.getString("Content");
    noticeVector.add(new Notice(id, title, content));
    }

    i++;
   } while (rs.next() && i < pgb.getCurrentPageRowsCount());
   

抱歉!评论已关闭.