分类: LINUX
2008-04-23 16:02:42
关键字:struts2 spring hibernate 分页显示
J2EE系统中分页显示,修改dao层,service层,action层,两个分页类文件,代码如下:
dao层接口:
package com.last999.im.news.dao;
import java.util.*;
import com.last999.im.news.entity.KindEntity;
import com.last999.im.news.web.PageTool;
public interface KindEntityDao{
public KindEntity get(String uuid);
public void save(KindEntity kindEntity);
public void update(KindEntity kindEntity);
public void delete(String uuid);
public void delete(KindEntity kindEntity);
public List
public KindEntity findKindEntityByName(String name);
public List
public int getRows();
}
实现类:
package com.last999.im.news.dao.impl;
import java.util.*;
import com.last999.im.news.dao.KindEntityDao;
import com.last999.im.news.entity.KindEntity;
import com.last999.im.news.web.PageTool;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class KindEntityDaoHibernate extends HibernateDaoSupport implements KindEntityDao{
public KindEntity get(String uuid){
return (KindEntity)getHibernateTemplate().get(KindEntity.class,uuid);
}
public void save(KindEntity kindEntity){
getHibernateTemplate().save(kindEntity);
}
public void update(KindEntity kindEntity){
getHibernateTemplate().saveOrUpdate(kindEntity);
}
public void delete(KindEntity kindEntity){
getHibernateTemplate().delete(kindEntity);
}
public void delete(String uuid){
getHibernateTemplate().delete((KindEntity)getHibernateTemplate().get(KindEntity.class,uuid));
}
public List findAll(){
return getHibernateTemplate().find("from KindEntity");
}
public KindEntity findKindEntityByName(String name){
List k1 = getHibernateTemplate().find("from KindEntity as ke where ke.name = ?",name);
if (k1 != null && k1.size() == 1){
return (KindEntity)k1.get(0);
}
return null;
}
public int getRows(){
return getHibernateTemplate().find("from KindEntity").size();
}
public List getKindEntity(PageTool pageInfo){
final int size1=pageInfo.getSize();
final int startRow=(pageInfo.getCur()-1)*pageInfo.getSize();
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public List doInHibernate(Session session) throws HibernateException{
Query query = session.createQuery("from KindEntity");
query.setFirstResult(startRow);
query.setMaxResults(size1);
return query.list();
}
}
);
}
}
SERVICE层接口:
package com.last999.im.news.service;
import java.util.List;
import com.last999.im.news.web.PageTool;
import com.last999.im.news.entity.KindEntity;
public interface KindEntityManage{
public void addKindEntity(KindEntity entity);
public List findAllKind();
public int getRows();
public List getKindEntity(PageTool pageInfo);
}
实现类:
package com.last999.im.news.service.impl;
import java.util.List;
import com.last999.im.news.web.PageTool;
import com.last999.im.news.entity.KindEntity;
import com.last999.im.news.service.KindEntityManage;
import com.last999.im.news.dao.KindEntityDao;
public class KindEntityManageImpl implements KindEntityManage{
private KindEntityDao kindEntityDao;
public KindEntityDao getKindEntityDao(){
return this.kindEntityDao;
}
public void setKindEntityDao(KindEntityDao kindEntityDao) {
this.kindEntityDao=kindEntityDao;
}
public void addKindEntity(KindEntity kindEntity){
this.getKindEntityDao().save(kindEntity);
}
public List findAllKind(){
return this.getKindEntityDao().findAll();
}
public int getRows(){
return this.getKindEntityDao().getRows();
}
public List getKindEntity(PageTool pageInfo){
return this.getKindEntityDao().getKindEntity(pageInfo);
}
}
action类:
package com.last999.im.news.action;
import java.util.List;
import com.last999.im.news.web.PageTool;
import com.last999.im.news.web.PageMaker;
import com.last999.im.news.entity.KindEntity;
import com.last999.im.news.service.KindEntityManage;
public class KindViewAction extends SafeStruts2{
private List kindList;
private KindEntity kindEntity;
private KindEntityManage kindEntityManage;
private int ppp;
private PageMaker pageMaker;
private String pager;
public String getPager(){
return this.pager;
}
public void setPager(String pager) {
this.pager=pager;
}
public PageMaker getPageMaker(){
return this.pageMaker;
}
public void setPageMaker(PageMaker pageMaker) {
this.pageMaker=pageMaker;
}
public int getPpp(){
return this.ppp;
}
public void setPpp(int ppp) {
this.ppp=ppp;
}
public KindEntityManage getKindEntityManage(){
return this.kindEntityManage;
}
public void setKindEntityManage(KindEntityManage kindEntityManage) {
this.kindEntityManage=kindEntityManage;
}
public KindEntity getKindEntity(){
return this.kindEntity;
}
public void setKindEntity(KindEntity kindEntity) {
this.kindEntity=kindEntity;
}
public List getKindList(){
return this.kindList;
}
public void setKindList(List kindList) {
this.kindList=kindList;
}
public String kindView(){
PageTool pageInfo = new PageTool();
pageInfo.setCur(this.getPpp());
pageInfo.setTotal(this.getKindEntityManage().getRows());
setKindList(this.getKindEntityManage().getKindEntity(pageInfo));
PageMaker pageMaker = new PageMaker();
setPager(pageMaker.makeList(pageInfo,"kindView.action?ppp="));
return SUCCESS;
}
}
分页辅助工具类:
package com.last999.im.news.web;
public class PageTool{
private int cur=1;
private int size=6;
private int total=1;
public int getCur(){
return this.cur;
}
public void setCur(int cur) {
this.cur=cur;
}
public int getSize(){
return this.size;
}
public void setSize(int size) {
this.size=size;
}
public int getTotal(){
return this.total;
}
public void setTotal(int total) {
this.total=total;
}
public PageTool(){}
public PageTool(int cur,int size,int total){
this.cur=cur;
this.size=size;
this.total=total;
}
public PageTool(int size){
this.size=size;
this.cur=1;
this.total=1;
}
}
PageMaker.java
package com.last999.im.news.web;
import com.last999.im.news.web.PageTool;
public class PageMaker {
public PageMaker(){}
public String makeList(PageTool pageinfo,String url){
int linkNumber=10;
int halfLinkNumber=5;
String pageStyle="";
String pageTable="
");";
//a string that will be decretion to show
StringBuffer pagelist=new StringBuffer("");
//get current page.
int curpage=pageinfo.getCur();
int pagenumber=pageinfo.getTotal()/pageinfo.getSize()+1;
//make it's pre,next page
int prepage=curpage-1;
prepage=prepage<=0?1:prepage;
int nextpage=curpage+1;
nextpage=nextpage>=pagenumber?pagenumber:nextpage;
//finger out how many page total
pagelist.append(pageStyle);
pagelist.append(pageTable);
pagelist.append("Record:"+pageinfo.getTotal()+" ");
//frist page
//pagelist.append("|< ");
//pagelist.append("< ");
if (pagenumber<=linkNumber){
for(int i=1;i<=pagenumber;i++){
if(i==curpage)
pagelist.append(""+i+" ");
else
pagelist.append(""+i+" ");
}
}else{
int begin=curpage-halfLinkNumber;
begin=(begin<=0)?1:begin;
begin=((halfLinkNumber+curpage)>pagenumber) ? (pagenumber-linkNumber+1):begin;
for(int i=begin;i<=linkNumber+begin-1;i++){
if(i==curpage){
pagelist.append(""+i+" ");
pagelist.append("\n");
}
else{
pagelist.append(" ");
pagelist.append("\n");
}
}
}
//pagelist.append("> ");
//pagelist.append(">| ");
pagelist.append("Page:"+pagenumber+"
return pagelist.toString();
}
}
KindEntity.java
package com.last999.im.news.entity;
/**
* @hibernate.class
* table="kind"
* dynamic-update="true"
*/
public class KindEntity extends Persistent{
private String title;
private String content;
private String parentId;
private String description;
/**
* @hibernate.property
*/
public String getDescription(){
return this.description;
}
public void setDescription(String description) {
this.description=description;
}
private String em1;
private String em2;
private String em3;
/**
* @hibernate.property
*/
public String getEm3(){
return this.em3;
}
public void setEm3(String em3) {
this.em3=em3;
}
/**
* @hibernate.property
*/
public String getEm2(){
return this.em2;
}
public void setEm2(String em2) {
this.em2=em2;
}
/**
* @hibernate.property
*/
public String getEm1(){
return this.em1;
}
public void setEm1(String em1) {
this.em1=em1;
}
/**
* @hibernate.property
*/
public String getParentId(){
return this.parentId;
}
public void setParentId(String parentId) {
this.parentId=parentId;
}
/**
* @hibernate.property
* @hibernate.column name="content" sql-type="TEXT"
*/
public String getContent(){
return this.content;
}
public void setContent(String content) {
this.content=content;
}
/**
* @hibernate.property
*/
public String getTitle(){
return this.title;
}
public void setTitle(String title) {
this.title=title;
}
}
为什么没有在页面接收的代码:
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
showkind
|
starming0332009-05-23 21:32:37
您好~我毕业设计用SSH2做得 能给我个完整的例子 参考下么 最好是有关联表的那种~先谢谢了 邮箱 050705033@163.com
coldwinter0112008-11-01 05:32:41
真对不起啊,最近出来找工作了,所有得东西都在原来得机器得硬盘里,没法拿出来了,我到北京混,没法带着机器,真是不好意思了!!