SSH整合过程.docx
《SSH整合过程.docx》由会员分享,可在线阅读,更多相关《SSH整合过程.docx(27页珍藏版)》请在冰豆网上搜索。
SSH整合过程
SSH整合
SSH整合前应该先熟悉各个框架的单独使用,至少要了解各自的xml配置。
环境:
struts-2.3.15.1,spring-3.2.4,hibernate-4.2.3,myeclipse10,JDK-1.6,Mysql
Jar包:
struts-2.3.15.1:
sturts2空项目下的所有jar包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar
其他
struts2-spring-plugin-2.3.15.1
commons-logging-1.1.3
aopalliance-1.0
spring-3.2.4:
spring-aop-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-jdbc-3.2.4.RELEASE.jar
spring-orm-3.2.4.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
hibernate-4.2.3:
lib-required下的所有jar包
c3p0-0.9.2.1.jar
hibernate-c3p0-4.2.3.Final.jar
hibernate-entitymanager-4.2.3.Final.jar
1.新建web项目
2.以简单的登录验证为例
3.建立login.jsp,login_success.jsp,login_error.jsp
4.引入struts2
(1)web.xml配置
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
(2)src下的struts.xml配置
/login_success.jsp
/login_error.jsp
(3)测试struts2是否能够通过
5.引入spring
(1)配置web.xml,添加listener
org.springframework.web.context.ContextLoaderListener
--告知springconfiglocation的存储位置-->
contextConfigLocation
classpath:
beans.xml
(2)配置src下的beans.xml
将spring的ApplicationContext.xml拷贝至src下,删除里面的配置项,只保留下面内容
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans.xsd"
>
6.引入hibernate
(1)配置beans.xml
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
jee="http:
//www.springframework.org/schema/jee"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
p="http:
//www.springframework.org/schema/p"
xmlns:
util="http:
//www.springframework.org/schema/util"
xmlns:
tool="http:
//www.springframework.org/schema/tool"
xmlns:
context="http:
//www.springframework.org/schema/context"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop.xsd
http:
//www.springframework.org/schema/jee
http:
//www.springframework.org/schema/jee/spring-jee.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context.xsd
http:
//www.springframework.org/schema/util
http:
//www.springframework.org/schema/util/spring-util.xsd
http:
//www.springframework.org/schema/tool
http:
//www.springframework.org/schema/tool/spring-tool.xsd"
default-lazy-init="true"default-autowire="byName">
--springjdbc配置数据源Mysql-->
mysql:
//localhost:
3306/test"/>
--hibernate.cfg.xmlSpringconfig-->
--connection-->
--hibernate自身属性-->
true
true
org.hibernate.dialect.MySQLDialect
thread
--映射文件
com/forwor/ssh/entity/xml/User.hbm.xml
-->
--注入-->
--aop-->
--transaction-->
--advice-->
--事务处理-->
PROPAGATION_REQUIRED
--hibernate4必须配置为开启事务否则getCurrentSession()获取不到-->
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED,-java.lang.Exception
--autoproxy自动创建代理-->
*Manager
transactionInterceptor
7.类图
以用户登录验证为基础的测试
包
类或接口
方法或属性
com.accp.action
LoginAction
Useruser
UserManageruserManager
Stringlogin()
com.accp.model
User
intid
Stringusername
Stringpassword
com.accp.dao
UserDao
booleanexist()
com.accp.dao.impl
UserDaoImpl
SessionFactorysessionFactory
booleanexist()
com.accp.service
UserManager
Stringlogin()
com.accp.service.impl
UserManagerImpl
UserDaouserDao
Stringlogin()
流程控制
8.注意事项
(1)login.jsp输入内容的name为user.usernameuser.password时,LoginAction中应该为user生成相应的get/set方法。
(2)通过spring注入的属性,应该生成相应的get/set方法。
(3)如果登录时网页出现错误nestedtransactionsnotsupported,一般错误出在UserDaoImpl的exist()方法的session.beginTransaction()这一行。
这是因为getCurrentSession()没有获得session。
先检查beans.xml中事务管理配置,看看是否在UserManager中login方法或者UserDao的exist()方法上开启事务。
然后检查UserDaoImpl的属性sessionFactory有没有get/set方法。
检查session执行对应的数据库操作后,有没有提交事务(session.getTmit())。
如果会报以上错误,在beans.xml的hibernate配置中加入thread。
如果还是不能解决问题,请google或baidu一下。
(4)当前测试的代码只是完成了用户登录验证功能,只是将hibernate的SessionFactory通过spring注入,数据库操作使用的是原生的sql语句,没有使用实体。
如果需要测试,请自己完成实体类xml或者使用HibernateAnnotation。
(5)如果操作中出现其他错误,请google或baidu一下。
9.主要代码
(1)login.jsp
用户名:
密码:
(2)web.xml
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
--告知springconfiglocation的存储位置-->
contextConfigLocation
classpath:
beans.xml
(3)struts.xml
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN"
"http:
//struts.apache.org/dtds/struts-2.3.dtd">