请大侠粘点代码上来让我copy吧
32 个解决方案
#1
#2
你所有数据都有了,分页应该很简单啊,一页多少条,当前是第几页,总共多少条,计算一下就可以按照索引取数据了。
#4
for(int i=fromIndex;i<toIndex && i<list.size();i++)
{
list.get(i);
}
#5
session存一个list对象,根据页号提取相应的数据 ,例如:第2页没页10条,就取list.get(11) -- list.get(20)
把所有数据都放在session里边是很占资源的,如果数据超过几万的话会非常慢的
一般是通过sql语句查询出指定页号的数据
sql server用top N
oracle用 rownum
hibernate有相应的api的
自己去搜 sql 分页 吧
把所有数据都放在session里边是很占资源的,如果数据超过几万的话会非常慢的
一般是通过sql语句查询出指定页号的数据
sql server用top N
oracle用 rownum
hibernate有相应的api的
自己去搜 sql 分页 吧
#6
我现在在jsp页面已经有能拿到一个list了
只要做一个分页就好了
可我就是不会,头疼
只要做一个分页就好了
可我就是不会,头疼
#7
建议用SQL分页
#8
分页有真分页和假分页两种,假分页即把所有符合要求得数据从数据库中取出后在代码中实现分页,而真分页则是取出数据库中适当的数据实现分页,分页的代码最好能组件化,这样才能重复利用。。。具体代码也比较复杂,你可以在网上search一下。。应该有的。。。
#9
要想那样分页的话,直接使用Velocity标签吧
#10
看看我的不过是ssh的
以下分页的代码部分出自javaeye的robbin先生的:
原帖:http://www.javaeye.com/topic/14657
package com.javaeye.common.util;
import java.util.List;
public class PaginationSupport {
public final static int PAGESIZE = 30;
private int pageSize = PAGESIZE;
private List items;
private int totalCount;
private int[] indexes = new int[0];
private int startIndex = 0;
public PaginationSupport(List items, int totalCount) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(0);
}
public PaginationSupport(List items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
setPageSize(pageSize);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if (totalCount > 0) {
this.totalCount = totalCount;
int count = totalCount / pageSize;
if (totalCount % pageSize > 0)
count++;
indexes = new int[count];
for (int i = 0; i < count; i++) {
indexes[i] = pageSize * i;
}
} else {
this.totalCount = 0;
}
}
public int[] getIndexes() {
return indexes;
}
public void setIndexes(int[] indexes) {
this.indexes = indexes;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
if (totalCount <= 0)
this.startIndex = 0;
else if (startIndex >= totalCount)
this.startIndex = indexes[indexes.length - 1];
else if (startIndex < 0)
this.startIndex = 0;
else {
this.startIndex = indexes[startIndex / pageSize];
}
}
public int getNextIndex() {
int nextIndex = getStartIndex() + pageSize;
if (nextIndex >= totalCount)
return getStartIndex();
else
return nextIndex;
}
public int getPreviousIndex() {
int previousIndex = getStartIndex() - pageSize;
if (previousIndex < 0)
return 0;
else
return previousIndex;
}
}
以下分页的代码部分出自javaeye的robbin先生的:
原帖:http://www.javaeye.com/topic/14657
package com.javaeye.common.util;
import java.util.List;
public class PaginationSupport {
public final static int PAGESIZE = 30;
private int pageSize = PAGESIZE;
private List items;
private int totalCount;
private int[] indexes = new int[0];
private int startIndex = 0;
public PaginationSupport(List items, int totalCount) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(0);
}
public PaginationSupport(List items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
setPageSize(pageSize);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if (totalCount > 0) {
this.totalCount = totalCount;
int count = totalCount / pageSize;
if (totalCount % pageSize > 0)
count++;
indexes = new int[count];
for (int i = 0; i < count; i++) {
indexes[i] = pageSize * i;
}
} else {
this.totalCount = 0;
}
}
public int[] getIndexes() {
return indexes;
}
public void setIndexes(int[] indexes) {
this.indexes = indexes;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
if (totalCount <= 0)
this.startIndex = 0;
else if (startIndex >= totalCount)
this.startIndex = indexes[indexes.length - 1];
else if (startIndex < 0)
this.startIndex = 0;
else {
this.startIndex = indexes[startIndex / pageSize];
}
}
public int getNextIndex() {
int nextIndex = getStartIndex() + pageSize;
if (nextIndex >= totalCount)
return getStartIndex();
else
return nextIndex;
}
public int getPreviousIndex() {
int previousIndex = getStartIndex() - pageSize;
if (previousIndex < 0)
return 0;
else
return previousIndex;
}
}
#11
抽象业务类
Java代码
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
Java代码
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
#12
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
#13
我做的一个项目就有大量分页,hibernate有个方法 指定取出多少条记录 并且从第几条开始都可以指定
比如一次20条 在页面就好办了 直接取这个list 并且在后台取出总条数 就可以控制上一页下一页
比如一次20条 在页面就好办了 直接取这个list 并且在后台取出总条数 就可以控制上一页下一页
#14
用户在web层构造查询条件detachedCriteria,和可选的startIndex,调用业务bean的相应findByCriteria方法,返回一个PaginationSupport的实例ps。
ps.getItems()得到已分页好的结果集
ps.getIndexes()得到分页索引的数组
ps.getTotalCount()得到总结果数
ps.getStartIndex()当前分页索引
ps.getNextIndex()下一页索引
ps.getPreviousIndex()上一页索引
正如他们说的在查询时如果detachedCriteria有order属性,就会出问题上网查了一下,找到了解决的办法
就是利用java的反射机制先把order属性去掉,获得总记录数后,在把order属性添加回去,修改一下
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
// 先去掉Order部分
List orderEntrys = null;
Field field = null;
CriteriaImpl impl = (CriteriaImpl) criteria;
try {
field = CriteriaImpl.class.getDeclaredField("orderEntries");
field.setAccessible(true);// 这是关键:)
orderEntrys = (List) field.get(impl);
field.set(criteria, new ArrayList());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// 获取总记录数
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult())
.intValue();
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
double mo = Double.parseDouble(String.valueOf(criteria.setProjection(
Projections.sum("incExpMn")).uniqueResult()));
System.out.println(mo);
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
DecimalFormat df = new DecimalFormat("###############0.00");
System.out.println(df.format(mo));
// 再恢复Order部分
List innerOrderEntries = null;
try {
innerOrderEntries = (List) field.get(criteria);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int i = 0; i < orderEntrys.size(); i++) {
innerOrderEntries.add(orderEntrys.get(i));
}
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
在action中调用的部分代码如下:
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(IncomeExpenses.class);
detachedCriteria.add(Restrictions.eq("systemUser", sysUser)).add(Restrictions.disjunction())
.addOrder(Order.desc("incExpId"));
int pageSize = 0;
try {
//pageSize是在通过获取在web.xml中设置的参数
pageSize = Integer
.parseInt(request.getSession().getServletContext().getInitParameter("pageSize"));
} catch (Exception e) {
pageSize = 10;
}
int pageNumber = 0;
//pageNumber是通过获得displaytag控件传过来的参数获得的
String paramName = (new ParamEncoder("lifo").encodeParameterName(TableTagParameters.PARAMETER_PAGE));
try {
pageNumber = Integer.parseInt(request.getParameter(paramName));
} catch (Exception e) {
pageNumber = 1;
}
int startIndex = 0;
if (pageNumber == 1) {
startIndex = 0;
} else {
startIndex = pageSize * (pageNumber - 1);
}
PaginationSupport ps = mangagerNewPagin.findPageByCriteria(detachedCriteria, pageSize, startIndex);
ps.getItems()得到已分页好的结果集
ps.getIndexes()得到分页索引的数组
ps.getTotalCount()得到总结果数
ps.getStartIndex()当前分页索引
ps.getNextIndex()下一页索引
ps.getPreviousIndex()上一页索引
正如他们说的在查询时如果detachedCriteria有order属性,就会出问题上网查了一下,找到了解决的办法
就是利用java的反射机制先把order属性去掉,获得总记录数后,在把order属性添加回去,修改一下
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
// 先去掉Order部分
List orderEntrys = null;
Field field = null;
CriteriaImpl impl = (CriteriaImpl) criteria;
try {
field = CriteriaImpl.class.getDeclaredField("orderEntries");
field.setAccessible(true);// 这是关键:)
orderEntrys = (List) field.get(impl);
field.set(criteria, new ArrayList());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// 获取总记录数
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult())
.intValue();
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
double mo = Double.parseDouble(String.valueOf(criteria.setProjection(
Projections.sum("incExpMn")).uniqueResult()));
System.out.println(mo);
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
DecimalFormat df = new DecimalFormat("###############0.00");
System.out.println(df.format(mo));
// 再恢复Order部分
List innerOrderEntries = null;
try {
innerOrderEntries = (List) field.get(criteria);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int i = 0; i < orderEntrys.size(); i++) {
innerOrderEntries.add(orderEntrys.get(i));
}
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
在action中调用的部分代码如下:
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(IncomeExpenses.class);
detachedCriteria.add(Restrictions.eq("systemUser", sysUser)).add(Restrictions.disjunction())
.addOrder(Order.desc("incExpId"));
int pageSize = 0;
try {
//pageSize是在通过获取在web.xml中设置的参数
pageSize = Integer
.parseInt(request.getSession().getServletContext().getInitParameter("pageSize"));
} catch (Exception e) {
pageSize = 10;
}
int pageNumber = 0;
//pageNumber是通过获得displaytag控件传过来的参数获得的
String paramName = (new ParamEncoder("lifo").encodeParameterName(TableTagParameters.PARAMETER_PAGE));
try {
pageNumber = Integer.parseInt(request.getParameter(paramName));
} catch (Exception e) {
pageNumber = 1;
}
int startIndex = 0;
if (pageNumber == 1) {
startIndex = 0;
} else {
startIndex = pageSize * (pageNumber - 1);
}
PaginationSupport ps = mangagerNewPagin.findPageByCriteria(detachedCriteria, pageSize, startIndex);
#15
在需要分页的页面中调用displaytag的部分代码如下:
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%
String context = request.getContextPath();
pageContext.setAttribute("ctx", context);
%>
<style>
@import url("${ctx}/css/screen.css");
</style>
<display:table name="${requestScope['listInfo']}" id="lifo"
partialList="true" size="${requestScope['total']}"
requestURI="/incomeExpenses.do" export="true"
pagesize="${initParam.pageSize}">
<display:column property="incExpId" sortable="true" title="ID" />
<display:column property="userName" title="名字" />
<display:column property="incExpTime" sortable="true" title="时间" />
<display:column property="incExpTyp" sortable="true" title="开支类型" />
<display:column property="incExpDesc" title="开支描述" />
<display:column property="incExpMn" sortable="true" title="开支金额" />
<display:column title="(编辑删除备注)">
<html:link
action="/incomeExpenses.do?method=delete&incExpId=${lifo.incExpId}">删除</html:link>
<html:link
action="/incomeExpenses.do?method=edit&incExpId=${lifo.incExpId}">编辑</html:link>
<html:link onclick="disIncExpRemark('${lifo.incExpRemark}');"
href="#">备注</html:link>
</display:column>
</display:table>
还有是让displaytag的table显示中文,必须添加一个资源文件displaytag_zh_CN.properties内容如下:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
basic.msg.empty_list = \u6CA1\u6709\u67E5\u8BE2\u7684\u6570\u636E\uFF0C\u8BF7\u91CD\u65B0\u67E5\u8BE2
export.banner = <div class="exportlinks">\u5BFC\u51FA\u4E3A\u5176\u5B83\u683C\u5F0F: {0}</div>
export.csv = true
export.excel = true
# if set, file is downloaded instead of opened in the browser window
#export.[mymedia].filename=
export.excel.class = org.displaytag.export.excel.ExcelHssfView
export.pdf = true
export.pdf.class = org.displaytag.export.DefaultPdfExportView
export.rtf = true
export.rtf.class = org.displaytag.export.DefaultRtfExportView
#sort.behavior=list
#sort.amount=list
#basic.empty.showtable=true
#basic.msg.empty_list=No results matched your criteria.
#paging.banner.placement=top
#paging.banner.onepage=<span class="pagelinks"></span>
export.types = csv excel xml pdf rtf
export.xml = true
paging.banner.first = <span class="pagelinks">[\u7B2C\u4E00\u9875/\u4E0A\u4E00\u9875] {0}[<a href="{3}">\u4E0B\u4E00\u9875</a>/<a href="{4}">\u6700\u540E\u4E00\u9875</a>]</span>
paging.banner.full = <span class="pagelinks">[<a href="{1}">\u7B2C\u4E00\u9875</a>/<a href="{2}">\u4E0A\u4E00\u9875</a>]{0}[<a href="{3}">\u4E0B\u4E00\u9875/<a href="{4}">\u6700\u540E\u4E00\u9875</a>]</span>
paging.banner.last = <span class="pagelinks">[<a href="{1}">\u7B2C\u4E00\u9875</a>/<a href="{2}">\u4E0A\u4E00\u9875</a>]{0} [\u4E0B\u4E00\u9875/\u6700\u540E\u4E00\u9875]</span>
paging.banner.some_items_found = <span class="pagebanner">{0}\u6761\u8BB0\u5F55\uFF0C\u663E\u793A{2}\u5230{3}\u6761\u8BB0\u5F55 .</span>
displaytag的组件是用组件自己封装的表格来分页显示数据的。
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%
String context = request.getContextPath();
pageContext.setAttribute("ctx", context);
%>
<style>
@import url("${ctx}/css/screen.css");
</style>
<display:table name="${requestScope['listInfo']}" id="lifo"
partialList="true" size="${requestScope['total']}"
requestURI="/incomeExpenses.do" export="true"
pagesize="${initParam.pageSize}">
<display:column property="incExpId" sortable="true" title="ID" />
<display:column property="userName" title="名字" />
<display:column property="incExpTime" sortable="true" title="时间" />
<display:column property="incExpTyp" sortable="true" title="开支类型" />
<display:column property="incExpDesc" title="开支描述" />
<display:column property="incExpMn" sortable="true" title="开支金额" />
<display:column title="(编辑删除备注)">
<html:link
action="/incomeExpenses.do?method=delete&incExpId=${lifo.incExpId}">删除</html:link>
<html:link
action="/incomeExpenses.do?method=edit&incExpId=${lifo.incExpId}">编辑</html:link>
<html:link onclick="disIncExpRemark('${lifo.incExpRemark}');"
href="#">备注</html:link>
</display:column>
</display:table>
还有是让displaytag的table显示中文,必须添加一个资源文件displaytag_zh_CN.properties内容如下:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
basic.msg.empty_list = \u6CA1\u6709\u67E5\u8BE2\u7684\u6570\u636E\uFF0C\u8BF7\u91CD\u65B0\u67E5\u8BE2
export.banner = <div class="exportlinks">\u5BFC\u51FA\u4E3A\u5176\u5B83\u683C\u5F0F: {0}</div>
export.csv = true
export.excel = true
# if set, file is downloaded instead of opened in the browser window
#export.[mymedia].filename=
export.excel.class = org.displaytag.export.excel.ExcelHssfView
export.pdf = true
export.pdf.class = org.displaytag.export.DefaultPdfExportView
export.rtf = true
export.rtf.class = org.displaytag.export.DefaultRtfExportView
#sort.behavior=list
#sort.amount=list
#basic.empty.showtable=true
#basic.msg.empty_list=No results matched your criteria.
#paging.banner.placement=top
#paging.banner.onepage=<span class="pagelinks"></span>
export.types = csv excel xml pdf rtf
export.xml = true
paging.banner.first = <span class="pagelinks">[\u7B2C\u4E00\u9875/\u4E0A\u4E00\u9875] {0}[<a href="{3}">\u4E0B\u4E00\u9875</a>/<a href="{4}">\u6700\u540E\u4E00\u9875</a>]</span>
paging.banner.full = <span class="pagelinks">[<a href="{1}">\u7B2C\u4E00\u9875</a>/<a href="{2}">\u4E0A\u4E00\u9875</a>]{0}[<a href="{3}">\u4E0B\u4E00\u9875/<a href="{4}">\u6700\u540E\u4E00\u9875</a>]</span>
paging.banner.last = <span class="pagelinks">[<a href="{1}">\u7B2C\u4E00\u9875</a>/<a href="{2}">\u4E0A\u4E00\u9875</a>]{0} [\u4E0B\u4E00\u9875/\u6700\u540E\u4E00\u9875]</span>
paging.banner.some_items_found = <span class="pagebanner">{0}\u6761\u8BB0\u5F55\uFF0C\u663E\u793A{2}\u5230{3}\u6761\u8BB0\u5F55 .</span>
displaytag的组件是用组件自己封装的表格来分页显示数据的。
#16
使用displaytag的组件,还有个是pager组件可以自己定义分页的类型不一定是表格。
#17
#18
你看看我的代码没有,已经做了处理了
#19
这么多,一看就晕乎乎的,
#20
学习中。。。。。。。。。。
#21
分页什么时候变得这么难懂了。晕乎乎的。
list存那么多数据到页面,实在不可取。
list存那么多数据到页面,实在不可取。
#22
貌似太复杂了,头就看晕了,不过对于数据库多达数百万的纪录怎么实现分批到数据库取纪录,而不是一次取完,那太消耗内存了,
#23
#24
阿拉的就是在数据库端分页的。
#25
public static final String splitPages2(int thePage, String queryString, int count,
int pageLength)
{
int maxPage = count / pageLength + 1;
int prevPage = thePage - 1;
if (prevPage < 1)
{
prevPage = 1;
}
int nextPage = thePage + 1;
int showMaxPage = thePage + 6;
int spacePage = 1;
if (thePage > 6)
{
spacePage = thePage - 5;
}
if (showMaxPage < 10)
{
showMaxPage = 10;
}
//<p class="fy"><a class="back" href="">上一页</a><a href="">01</a><a href="">02</a><a href="">03</a><a class="next" href="">下一页</a></p>
StringBuffer sb = new StringBuffer();
sb.append("<p class=\"fy\"> <a href=\""+queryString+"\">第一页</a>");
sb.append("<a class=\"back\" href=\"" + queryString + "&pagex=" + prevPage
+ "\">上一页</a>");
for (int i = spacePage; i < showMaxPage && i <= maxPage; i++)
{
String fontColor = "black";
if (i == thePage)
{
fontColor = "red";
}
sb.append("<a href=\"" + queryString + "&pagex=" + i
+ "\"><font color=" + fontColor + ">" + addzero(i, 2)
+ "</font></a>");
}
sb.append("<a class=\"next\" href=\"" + queryString + "&pagex=" + nextPage
+ "\">下一页</a>");
sb.append(" <a href=\"" + queryString + "&pagex=" + maxPage
+ "\">最后页</a>");
sb.append("</p>");
return sb.toString();
}
#26
int thePage, String queryString, int count,int pageLength
thePage:当前页
queryString:链接的参数,
count:数据总数,也就是你list的长度,
pageLength: 每页显示的长度
thePage:当前页
queryString:链接的参数,
count:数据总数,也就是你list的长度,
pageLength: 每页显示的长度
#29
我的比较简单:
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/second.css" type="text/css" rel="stylesheet" rev="stylesheet" media="all" />
<title>新闻内容</title>
<script type="text/javascript">
function listInfos(pageNo)
{
document.getElementById("idPageNum").value=pageNo;
document.getElementById("pageForm").submit();
}
</script>
</head>
<body>
<table width="650" height="490" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<table width="630" border="0" align="center" cellpadding="0" cellspacing="0">
<c:forEach items="${infosList}" var="info">
<tr>
<td height="30" valign="top">
<table width="630" height="30" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="20"><img src="cs_images/secNews.gif" width="19" height="19" /></td>
<td class="newsTab"><a href="#" onClick="window.open('/cs/SerShowNews?id=${info.id}&type=${info.type}','','height=600,width=800,top=60,left=100,toolbar=no,menubar=no,scrollbars=no,resizable=no')">${info.simpleTitle} ${true==info.hasPic?"图":""}</a></td>
<td width="120">${info.formattedDate}</td>
</tr>
</table>
</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
<tr>
<td valign="bottom">
<form action="/cs/SerShowNav" method="POST" id="pageForm">
<input type="hidden" name="pageNum" value="${helper.pageNum}" id="idPageNum">
<table width="300" height="30" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>
<c:forEach begin="1" end="${helper.pageCount}" var="i" step="1">
<c:if test="${i==helper.pageNum}">
<font color="red" size="5">${i}</font>
</c:if>
<c:if test="${i!=helper.pageNum}">
<a href="#" onClick="listInfos(${i})">${i}</a>
</c:if>
</c:forEach>
</td>
<td width="230" valign="middle">共${helper.pageCount}页</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
#30
package com.zzuli.cs.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zzuli.cs.info.CInfo;
import com.zzuli.cs.info.CInfoDao;
public class CSerShowAllNews extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
int pageNum=1;
int pageCount=0;
int itemNum=com.zzuli.cs.common.CCommon.ITEMNUM_IN_ONE_PAGE;
String title=req.getParameter("newsTitle");
if(null==title)
{
title="";
}
try
{
pageNum=Integer.valueOf(req.getParameter("pageNum"));
}
catch (Exception e)
{
}
if(1>pageNum)
{
pageNum=1;
}
pageCount=(CInfoDao.getInfoCountLikeTitle(title, type)+itemNum-1)/itemNum;
java.util.List<CInfo> infosList=CInfoDao.getInfosLikeTitle(title, pageNum, itemNum, type);
req.setAttribute("pageCount", pageCount);
req.setAttribute("pageNum", pageNum);
req.setAttribute("infoTitle", title);
req.setAttribute("infosList", infosList);
req.getRequestDispatcher("/newsList.jsp").forward(req, resp);
}
}
#31
下个分页源码看看就会了``
#32
顶一下。。
#1
#2
你所有数据都有了,分页应该很简单啊,一页多少条,当前是第几页,总共多少条,计算一下就可以按照索引取数据了。
#3
#4
for(int i=fromIndex;i<toIndex && i<list.size();i++)
{
list.get(i);
}
#5
session存一个list对象,根据页号提取相应的数据 ,例如:第2页没页10条,就取list.get(11) -- list.get(20)
把所有数据都放在session里边是很占资源的,如果数据超过几万的话会非常慢的
一般是通过sql语句查询出指定页号的数据
sql server用top N
oracle用 rownum
hibernate有相应的api的
自己去搜 sql 分页 吧
把所有数据都放在session里边是很占资源的,如果数据超过几万的话会非常慢的
一般是通过sql语句查询出指定页号的数据
sql server用top N
oracle用 rownum
hibernate有相应的api的
自己去搜 sql 分页 吧
#6
我现在在jsp页面已经有能拿到一个list了
只要做一个分页就好了
可我就是不会,头疼
只要做一个分页就好了
可我就是不会,头疼
#7
建议用SQL分页
#8
分页有真分页和假分页两种,假分页即把所有符合要求得数据从数据库中取出后在代码中实现分页,而真分页则是取出数据库中适当的数据实现分页,分页的代码最好能组件化,这样才能重复利用。。。具体代码也比较复杂,你可以在网上search一下。。应该有的。。。
#9
要想那样分页的话,直接使用Velocity标签吧
#10
看看我的不过是ssh的
以下分页的代码部分出自javaeye的robbin先生的:
原帖:http://www.javaeye.com/topic/14657
package com.javaeye.common.util;
import java.util.List;
public class PaginationSupport {
public final static int PAGESIZE = 30;
private int pageSize = PAGESIZE;
private List items;
private int totalCount;
private int[] indexes = new int[0];
private int startIndex = 0;
public PaginationSupport(List items, int totalCount) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(0);
}
public PaginationSupport(List items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
setPageSize(pageSize);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if (totalCount > 0) {
this.totalCount = totalCount;
int count = totalCount / pageSize;
if (totalCount % pageSize > 0)
count++;
indexes = new int[count];
for (int i = 0; i < count; i++) {
indexes[i] = pageSize * i;
}
} else {
this.totalCount = 0;
}
}
public int[] getIndexes() {
return indexes;
}
public void setIndexes(int[] indexes) {
this.indexes = indexes;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
if (totalCount <= 0)
this.startIndex = 0;
else if (startIndex >= totalCount)
this.startIndex = indexes[indexes.length - 1];
else if (startIndex < 0)
this.startIndex = 0;
else {
this.startIndex = indexes[startIndex / pageSize];
}
}
public int getNextIndex() {
int nextIndex = getStartIndex() + pageSize;
if (nextIndex >= totalCount)
return getStartIndex();
else
return nextIndex;
}
public int getPreviousIndex() {
int previousIndex = getStartIndex() - pageSize;
if (previousIndex < 0)
return 0;
else
return previousIndex;
}
}
以下分页的代码部分出自javaeye的robbin先生的:
原帖:http://www.javaeye.com/topic/14657
package com.javaeye.common.util;
import java.util.List;
public class PaginationSupport {
public final static int PAGESIZE = 30;
private int pageSize = PAGESIZE;
private List items;
private int totalCount;
private int[] indexes = new int[0];
private int startIndex = 0;
public PaginationSupport(List items, int totalCount) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(0);
}
public PaginationSupport(List items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
setPageSize(pageSize);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if (totalCount > 0) {
this.totalCount = totalCount;
int count = totalCount / pageSize;
if (totalCount % pageSize > 0)
count++;
indexes = new int[count];
for (int i = 0; i < count; i++) {
indexes[i] = pageSize * i;
}
} else {
this.totalCount = 0;
}
}
public int[] getIndexes() {
return indexes;
}
public void setIndexes(int[] indexes) {
this.indexes = indexes;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
if (totalCount <= 0)
this.startIndex = 0;
else if (startIndex >= totalCount)
this.startIndex = indexes[indexes.length - 1];
else if (startIndex < 0)
this.startIndex = 0;
else {
this.startIndex = indexes[startIndex / pageSize];
}
}
public int getNextIndex() {
int nextIndex = getStartIndex() + pageSize;
if (nextIndex >= totalCount)
return getStartIndex();
else
return nextIndex;
}
public int getPreviousIndex() {
int previousIndex = getStartIndex() - pageSize;
if (previousIndex < 0)
return 0;
else
return previousIndex;
}
}
#11
抽象业务类
Java代码
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
Java代码
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
#12
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
/**
* Created on 2005-7-12
*/
package com.javaeye.common.business;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javaeye.common.util.PaginationSupport;
public abstract class AbstractManager extends HibernateDaoSupport {
private boolean cacheQueries = false;
private String queryCacheRegion;
public void setCacheQueries(boolean cacheQueries) {
this.cacheQueries = cacheQueries;
}
public void setQueryCacheRegion(String queryCacheRegion) {
this.queryCacheRegion = queryCacheRegion;
}
public void save(final Object entity) {
getHibernateTemplate().save(entity);
}
public void persist(final Object entity) {
getHibernateTemplate().save(entity);
}
public void update(final Object entity) {
getHibernateTemplate().update(entity);
}
public void delete(final Object entity) {
getHibernateTemplate().delete(entity);
}
public Object load(final Class entity, final Serializable id) {
return getHibernateTemplate().load(entity, id);
}
public Object get(final Class entity, final Serializable id) {
return getHibernateTemplate().get(entity, id);
}
public List findAll(final Class entity) {
return getHibernateTemplate().find("from " + entity.getName());
}
public List findByNamedQuery(final String namedQuery) {
return getHibernateTemplate().findByNamedQuery(namedQuery);
}
public List findByNamedQuery(final String query, final Object parameter) {
return getHibernateTemplate().findByNamedQuery(query, parameter);
}
public List findByNamedQuery(final String query, final Object[] parameters) {
return getHibernateTemplate().findByNamedQuery(query, parameters);
}
public List find(final String query) {
return getHibernateTemplate().find(query);
}
public List find(final String query, final Object parameter) {
return getHibernateTemplate().find(query, parameter);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {
return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
public List findAllByCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.list();
}
}, true);
}
public int getCountByCriteria(final DetachedCriteria detachedCriteria) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return criteria.setProjection(Projections.rowCount()).uniqueResult();
}
}, true);
return count.intValue();
}
}
#13
我做的一个项目就有大量分页,hibernate有个方法 指定取出多少条记录 并且从第几条开始都可以指定
比如一次20条 在页面就好办了 直接取这个list 并且在后台取出总条数 就可以控制上一页下一页
比如一次20条 在页面就好办了 直接取这个list 并且在后台取出总条数 就可以控制上一页下一页
#14
用户在web层构造查询条件detachedCriteria,和可选的startIndex,调用业务bean的相应findByCriteria方法,返回一个PaginationSupport的实例ps。
ps.getItems()得到已分页好的结果集
ps.getIndexes()得到分页索引的数组
ps.getTotalCount()得到总结果数
ps.getStartIndex()当前分页索引
ps.getNextIndex()下一页索引
ps.getPreviousIndex()上一页索引
正如他们说的在查询时如果detachedCriteria有order属性,就会出问题上网查了一下,找到了解决的办法
就是利用java的反射机制先把order属性去掉,获得总记录数后,在把order属性添加回去,修改一下
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
// 先去掉Order部分
List orderEntrys = null;
Field field = null;
CriteriaImpl impl = (CriteriaImpl) criteria;
try {
field = CriteriaImpl.class.getDeclaredField("orderEntries");
field.setAccessible(true);// 这是关键:)
orderEntrys = (List) field.get(impl);
field.set(criteria, new ArrayList());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// 获取总记录数
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult())
.intValue();
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
double mo = Double.parseDouble(String.valueOf(criteria.setProjection(
Projections.sum("incExpMn")).uniqueResult()));
System.out.println(mo);
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
DecimalFormat df = new DecimalFormat("###############0.00");
System.out.println(df.format(mo));
// 再恢复Order部分
List innerOrderEntries = null;
try {
innerOrderEntries = (List) field.get(criteria);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int i = 0; i < orderEntrys.size(); i++) {
innerOrderEntries.add(orderEntrys.get(i));
}
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
在action中调用的部分代码如下:
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(IncomeExpenses.class);
detachedCriteria.add(Restrictions.eq("systemUser", sysUser)).add(Restrictions.disjunction())
.addOrder(Order.desc("incExpId"));
int pageSize = 0;
try {
//pageSize是在通过获取在web.xml中设置的参数
pageSize = Integer
.parseInt(request.getSession().getServletContext().getInitParameter("pageSize"));
} catch (Exception e) {
pageSize = 10;
}
int pageNumber = 0;
//pageNumber是通过获得displaytag控件传过来的参数获得的
String paramName = (new ParamEncoder("lifo").encodeParameterName(TableTagParameters.PARAMETER_PAGE));
try {
pageNumber = Integer.parseInt(request.getParameter(paramName));
} catch (Exception e) {
pageNumber = 1;
}
int startIndex = 0;
if (pageNumber == 1) {
startIndex = 0;
} else {
startIndex = pageSize * (pageNumber - 1);
}
PaginationSupport ps = mangagerNewPagin.findPageByCriteria(detachedCriteria, pageSize, startIndex);
ps.getItems()得到已分页好的结果集
ps.getIndexes()得到分页索引的数组
ps.getTotalCount()得到总结果数
ps.getStartIndex()当前分页索引
ps.getNextIndex()下一页索引
ps.getPreviousIndex()上一页索引
正如他们说的在查询时如果detachedCriteria有order属性,就会出问题上网查了一下,找到了解决的办法
就是利用java的反射机制先把order属性去掉,获得总记录数后,在把order属性添加回去,修改一下
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
final int startIndex) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
// 先去掉Order部分
List orderEntrys = null;
Field field = null;
CriteriaImpl impl = (CriteriaImpl) criteria;
try {
field = CriteriaImpl.class.getDeclaredField("orderEntries");
field.setAccessible(true);// 这是关键:)
orderEntrys = (List) field.get(impl);
field.set(criteria, new ArrayList());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// 获取总记录数
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult())
.intValue();
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
double mo = Double.parseDouble(String.valueOf(criteria.setProjection(
Projections.sum("incExpMn")).uniqueResult()));
System.out.println(mo);
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
DecimalFormat df = new DecimalFormat("###############0.00");
System.out.println(df.format(mo));
// 再恢复Order部分
List innerOrderEntries = null;
try {
innerOrderEntries = (List) field.get(criteria);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int i = 0; i < orderEntrys.size(); i++) {
innerOrderEntries.add(orderEntrys.get(i));
}
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
}, true);
}
在action中调用的部分代码如下:
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(IncomeExpenses.class);
detachedCriteria.add(Restrictions.eq("systemUser", sysUser)).add(Restrictions.disjunction())
.addOrder(Order.desc("incExpId"));
int pageSize = 0;
try {
//pageSize是在通过获取在web.xml中设置的参数
pageSize = Integer
.parseInt(request.getSession().getServletContext().getInitParameter("pageSize"));
} catch (Exception e) {
pageSize = 10;
}
int pageNumber = 0;
//pageNumber是通过获得displaytag控件传过来的参数获得的
String paramName = (new ParamEncoder("lifo").encodeParameterName(TableTagParameters.PARAMETER_PAGE));
try {
pageNumber = Integer.parseInt(request.getParameter(paramName));
} catch (Exception e) {
pageNumber = 1;
}
int startIndex = 0;
if (pageNumber == 1) {
startIndex = 0;
} else {
startIndex = pageSize * (pageNumber - 1);
}
PaginationSupport ps = mangagerNewPagin.findPageByCriteria(detachedCriteria, pageSize, startIndex);
#15
在需要分页的页面中调用displaytag的部分代码如下:
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%
String context = request.getContextPath();
pageContext.setAttribute("ctx", context);
%>
<style>
@import url("${ctx}/css/screen.css");
</style>
<display:table name="${requestScope['listInfo']}" id="lifo"
partialList="true" size="${requestScope['total']}"
requestURI="/incomeExpenses.do" export="true"
pagesize="${initParam.pageSize}">
<display:column property="incExpId" sortable="true" title="ID" />
<display:column property="userName" title="名字" />
<display:column property="incExpTime" sortable="true" title="时间" />
<display:column property="incExpTyp" sortable="true" title="开支类型" />
<display:column property="incExpDesc" title="开支描述" />
<display:column property="incExpMn" sortable="true" title="开支金额" />
<display:column title="(编辑删除备注)">
<html:link
action="/incomeExpenses.do?method=delete&incExpId=${lifo.incExpId}">删除</html:link>
<html:link
action="/incomeExpenses.do?method=edit&incExpId=${lifo.incExpId}">编辑</html:link>
<html:link onclick="disIncExpRemark('${lifo.incExpRemark}');"
href="#">备注</html:link>
</display:column>
</display:table>
还有是让displaytag的table显示中文,必须添加一个资源文件displaytag_zh_CN.properties内容如下:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
basic.msg.empty_list = \u6CA1\u6709\u67E5\u8BE2\u7684\u6570\u636E\uFF0C\u8BF7\u91CD\u65B0\u67E5\u8BE2
export.banner = <div class="exportlinks">\u5BFC\u51FA\u4E3A\u5176\u5B83\u683C\u5F0F: {0}</div>
export.csv = true
export.excel = true
# if set, file is downloaded instead of opened in the browser window
#export.[mymedia].filename=
export.excel.class = org.displaytag.export.excel.ExcelHssfView
export.pdf = true
export.pdf.class = org.displaytag.export.DefaultPdfExportView
export.rtf = true
export.rtf.class = org.displaytag.export.DefaultRtfExportView
#sort.behavior=list
#sort.amount=list
#basic.empty.showtable=true
#basic.msg.empty_list=No results matched your criteria.
#paging.banner.placement=top
#paging.banner.onepage=<span class="pagelinks"></span>
export.types = csv excel xml pdf rtf
export.xml = true
paging.banner.first = <span class="pagelinks">[\u7B2C\u4E00\u9875/\u4E0A\u4E00\u9875] {0}[<a href="{3}">\u4E0B\u4E00\u9875</a>/<a href="{4}">\u6700\u540E\u4E00\u9875</a>]</span>
paging.banner.full = <span class="pagelinks">[<a href="{1}">\u7B2C\u4E00\u9875</a>/<a href="{2}">\u4E0A\u4E00\u9875</a>]{0}[<a href="{3}">\u4E0B\u4E00\u9875/<a href="{4}">\u6700\u540E\u4E00\u9875</a>]</span>
paging.banner.last = <span class="pagelinks">[<a href="{1}">\u7B2C\u4E00\u9875</a>/<a href="{2}">\u4E0A\u4E00\u9875</a>]{0} [\u4E0B\u4E00\u9875/\u6700\u540E\u4E00\u9875]</span>
paging.banner.some_items_found = <span class="pagebanner">{0}\u6761\u8BB0\u5F55\uFF0C\u663E\u793A{2}\u5230{3}\u6761\u8BB0\u5F55 .</span>
displaytag的组件是用组件自己封装的表格来分页显示数据的。
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%
String context = request.getContextPath();
pageContext.setAttribute("ctx", context);
%>
<style>
@import url("${ctx}/css/screen.css");
</style>
<display:table name="${requestScope['listInfo']}" id="lifo"
partialList="true" size="${requestScope['total']}"
requestURI="/incomeExpenses.do" export="true"
pagesize="${initParam.pageSize}">
<display:column property="incExpId" sortable="true" title="ID" />
<display:column property="userName" title="名字" />
<display:column property="incExpTime" sortable="true" title="时间" />
<display:column property="incExpTyp" sortable="true" title="开支类型" />
<display:column property="incExpDesc" title="开支描述" />
<display:column property="incExpMn" sortable="true" title="开支金额" />
<display:column title="(编辑删除备注)">
<html:link
action="/incomeExpenses.do?method=delete&incExpId=${lifo.incExpId}">删除</html:link>
<html:link
action="/incomeExpenses.do?method=edit&incExpId=${lifo.incExpId}">编辑</html:link>
<html:link onclick="disIncExpRemark('${lifo.incExpRemark}');"
href="#">备注</html:link>
</display:column>
</display:table>
还有是让displaytag的table显示中文,必须添加一个资源文件displaytag_zh_CN.properties内容如下:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
basic.msg.empty_list = \u6CA1\u6709\u67E5\u8BE2\u7684\u6570\u636E\uFF0C\u8BF7\u91CD\u65B0\u67E5\u8BE2
export.banner = <div class="exportlinks">\u5BFC\u51FA\u4E3A\u5176\u5B83\u683C\u5F0F: {0}</div>
export.csv = true
export.excel = true
# if set, file is downloaded instead of opened in the browser window
#export.[mymedia].filename=
export.excel.class = org.displaytag.export.excel.ExcelHssfView
export.pdf = true
export.pdf.class = org.displaytag.export.DefaultPdfExportView
export.rtf = true
export.rtf.class = org.displaytag.export.DefaultRtfExportView
#sort.behavior=list
#sort.amount=list
#basic.empty.showtable=true
#basic.msg.empty_list=No results matched your criteria.
#paging.banner.placement=top
#paging.banner.onepage=<span class="pagelinks"></span>
export.types = csv excel xml pdf rtf
export.xml = true
paging.banner.first = <span class="pagelinks">[\u7B2C\u4E00\u9875/\u4E0A\u4E00\u9875] {0}[<a href="{3}">\u4E0B\u4E00\u9875</a>/<a href="{4}">\u6700\u540E\u4E00\u9875</a>]</span>
paging.banner.full = <span class="pagelinks">[<a href="{1}">\u7B2C\u4E00\u9875</a>/<a href="{2}">\u4E0A\u4E00\u9875</a>]{0}[<a href="{3}">\u4E0B\u4E00\u9875/<a href="{4}">\u6700\u540E\u4E00\u9875</a>]</span>
paging.banner.last = <span class="pagelinks">[<a href="{1}">\u7B2C\u4E00\u9875</a>/<a href="{2}">\u4E0A\u4E00\u9875</a>]{0} [\u4E0B\u4E00\u9875/\u6700\u540E\u4E00\u9875]</span>
paging.banner.some_items_found = <span class="pagebanner">{0}\u6761\u8BB0\u5F55\uFF0C\u663E\u793A{2}\u5230{3}\u6761\u8BB0\u5F55 .</span>
displaytag的组件是用组件自己封装的表格来分页显示数据的。
#16
使用displaytag的组件,还有个是pager组件可以自己定义分页的类型不一定是表格。
#17
#18
你看看我的代码没有,已经做了处理了
#19
这么多,一看就晕乎乎的,
#20
学习中。。。。。。。。。。
#21
分页什么时候变得这么难懂了。晕乎乎的。
list存那么多数据到页面,实在不可取。
list存那么多数据到页面,实在不可取。
#22
貌似太复杂了,头就看晕了,不过对于数据库多达数百万的纪录怎么实现分批到数据库取纪录,而不是一次取完,那太消耗内存了,
#23
#24
阿拉的就是在数据库端分页的。
#25
public static final String splitPages2(int thePage, String queryString, int count,
int pageLength)
{
int maxPage = count / pageLength + 1;
int prevPage = thePage - 1;
if (prevPage < 1)
{
prevPage = 1;
}
int nextPage = thePage + 1;
int showMaxPage = thePage + 6;
int spacePage = 1;
if (thePage > 6)
{
spacePage = thePage - 5;
}
if (showMaxPage < 10)
{
showMaxPage = 10;
}
//<p class="fy"><a class="back" href="">上一页</a><a href="">01</a><a href="">02</a><a href="">03</a><a class="next" href="">下一页</a></p>
StringBuffer sb = new StringBuffer();
sb.append("<p class=\"fy\"> <a href=\""+queryString+"\">第一页</a>");
sb.append("<a class=\"back\" href=\"" + queryString + "&pagex=" + prevPage
+ "\">上一页</a>");
for (int i = spacePage; i < showMaxPage && i <= maxPage; i++)
{
String fontColor = "black";
if (i == thePage)
{
fontColor = "red";
}
sb.append("<a href=\"" + queryString + "&pagex=" + i
+ "\"><font color=" + fontColor + ">" + addzero(i, 2)
+ "</font></a>");
}
sb.append("<a class=\"next\" href=\"" + queryString + "&pagex=" + nextPage
+ "\">下一页</a>");
sb.append(" <a href=\"" + queryString + "&pagex=" + maxPage
+ "\">最后页</a>");
sb.append("</p>");
return sb.toString();
}
#26
int thePage, String queryString, int count,int pageLength
thePage:当前页
queryString:链接的参数,
count:数据总数,也就是你list的长度,
pageLength: 每页显示的长度
thePage:当前页
queryString:链接的参数,
count:数据总数,也就是你list的长度,
pageLength: 每页显示的长度
#27
#28
#29
我的比较简单:
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/second.css" type="text/css" rel="stylesheet" rev="stylesheet" media="all" />
<title>新闻内容</title>
<script type="text/javascript">
function listInfos(pageNo)
{
document.getElementById("idPageNum").value=pageNo;
document.getElementById("pageForm").submit();
}
</script>
</head>
<body>
<table width="650" height="490" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<table width="630" border="0" align="center" cellpadding="0" cellspacing="0">
<c:forEach items="${infosList}" var="info">
<tr>
<td height="30" valign="top">
<table width="630" height="30" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="20"><img src="cs_images/secNews.gif" width="19" height="19" /></td>
<td class="newsTab"><a href="#" onClick="window.open('/cs/SerShowNews?id=${info.id}&type=${info.type}','','height=600,width=800,top=60,left=100,toolbar=no,menubar=no,scrollbars=no,resizable=no')">${info.simpleTitle} ${true==info.hasPic?"图":""}</a></td>
<td width="120">${info.formattedDate}</td>
</tr>
</table>
</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
<tr>
<td valign="bottom">
<form action="/cs/SerShowNav" method="POST" id="pageForm">
<input type="hidden" name="pageNum" value="${helper.pageNum}" id="idPageNum">
<table width="300" height="30" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>
<c:forEach begin="1" end="${helper.pageCount}" var="i" step="1">
<c:if test="${i==helper.pageNum}">
<font color="red" size="5">${i}</font>
</c:if>
<c:if test="${i!=helper.pageNum}">
<a href="#" onClick="listInfos(${i})">${i}</a>
</c:if>
</c:forEach>
</td>
<td width="230" valign="middle">共${helper.pageCount}页</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
#30
package com.zzuli.cs.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zzuli.cs.info.CInfo;
import com.zzuli.cs.info.CInfoDao;
public class CSerShowAllNews extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
int pageNum=1;
int pageCount=0;
int itemNum=com.zzuli.cs.common.CCommon.ITEMNUM_IN_ONE_PAGE;
String title=req.getParameter("newsTitle");
if(null==title)
{
title="";
}
try
{
pageNum=Integer.valueOf(req.getParameter("pageNum"));
}
catch (Exception e)
{
}
if(1>pageNum)
{
pageNum=1;
}
pageCount=(CInfoDao.getInfoCountLikeTitle(title, type)+itemNum-1)/itemNum;
java.util.List<CInfo> infosList=CInfoDao.getInfosLikeTitle(title, pageNum, itemNum, type);
req.setAttribute("pageCount", pageCount);
req.setAttribute("pageNum", pageNum);
req.setAttribute("infoTitle", title);
req.setAttribute("infosList", infosList);
req.getRequestDispatcher("/newsList.jsp").forward(req, resp);
}
}
#31
下个分页源码看看就会了``
#32
顶一下。。