分页封装实用工具类及其使用方法

简介:

分页封装实用工具类及其使用方法


作者: javaboy2012
Email:yanek@163.com
qq:    1046011462

 

 

package com.yanek.util;

import java.util.List;

public class PageBean {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

	private int currentpage; // 当前页数
	private int pagesize; // 每页大小
	private int totalPagecount; // 总页数
	private int totalRecordCount; // 总记录数
	private List recordContents; // 当前页记录内容列表

	public int getCurrentpage() {
		return currentpage;
	}

	public int getPagesize() {
		return pagesize;
	}

	public List getRecordContents() {
		return recordContents;
	}

	public int getTotalPagecount() {
		return totalPagecount;
	}

	public int getTotalRecordCount() {
		return totalRecordCount;
	}

	public PageBean(int currentpage, int pagesize, int totalRecordCount, List recordContents) {
		super();
		this.currentpage = currentpage;
		this.pagesize = pagesize;
		this.totalRecordCount = totalRecordCount;
		this.recordContents = recordContents;

		if (totalRecordCount % pagesize == 0) {
			this.totalPagecount = totalRecordCount / pagesize;
		} else {
			this.totalPagecount = totalRecordCount / pagesize + 1;
		}
	}

}




调用方法:

 action里调用:


	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		
                TopicDAO tdao=new TopicDAOImpl();

		String s_pagesize=(String)request.getParameter("pagesize");
		int pagesize=20;
		if (s_pagesize!=null)
		{
			pagesize=Integer.parseInt(s_pagesize);
		}
		int pagecount=1;
		String s_pagecount=(String)request.getParameter("pagecount");
		if (s_pagecount!=null)
		{
			pagecount=Integer.parseInt(s_pagecount);
		}
                
                //得到记录总数		

		long totalcount=tdao.getTopicTotalCount();
		
		
		System.out.println("total:"+totalcount);
		
		List topics=tdao.getTopics(pagesize,pagecount);
		
		for (int i=0;i<topics.size();i++)
		{
			TopicBean tb=(TopicBean)topics.get(i);
			
			System.out.println(tb.getTid()+"-"+tb.getTname());
		}
		
		
		PageBean  pb=new PageBean(pagecount,pagesize, (int)totalcount,topics);

	
		request.setAttribute("topic_pagebean", pb);
		
		return mapping.findForward("topic_list");
	}


jsp页面调用示例:

  <%
  
	    PageBean  pb=(PageBean)request.getAttribute("topic_pagebean");
	    List topics=pb.getRecordContents();
   %>
  

<div class="cont">

<center><h2>记录列表</h2></center><br/><br/>
	<div class="list">
	
<form name="topic"  action="topic.do" method="get">


      <input type="hidden" name="method" value="list" />
      <input type="hidden" name="pagesize" value="<%=pb.getPagesize()%>" />


	  <table width="100%" border="0">
        <tr class="white">
          <th class="sl1" width="30%">标题</th>
          <th width="30%" class="sl1">描述</th>
          <th class="sl1" width="25%">ID </th>
          <th width="15%"> 操作</th>
        </tr>
        
      
      	<% 
      	
      	for (int i=0;i<topics.size();i++)
		{
			TopicBean tb=(TopicBean)topics.get(i);
			%>
	
			<%
			 String classname="";
			 if (i%2==1) 
			 {
			  classname="white";
			 }
		
			%>
			<tr class="<%=classname %>">
	          <td><%=tb.getTname() %></td>
	          <td><%=tb.getTdesc() %></td>
	          <td>
				
				<%=tb.getTid() %>
			  </td>
	          <td><a href='/topic.do?method=view&tid=<%=tb.getTid() %>'>[查看]</a></td>
	        </tr>
			
			
			<%
			
		}
		%>
		
      	
	        
	     	
	        
		
	        <tr>
		<td colspan="6">
		<p align="center">
		<br/>
		
		共<%=pb.getTotalRecordCount() %>个记录,共<%=pb.getTotalPagecount() %>页,每页<%=pb.getPagesize() %>个 ,当前第<%=pb.getCurrentpage() %>页
	
		
		
		 <%
   if (pb.getCurrentpage()==1)
   {
     
     out.print("首页  上页 ");
   }
   else
   {
     %>
      <a href="topic.do?method=list&pagecount=1&pagesize=<%=pb.getPagesize() %>">首页</a>
      <a href="topic.do?method=list&pagecount=<%=pb.getCurrentpage()-1%>&pagesize=<%=pb.getPagesize() %>">上页</a>
     <%
     
   }
   %>
   
    <%
   if (pb.getCurrentpage()==pb.getTotalPagecount())
   {
     
     out.print("下一页  尾页 ");
   }
   else
   {
     %>
  
  <a href="topic.do?method=list&pagecount=<%=pb.getCurrentpage()+1 %>&pagesize=<%=pb.getPagesize() %>">下一页 </a>
  <a href="topic.do?method=list&pagecount=<%=pb.getTotalPagecount() %>&pagesize=<%=pb.getPagesize() %>">  尾页</a>
     <%
     
   }
   %> 
		
		
		
		转到第 <input type="text" name="pagecount" value="<%=pb.getCurrentpage() %>" size="4"> 页
		
		 <input type="submit" value="GO" size="4">
		
		</td>
	</tr>
	
	   
		
	
	
      </table></form>
	  
	 
  </div>
</div>


action配置

<action path="/topic" type="com.myweb.web.action.TopicAction" scope="request"  parameter="method">
			<forward name="topic_list" path="/WEB-INF/pages/test/test/topic_list.jsp"/>
</action>

访问方式: http://192.168.0.1/topic.do?method=list&pagecount=1&pagesize=10


 

目录
相关文章
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16725 61
|
7天前
|
域名解析 人工智能 Linux
|
1天前
|
人工智能 安全 网络安全
|
15天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
云安全 数据采集 人工智能
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2257 7
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1185 2
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3754 22
|
19天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。