五、整合Struts2、Extjs将struts-2.1.6\lib目录下的struts2-codebehind-plugin-2.1.6.jar、struts2-core-2.1.6.jar、struts2-spring-plugin-2.1.6.jar、xwork-2.1.2.jar、ognl-2.6.11.jar、freemarker-2.3.13.jar、commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar复制到项目WebContent\lib下。另外还需jsonplugin-0.34.jar包,使struts2action返回json类型。新建一个entity接口,action层需使用该接口调用实体主键。packagecom.xy.entity;publicinterfaceBaseEntity{publiclonggetId();}修改Demo实体,使其实现BaseEntity接口packagecom.xy.entity.demo;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.SequenceGenerator;importjavax.persistence.Table;importjavax.persistence.TableGenerator;importcom.xy.entity.BaseEntity;@Entity@Table(name=HSSEA_DEMO)publicclassDemoimplementsBaseEntity{@Id@GeneratedValue(strategy=GenerationType.TABLE,generator=hssea_demo)@TableGenerator(name=hssea_demo,table=OBJECT_ID,pkColumnName=NAME,valueColumnName=VALUE,pkColumnValue=HSSEA_DEMO_PK,initialValue=1,allocationSize=1)@SequenceGenerator(name=hssea_demo_seq,sequenceName=seq_hssea_demo,allocationSize=1)privatelongid;@Column(name=CODE)privateStringcode;@Column(name=DESCRIPTION)privateStringdescription;publiclonggetId(){returnid;}publicvoidsetId(longid){this.id=id;}publicStringgetCode(){returncode;}publicvoidsetCode(Stringcode){this.code=code;}publicStringgetDescription(){returndescription;}publicvoidsetDescription(Stringdescription){this.description=description;}}增加struts2与extjs交互抽象类,完成分页、排序、增删改查。packagecom.xy.action;importjava.util.HashSet;importjava.util.List;importjava.util.Set;importorg.apache.log4j.Logger;importorg.apache.struts2.config.ParentPackage;importorg.apache.struts2.config.Result;importorg.apache.struts2.config.Results;importorg.hibernate.criterion.Criterion;importorg.hibernate.criterion.Order;importorg.springframework.context.annotation.Scope;importorg.springframework.stereotype.Controller;importcom.googlecode.jsonplugin.JSONResult;importcom.opensymphony.xwork2.ActionSupport;importcom.xy.service.BaseServiceForHibernate;importcom.xy.entity.BaseEntity;/***struts2与extjs交互抽象类*完成基本增删改查分页等功能,子类继承时只需设置entity中属性的set。*@authorxy*@date2009-12-31*@paramTextendsBaseEntity,XextendsBaseHibernateDaoTentity和service*/@Controller@Scope(prototype)@ParentPackage(value=json-default)@Results({@Result(name=success,value=,type=JSONResult.class,params={ignoreHierarchy,false})})publicabstractclassStruts2ExtjsBaseActionTextendsBaseEntity,XextendsBaseServiceForHibernateTextendsActionSupport{staticLoggerlogger=Logger.getLogger(Struts2ExtjsBaseAction.class.getName());privatestaticfinallongserialVersionUID=2863769505963567954L;//Extjs使用,成功与否,返回信息protectedbooleansuccess=false;protectedStringmessage;//Extjs使用,分页、排序信息protectedintstart;protectedintlimit;protectedStringsort;protectedStringdir;//Service类protectedXservice;//返回的数据列表和记录总数protectedListTlist;protectedinttotal;//entity对象,用于提交保存,更新前取个别数据protectedTentity;//记录选中记录的idprotectedStringids;/***取列表*/publicStringlist(){logger.debug(取数据列表!start:+this.start+,limit:+this.limit+,sort:+this.sort+,dir:+this.dir);SetCriterioncriterionSet=newHashSetCriterion();SetOrderorderSet=newHashSetOrder();if(this.dir.equals(ASC)){orderSet.add(Order.asc(this.sort));}else{orderSet.add(Order.desc(this.sort));}list=service.query(criterionSet,orderSet,this.start,this.limit);total=service.totalSize(criterionSet);success=true;returnSUCCESS;}/***根据ID取数据*/publicStringget(){logger.debug(根据id取数据明细!id:+entity.getId());entity=service.get(entity.getId());success=true;returnSUCCESS;}/***保存*/publicStringsave(){logger.debug(保存数据!entity:+entity);service.save(entity);success=true;returnSUCCESS;}/***删除*/publicStringdelete(){logger.debug(根据ids删除数据!ids:+ids);for(Strings:ids.split(,)){service.delete(Long.parseLong(s));}success=true;returnSUCCESS;}//对象T内部信息的读取均在子类中进行//getter,setter方法//struts2需get方法将数据转换成jsonpublicListTgetList(){returnlist;}publicintgetTotal(){returntotal;}publicbooleanisSuccess(){returnsuccess;}publicStringgetMessage(){returnmessage;}//读取extjs传来的分页和排序信息publicvoidsetStart(intstart){this.start=start;}publicvoidsetLimit(intlimit){this.limit=limit;}publicvoidsetSort(Stringsort){this.sort=sort;}publicvoidsetDir(Stringdir){this.dir=dir;}//读取extjs传来的选中记录idpublicvoidsetIds(Stringids){this.ids=ids;}publicTgetEntity(){returnentity;}}新增DemoAction类packagecom.xy.action.demo;importjavax.annotation.Resource;importorg.apache.struts2.config.Namespace;importcom.xy.action.Struts2ExtjsBaseAction;importcom.xy.entity.demo.Demo;importcom.xy.service.demo.DemoService;@Namespace(value=/demo)publicclassDemoActionextendsStruts2ExtjsBaseActionDemo,DemoService{privatestaticfinallongserialVersionUID=7796054923782630546L;//注入service@Resource(name=demoService)publicvoidsetDemoService(DemoServicedemoService){super.service=demoService;}publicDemoAction(){super.entity=newDemo();}//entity对象中的set方法publicvoidsetId(longid){entity.setId(id);}publicvoidsetCode(Stringcode){entity.setCode(code);}publicvoidsetDescriptio