您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Struts2+spring2+hibernate3整合方案
Struts2+spring2+hibernate3整合方案最近闲来无事可做,于是开始学习struts2。Struts2和struts1、webwork2有什么区别我也不说了,网上有很多这方面的资料。以前在项目中从未使用过struts,一直使用spring+hibernate,现在既然学习了Struts,也不能浪费,于是乎开始琢磨着怎么整合这3个框架。整合原理以spring为容器,管理hibernate的DAO和Struts2的Action。一、准备工作Struts2.06+spring2.5+hibernate3.2+jdk6.0+myeclipse6.0+tomcat5.5+mysql5.0以上是整合的原料。下面以一个注册登陆的例子来开始我们的整合过程。这个例子很简单,下面是它的sql脚本内容:CREATETABLE`user`(`userid`int(11)NOTNULLAUTO_INCREMENT,`username`varchar(20)NOTNULL,`password`varchar(16)NOTNULL,`email`varchar(30)NOTNULL,PRIMARYKEY(`userid`))ENGINE=InnoDBDEFAULTCHARSET=utf-8;二、开始行动包结构可以参考下图1)Struts部分:建立struts.xml和struts.propertiesStruts.xml内容如下:?xmlversion=1.0encoding=UTF-8?!DOCTYPEstrutsPUBLIC-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN=user_curdextends=struts-defaultglobal-results!--下面定义的结果对所有的Action都有效--resultname=exception/error.jsp/result/global-resultsglobal-exception-mappings!--指Action抛出Exception异常时,转入名为exception的结果。--exception-mappingexception=java.lang.Exceptionresult=exception//global-exception-mappingsactionname=Loginclass=LoginActionresultname=success/success.jsp/resultresultname=input/login.jsp/result/actionactionname=Registclass=RegistActionresultname=success/success.jsp/resultresultname=input/regist.jsp/result/action/package/strutsStruts.properties内容如下:struts.devMode=falsestruts.enable.DynamicMethodInvocation=truestruts.i18n.reload=truestruts.ui.theme=simplestruts.locale=zh_CNstruts.i18n.encoding=UTF-8struts.objectFactory=springstruts.objectFactory.spring.autoWire=namestruts.serve.static.browserCache=falsestruts.url.includeParams=none2)建立User.java和User.hbm.xml、jdbc.properties:User.java内容如下:/***//****@authorahref=mailto:flustar2008@163.comflustar/a*@version1.0*Creationdate:Dec23,20071:55:28PM*/packagecom.firstssh.model;importjava.io.Serializable;publicclassUserimplementsSerializable{privateintid;privateStringusername;privateStringpassword;privateStringemail;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicStringgetEmail(){returnemail;}publicvoidsetEmail(Stringemail){this.email=email;}}User.hbm.xml内容?xmlversion=1.0?!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=com.firstssh.modelclassname=Usertable=Useridname=idcolumn=useridgeneratorclass=identity//idpropertyname=usernamecolumn=usernamenot-null=truelength=20/propertyname=passwordcolumn=passwordnot-null=truelength=16/propertyname=emailcolumn=emailnot-null=truelength=30//class/hibernate-mappingjdbc.properties内容如下:datasource.type=mysqldatasource.driverClassName=com.mysql.jdbc.Driverdatasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8datasource.username=rootdatasource.password=123456datasource.maxActive=10datasource.maxIdle=2datasource.maxWait=120000datasource.whenExhaustedAction=1datasource.validationQuery=select1fromdualdatasource.testOnBorrow=truedatasource.testOnReturn=falsec3p0.acquireIncrement=3c3p0.initialPoolSize=3c3p0.idleConnectionTestPeriod=900c3p0.minPoolSize=2c3p0.maxPoolSize=50c3p0.maxStatements=100c3p0.numHelperThreads=10c3p0.maxIdleTime=600hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect#hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialecthibernate.jdbc.batch_size=25hibernate.jdbc.fetch_size=50hibernate.show_sql=truehibernate.connection.release_mode=after_transaction3)Spirng部分:为了清晰把Spring的配置文件拆分成以下几部分applicationContext-dao.xml、appliationContext-service.xml、applicationContext-hibernate.xml、action-servlet.xml。applicationContext-hibernate.xml内容:?xmlversion=1.0encoding=UTF-8?!DOCTYPEbeansPUBLIC-//SPRING//DTDBEAN2.0//EN=propertyConfigurerclass=org.springframework.beans.factory.config.PropertyPlaceholderConfigurerpropertyname=locationslist!--valueWEB-INF/mail.properties/value--valueWEB-INF/jdbc.properties/value!--valueWEB-INF/oscache.properties/value--/list/property/bean!--MailSenderusedbyEmailAdvice--!--beanid=mailSenderclass=org.springframework.mail.javamail.JavaMailSenderImplpropertyname=hostvalue=${mail.host}//bean--beanid=dataSourceclass=com.mchange.v2.c3p0.ComboPooledDataSourcedestroy-method=closedependency-check=nonepropertyname=driverClassvalue${datasource.driverClassName}/value/propertypropertyname=jdbcUrlvalue${datasource.url}/value/propertypropertyname=uservalue${datasource.username}/value/propertypropertyname=passwordvalue${datasource.password}/value/propertypropertyname=acquireIncrementvalue${c3p0.acquireIncrement}/value/propertypropertyname=initialPoolSizevalue${c3p0.initial
本文标题:Struts2+spring2+hibernate3整合方案
链接地址:https://www.777doc.com/doc-5946094 .html