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

news/2024/7/3 12:31:30 标签: string, exception, bean, null, vector, class
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

 分页使用
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 == class="tags" href="/tags/NULL.html" title=null>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 = class="tags" href="/tags/NULL.html" title=null>null;
   String content = class="tags" href="/tags/NULL.html" title=null>null;
   Notice class="tags" href="/tags/BEAN.html" title=bean>bean = class="tags" href="/tags/NULL.html" title=null>null;
   if(noticeVector!=class="tags" href="/tags/NULL.html" title=null>null&&noticeVector.size()!=0){
   for (int i = 0; i < noticeVector.size(); i++) {
    class="tags" href="/tags/BEAN.html" title=bean>bean = (Notice) noticeVector.elementAt(i);
    title = class="tags" href="/tags/BEAN.html" title=bean>bean.getTitle();
    //title = new String(title.getBytes("ISO-8859-1"), "GB2312");

    content = class="tags" href="/tags/BEAN.html" title=bean>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="
      + class="tags" href="/tags/BEAN.html" title=bean>bean.getID() + "/">Edit</a> " + "</td>");
    out
      .println("<td>"
        + "<a href=/"checkpower.do?functiontype=noticedelete&id="
        + class="tags" href="/tags/BEAN.html" title=bean>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!=class="tags" href="/tags/NULL.html" title=null>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!=class="tags" href="/tags/NULL.html" title=null>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());
   


http://www.niftyadmin.cn/n/1425650.html

相关文章

Pig安装部署与实例

安装包地址&#xff1a;https://mirrors.tuna.tsinghua.edu.cn/apache/pig/ 前提&#xff1a;Hadoop安装成功 pig安装部署&#xff1a; 1.将准备好的安装包上传到虚拟机rz 2.查看是否上传成功 3.解压缩 命令&#xff1a;tar xf pig-0.13.0.tar.gz 4.将解压缩后的文件移动到…

flash

FLASH导入到库的快捷键CTRLR 矢量渐变图导入flash在flash里面填充渐变的地方&#xff1f; EPS,AI格式请用Illustrator编辑 矢量&#xff08;很好很好很好&#xff09; http://www.veeqi.com/vector/people/ http://www.zcool.com.cn/vector/ 教学 http://www.7880.com/in…

Java网络编程(java.net )

http://java.ccidnet.com/images/java/javanet/Java网络编程&#xff08;java.net &#xff09;事实上网络编程简单的理解就是两台计算机相互通讯数据而已&#xff0c;Java SDK 提供一些相对简单的Api来完成这些工作。Socket就是其中之一&#xff0c;这些Api 存在与java.net …

hive实验

利用Hive对某网站的用户数据进行分析。 1.创建dblab数据库 命令&#xff1a;create database dblab; 2. 在dblab数据库下创建bigdata_user表&#xff0c;该表中的各种属性如下&#xff1a; 字段名 类型 id int uid string item_id string behavior_type …

zookeeper安装部署与使用

Zookeeper安装部署 安装包下载地址&#xff1a;https://apache.org/dist/zookeeper/ 1.下载安装包然后上传到主节点rz&#xff0c;centos上可使用 wget 地址 2.解压缩&#xff1a;tar xf 安装包 3.移动到/opt目录下mv zookeeper-3.4.12 /opt 4.修改目录权限 命令&#xff…

十进制十六进制转换

以321为例&#xff1a;先记住1、16、256、4096这几个数字&#xff0c;即16的平方、16的立方等等。 321/256 1 余 65 》写下1 65/16 4 余 1 》写下14 1/1 1 余 0 》写下141 即十进制321等于十六进制141 1.比如&#xff1a;216是16进制&#xff0c;转10进制: &#xff1d;2*…

ubuntu连接xshell出现的问题

使用Ubuntu连接xshell的时候出现以下问题&#xff1a; 解决方法&#xff1a; 在ubuntu的管理员用户下&#xff0c;安装openssh-server&#xff08;不是管理员在命令前使用sudo&#xff09; 安装成功后&#xff0c;查看是否有启动ssh,使用ps -e | grep ssh查看&#xff0c;如果…

ABC类IP地址

A类IP地址一个A类IP地址由1字节的网络地址和3字节主机地址组成&#xff0c;网络地址的最高位 必须是"0"&#xff0c; 地址范围从1.0.0.0 到126.0.0.0。可用的A类网络有126个&#xff0c;每个 网络能容纳1亿多个主机B类IP地址  一个B类IP地址由2个字节的网络地址和…