SSH整合过程Word格式文档下载.docx
《SSH整合过程Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《SSH整合过程Word格式文档下载.docx(27页珍藏版)》请在冰点文库上搜索。
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配置
<
filter>
<
filter-name>
struts2<
/filter-name>
filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<
/filter-class>
<
/filter>
filter-mapping>
url-pattern>
/*<
/url-pattern>
/filter-mapping>
(2)src下的struts.xml配置
packagename="
default"
namespace="
/"
extends="
struts-default"
>
actionname="
login"
class="
com.accp.action.LoginAction"
method="
resultname="
success"
/login_success.jsp<
/result>
error"
/login_error.jsp<
/action>
/package>
(3)测试struts2是否能够通过
5.引入spring
(1)配置web.xml,添加listener
listener>
listener-class>
org.springframework.web.context.ContextLoaderListener<
/listener-class>
/listener>
!
--告知springconfiglocation的存储位置-->
context-param>
param-name>
contextConfigLocation<
/param-name>
param-value>
classpath:
beans.xml<
/param-value>
/context-param>
(2)配置src下的beans.xml
将spring的ApplicationContext.xml拷贝至src下,删除里面的配置项,只保留下面内容
?
xmlversion="
1.0"
encoding="
UTF-8"
beansxmlns="
http:
//www.springframework.org/schema/beans"
xmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans.xsd"
>
/beans>
6.引入hibernate
(1)配置beans.xml
jee="
//www.springframework.org/schema/jee"
tx="
//www.springframework.org/schema/tx"
aop="
//www.springframework.org/schema/aop"
p="
//www.springframework.org/schema/p"
util="
//www.springframework.org/schema/util"
tool="
//www.springframework.org/schema/tool"
context="
//www.springframework.org/schema/context"
//www.springframework.org/schema/beans/spring-beans.xsd
//www.springframework.org/schema/tx
//www.springframework.org/schema/tx/spring-tx.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop.xsd
//www.springframework.org/schema/jee
//www.springframework.org/schema/jee/spring-jee.xsd
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context.xsd
//www.springframework.org/schema/util
//www.springframework.org/schema/util/spring-util.xsd
//www.springframework.org/schema/tool
//www.springframework.org/schema/tool/spring-tool.xsd"
default-lazy-init="
true"
default-autowire="
byName"
--springjdbc配置数据源Mysql-->
beanid="
dataSource"
org.springframework.jdbc.datasource.DriverManagerDataSource"
propertyname="
driverClassName"
value="
com.mysql.jdbc.Driver"
/>
<
url"
jdbc:
mysql:
//localhost:
3306/test"
username"
user"
password"
123456"
/bean>
--hibernate.cfg.xmlSpringconfig-->
sessionFactory"
org.springframework.orm.hibernate4.LocalSessionFactoryBean"
--connection-->
reflocal="
/>
/property>
--hibernate自身属性-->
hibernateProperties"
props>
propkey="
hibernate.show_sql"
true<
/prop>
hibernate.format_sql"
hibernate.dialect"
org.hibernate.dialect.MySQLDialect<
hibernate.current_session_context_class"
thread<
/props>
--映射文件
mappingResources"
list>
value>
com/forwor/ssh/entity/xml/User.hbm.xml<
/value>
/list>
-->
--注入-->
userDao"
com.accp.dao.impl.UserDaoImpl"
--aop-->
--transaction-->
userManager"
com.accp.service.impl.UserManagerImpl"
--advice-->
transactionManager"
org.springframework.orm.hibernate4.HibernateTransactionManager"
--事务处理-->
transactionInterceptor"
org.springframework.transaction.interceptor.TransactionInterceptor"
transactionAttributes"
register"
PROPAGATION_REQUIRED<
--hibernate4必须配置为开启事务否则getCurrentSession()获取不到-->
get*"
PROPAGATION_REQUIRED,readOnly<
find*"
select*"
query*"
sync*"
finish*"
add*"
insert*"
edit*"
update*"
save*"
remove*"
delete*"
login*"
*"
PROPAGATION_REQUIRED,-java.lang.Exception<
--autoproxy自动创建代理-->
ProxyCreator"
org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
beanNames"
*Manager<
interceptorNames"
transactionInterceptor<
loginAction"
scope="
prototype"
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
com.accp.service
UserManager
com.accp.service.impl
UserManagerImpl
UserDaouserDao
流程控制
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配置中加入<
。
如果还是不能解决问题,请google或baidu一下。
(4)当前测试的代码只是完成了用户登录验证功能,只是将hibernate的SessionFactory通过spring注入,数据库操作使用的是原生的sql语句,没有使用实体。
如果需要测试,请自己完成实体类xml或者使用HibernateAnnotation。
(5)如果操作中出现其他错误,请google或baidu一下。
9.主要代码
(1)login.jsp
formaction="
login.action"
post"
用户名:
inputtype="
text"
name="
user.username"
br>
密码:
user.password"
submit"
提交"
reset"
重置"
/form>
(2)web.xml
web-appversion="
2.5"
xmlns="
display-name>
/display-name>
welcome-file-list>
welcome-file>
index.jsp<
/welcome-file>
/welcome-file-list>
/web-app>
(3)struts.xml
?
DOCTYPEstrutsPUBLIC
"
-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN"
//struts.apache.org/dtds/struts-2.3.dtd"
struts>
constantname="
struts.enable.DynamicMethodInvocation"
false"