SSH整合教程.docx
《SSH整合教程.docx》由会员分享,可在线阅读,更多相关《SSH整合教程.docx(15页珍藏版)》请在冰豆网上搜索。
SSH整合教程
Ajax+Struts2+Spring2.5+Hibernate3整合文档
1建立Web项目
2加入Spring的jar包
3拷贝spring的包
4改变applicationContext.xml文件的位置至/WEB-INF/
5加入Hibernate的jar包
6使用spring的配置文件
7建立sessionFactory
8建立dataSource
9取消sessionFactory
10选择keepexisting
11删除asm-2.2.3.jar
12加入struts2的jar包
至此SSH所需的包添加完毕,接下来开始配置
13配置web.xm文件
14建立package
15利用逆向工程建立vo和dao
16将自动生成的UserDAO拖至com.ch05.dao包中
17编写service接口
18编写service代码
19编写LoginAction
20配置bean
21加入struts.xml
22编写登陆页面
23测试
Username:
scott
Password:
tiger
跳转至success.jsp,说明登陆成功
24加入事务管理
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
context="http:
//www.springframework.org/schema/context"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
http:
//www.springframework.org/schema/contexthttp:
//www.springframework.org/schema/context/spring-context-2.5.xsd
http:
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
http:
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd">
加入transactionManager
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
配置切面逻辑
adviceid="txAdvice"transaction-manager="transactionManager">
attributes>
methodname="save*"propagation="REQUIRED"/>
methodname="login*"read-only="true"/>
attributes>
advice>
配置AOP
config>
pointcutid="txPt"expression="execution(*com.ch05.service.*.*(..))"/>
advisoradvice-ref="txAdvice"pointcut-ref="txPt"/>
config>
测试
publicvoidsaveUser(Useruser){
//TODOAuto-generatedmethodstub
this.userDao.save(user);
Useruser2=newUser();
user2.setUsername("sssssssssssssssssssssssssssssssssssssssssssssss");
user2.setPassword("123");
this.userDao.save(user2);
}
编写RegistAction
publicStringexecute(){
Useruser=newUser();
user.setUsername(username);
user.setPassword(password);
this.userService.saveUser(user);
returnSUCCESS;
}
配置applicationContext.xml和struts.xml
编写regist.jsp
只有原来的用户,插入的2条用户都没有成功,说明事务发挥了作用
25加入DWR
在WEB-INF下加入dwr.xml文件
在web.xml中配置dwr
dwr-invoker
org.directwebremoting.servlet.DwrServlet
debug
true
dwr-invoker
/dwr/*
配置dwr