搭建SSH框架
Struts+hibernater+spring架构(myeclipse)
右击,首先加入spring,加入hibernater,再加入struts2
复制jar包(把tomcat发布的jar 复制到 lib) [copy以后可以移除struts2的引用]删除 asm2.2.3.jar, antlr-2.7.2.jar,
Copy struts2-spring-plugin-2.1.8.1.jar 根据版本选择
一:
改web。Xml文件
1 改web。Xml文件 2 <!-- 给context设置applicationContext.xml的位置 --> 3 <context-param> 4 <param-name>contextConfigLocation</param-name> 5 <param-value>classpath*:applicationContext*.xml</param-value> 6 </context-param> 7 <!-- 配置监听器 在启动程序的时候就加载spring配置文件并且会帮定到servlteContext里面 --> 8 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 9 </listener>
二:
在spring 里面 加入action 的注入
三:
<!-- struts2 注入必须要加入 属性 scope="prototype"
默认 spring的对象都是单例模式的对象
加上scope="prototype" 那么就变成多实例了
struts2action不能做单例的 因为里面有form的值
-->
<bean id="fwxxaction" class="com.web.action.FwxxAction" scope="prototype">
<property name="fwxxservcie" ref="fwxxServcie"></property>
</bean>
四:-----------------…配置事务 看最下面 --------------------------------
五:----------------------…加入 过滤器-前提条件你配置事务-----------------------
1 <filter> 2 <filter-name>sessionFilter</filter-name> 3 <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>sessionFilter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping>
注意:在action里面调用service 一定是一个接口的引用
如 UserService 而不是UserServiceImpl
六: Action 的配置
<!-- class 改为spring 配置文件中的id 那么就注入成功了 -->
<action name="fwxx" class="fwxxaction">
<result>/index.jsp</result>
</action>
下面为事务的配置 记得 改 那个表达式的位置哦……..
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 配置事物管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置实物传播特性 --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="zhuangzhang" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 哪些类的那些方法参与 事物 --> <aop:config> <aop:pointcut id="allService" expression="execution(* service.*.*(..))"/>//表达式 <aop:advisor pointcut-ref="allService" advice-ref="txadvice"/> </aop:config> </beans>
最后最关键的部分是 加入 struts2-spring-plugin-2.1.8.1.jar 注意版本号跟你的导入的struts2的版本相同
struts2-spring-plugin-2.1.8.1.jar 下载地址:http://i.cnblogs.com/Files.aspx 里面找。