首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

怎样用java实现分页显示,该怎么解决

2012-01-30 
怎样用java实现分页显示我用hibernate+struts写了一个数据库查询的系统,用java怎样才能实现分页显示?能给

怎样用java实现分页显示
我用hibernate   +   struts   写了一个数据库查询的系统,用java怎样才能实现分页显示?能给出例子更好!!!本人正在全力学习java,希望高手多多指点!!!本人联系方式:weokme@163.com     QQ:526881969   谢谢!

[解决办法]

/**
* 根据最大页数、开始记录数返回对应记录集
* @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;
}
[解决办法]
你要计算的,我给你个类似的例子,是我以前写的,也是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>

热点排行