怎样用java实现分页显示

zookie 2007-01-13 10:06:11
我用hibernate + struts 写了一个数据库查询的系统,用java怎样才能实现分页显示?能给出例子更好!!!本人正在全力学习java,希望高手多多指点!!!本人联系方式:weokme@163.com QQ:526881969 谢谢!
...全文
3861 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
GZ工作 2009-09-20
  • 打赏
  • 举报
回复
display标签是一个相当强大的标签
<% String uri= "showLog.do "; %>
<display:table name= "rsList " width= "100% " requestURI= " <%=uri%> " pagesize= " <%=((SessionContainer)session.getAttribute(Constant.SESSION_CONTAINER_KEY)).getPageSize()%> " uid= "item " cellspacing= '1 ' cellpadding= '0 ' >
<display:caption style= "text-align:left; " > 日志列表: </display:caption>
<display:column property= "log_date " title= "时间 " width= "20% " align= "center " />
<display:column property= "log_name " title= "操作人员 " width= "20% " align= "center " />
<display:column property= "log_cont " title= "操作内容 " width= "20% " align= "center " />
<display:column property= "log_part " title= "所操作内容所属模块 " width= "20% " align= "center " />

</display:table>
我听别人说不能用无敌百分号和表达式
应该用EL和JSTL
hg_Currenter 2009-09-07
  • 打赏
  • 举报
回复
不错 顶
hg_Currenter 2009-09-07
  • 打赏
  • 举报
回复
不错 顶
zookie 2007-02-06
  • 打赏
  • 举报
回复
谢谢大家!好人还是多!:-)
zookie 2007-02-05
  • 打赏
  • 举报
回复
加完分啦
jianghu77 2007-02-01
  • 打赏
  • 举报
回复
up
wise_dog 2007-02-01
  • 打赏
  • 举报
回复
写得不错,顶。
lzb1229 2007-02-01
  • 打赏
  • 举报
回复
jsp群2290187 欢迎加入
risa_meng 2007-01-25
  • 打赏
  • 举报
回复
display标签是一个相当强大的标签
<% String uri="showLog.do"; %>
<display:table name="rsList" width="100%" requestURI="<%=uri%>" pagesize="<%=((SessionContainer)session.getAttribute(Constant.SESSION_CONTAINER_KEY)).getPageSize()%>" uid="item" cellspacing='1' cellpadding='0' >
<display:caption style="text-align:left;" >日志列表:</display:caption>
<display:column property="log_date" title="时间" width="20%" align="center" />
<display:column property="log_name" title="操作人员" width="20%" align="center" />
<display:column property="log_cont" title="操作内容" width="20%" align="center" />
<display:column property="log_part" title="所操作内容所属模块" width="20%" align="center" />

</display:table>
zookie 2007-01-25
  • 打赏
  • 举报
回复
怎样给大家加分??
笨沙发 2007-01-25
  • 打赏
  • 举报
回复
不错,顶上去
rmouse_2005 2007-01-23
  • 打赏
  • 举报
回复
可以从数据库里面一次把记录全部取出来,然后每次显示一部分记录
或者显示一次就读取一次数据库..
Qingjian 2007-01-23
  • 打赏
  • 举报
回复
hibernate 里面就有分页的方法都已经封装好了
jianghu77 2007-01-22
  • 打赏
  • 举报
回复
关注
zookie 2007-01-19
  • 打赏
  • 举报
回复
谢谢大家!!!
shenglijay 2007-01-17
  • 打赏
  • 举报
回复
你要计算的,我给你个类似的例子,是我以前写的,也是struts + hibernate

存放信息的实体类
package com.entitys;

import java.io.Serializable;

public class Product implements Serializable{
private String productId;
private String productName;
private int num;
private double price;
private String factoryName;
private String tel;

public Product(){}

public Product(String productId,String productName,int num,double price,String factoryName,String tel){
this.productId = productId;
this.productName = productName;
this.num = num;
this.price = price;
this.factoryName = factoryName;
this.tel = tel;
}

public String getFactoryName() {
return factoryName;
}
public void setFactoryName(String factoryName) {
this.factoryName = factoryName;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}

访问数据库的类
package com.dbaccess;

import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.Icommand.IProduct;
import com.hibernateFactory.HibernateSessionFactory;

public class AccessProduct implements IProduct {
private int currentPage;//当前页面数
private int pageCount;//总页面数

public Collection getAllProduct(int page) {
Session session = getSession();
synchronized (session) {
try{
this.currentPage = page;

String hql = "from Product";
Query query = session.createQuery(hql);

int rows=10; // 每页显示10行

List list = query.list();

int count = list.size(); // 记录总数

this.pageCount=(count+rows-1)/rows; // 根据你的纪录总数,显示行数,来计算分多少页

/*如果当前页面数小于1,则让当前页面数等于1*/
if(this.currentPage<1)
{
this.currentPage=1;
}
/*如果当前页面数大于总页数,则让当前页面数等于总页数*/
if(this.currentPage>this.pageCount)
{
this.currentPage=this.pageCount;
}

List newList;
if((((this.currentPage-1)*rows)+rows)>count)
{
newList = list.subList((this.currentPage-1)*rows, count);
}
else
{
newList = list.subList((this.currentPage-1)*rows, ((this.currentPage-1)*rows)+rows);
}

return newList;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
}

public int getCurrentPage() {
return currentPage;
}

public int getPageCount() {
return pageCount;
}

private Session getSession(){
Session session = null;
try
{
session = HibernateSessionFactory.getSession();
}
catch(Exception e){
e.printStackTrace();
}
return session;
}
}
数据访问对象工厂类
package com.dbfactory;

import com.Icommand.IProduct;
import com.dbaccess.AccessProduct;

public class ProductFactory {

private ProductFactory(){}

public static IProduct getInstance(){
return new AccessProduct();
}
}
业务逻辑类
package com.logic;

import java.util.Collection;

import com.Icommand.IProduct;
import com.dbfactory.ProductFactory;

public class ProductLogic {

private IProduct proDB = ProductFactory.getInstance();
public Collection getAllProduct(int page){
return proDB.getAllProduct(page);
}

public int getCurrentPage(){
return proDB.getCurrentPage();
}

public int getPageCount(){
return proDB.getPageCount();
}
}
Action类
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.struts.action;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.entitys.Product;
import com.logic.ProductLogic;
import com.struts.form.ShowForm;

/**
* MyEclipse Struts
* Creation date: 11-28-2006
*
* XDoclet definition:
* @struts.action path="/show" name="showForm" scope="request"
*/
public class ShowAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ProductLogic logic = new ProductLogic();
int page = 1;
try{
String str = request.getParameter("page");
if(str == null){
page = 1;
}
else
{
page = Integer.parseInt(str);
}
}catch(Exception e){
page = 1;
e.printStackTrace();
}
Collection proList = logic.getAllProduct(page);
if(proList != null && proList.size() >0)
{
request.setAttribute("pro", proList);
request.setAttribute("page", ""+logic.getCurrentPage());
request.setAttribute("pageCount", ""+logic.getPageCount());
return mapping.findForward("product");
}
else
{
request.setAttribute("error", "暂时没有新的产品!");
return mapping.findForward("error");
}
}
}
jsp页面
<TABLE width="100%" class="border" align="center">
<caption><h3>产品信息</h3></caption>
<th>产品ID </th>
<th>产品名称</th>
<th>产品数量</th>
<th>产品价格</th>
<th>生产厂家</th>
<th>联系电话</th>
<th></th>
<logic:iterate id="prolist" name="pro" scope="request">
<tr align="center" onmouseover="onColor(this);" onmouseout="outColor(this);">
<td><bean:write name="prolist" property="productId"/></td>
<td><bean:write name="prolist" property="productName"/></td>
<td><bean:write name="prolist" property="num"/></td>
<td><bean:write name="prolist" property="price"/></td>
<td><bean:write name="prolist" property="factoryName"/></td>
<td><bean:write name="prolist" property="tel"/></td>
<TD><A href="#">查看该产品的详细信息</A></TD>
</tr>
</logic:iterate>
<tr>
<td colspan="7" align="center">
<A href="show.do?page=1">第一页
</A>
<A href='show.do?page=<%=Integer.parseInt((String)request.getAttribute("page"))+1 %>'>下一页
</A>
<A href='show.do?page=<%=Integer.parseInt((String)request.getAttribute("page"))-1 %>'>上一页
</A>
<A href='show.do?page=<%=Integer.parseInt((String)request.getAttribute("pageCount"))%>'>最后页
</A>
</td>
</tr>
</TABLE>
</logic:notEmpty>
</TD>
</TR>
</TABLE>
sole_lodestar 2007-01-17
  • 打赏
  • 举报
回复

/**
* 根据最大页数、开始记录数返回对应记录集
* @param pageSize 最大页数
* @param page 开始记录数
* @return
* @throws Exception
*/
public List getRSofPage(int pageSize,int page) throws Exception {
List retList = new ArrayList();
Session sess = null;
try {
sess = HibernateUtil.currentSession();
Transaction tx = sess.beginTransaction();
Query q = sess
.createQuery("from Channel where ParentId is not 0 order by ParentId ,ChannelId ");
q.setMaxResults( pageSize );
q.setFirstResult( (page - 1) * pageSize );
retList = q.list();
tx.commit();
//log
logger.info("(@@@@@@@@@@@ 根据最大页数、开始记录数返回对应记录集执行正常 @@@@@@@@@@@)");
} catch (HibernateException he) {
//log
logger.error("(@@@@@@@@@@@ 根据最大页数、开始记录数返回对应记录集执行异常 @@@@@@@@@@@)", he);
new org.hibernate.HibernateException("getRSofPage():"+listErrors);
} finally {
try {
HibernateUtil.closeSession();
} catch (HibernateException he) {
new org.hibernate.HibernateException(
"HibernateUtilServlet.closeSession()");
}
}
return retList;
}
llsljh 2007-01-17
  • 打赏
  • 举报
回复
百度~
accpedu 2007-01-17
  • 打赏
  • 举报
回复
关注一下

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧