1. 前置条件
引入struts2-spring-plugin-2.0.9.jar
2. web文件中引入spring配置文件装载的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果spring不是默认文件或者不在默认位置(web-inf/classes/),需要设置装载器的参数(这里可以使用通配符*)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
</context-param>
3. 在spring中配置struts配置中用到的aciton(像普通bean那样)
<bean id="bookAction" class="com.sterning.books.web.actions.BooksAction">
<property name="booksService">
<ref bean="booksService"/>
</property>
</bean>
4. 在struts中使用spring配置的bean id作为action(class使用bean id而不是具体类)
<package name="products" extends="struts-default">
<action name="list" class="bookAction" method="list">
<result name="success">/list.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="delete" class="bookAction" method="delete">
<result type="redirect" name="success">list.action</result>
<result type="redirect" name="error">/error.jsp</result>
</action>
</package>
5. 对于spring配置的简化
可以设置beans的default-autowire="byName",让spring帮你建立bean之间的依赖关系,那就不需要在定义bean的时候写<property>标签来显式定义依赖关系,只需要在bean类中增加依赖的bean的set方法即可。当然,如果这样设置的话,会使得配置文件难于理解,复杂的应用还是在配置文件中声明bean的依赖关系比较好。
引入struts2-spring-plugin-2.0.9.jar
2. web文件中引入spring配置文件装载的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果spring不是默认文件或者不在默认位置(web-inf/classes/),需要设置装载器的参数(这里可以使用通配符*)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
</context-param>
3. 在spring中配置struts配置中用到的aciton(像普通bean那样)
<bean id="bookAction" class="com.sterning.books.web.actions.BooksAction">
<property name="booksService">
<ref bean="booksService"/>
</property>
</bean>
4. 在struts中使用spring配置的bean id作为action(class使用bean id而不是具体类)
<package name="products" extends="struts-default">
<action name="list" class="bookAction" method="list">
<result name="success">/list.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="delete" class="bookAction" method="delete">
<result type="redirect" name="success">list.action</result>
<result type="redirect" name="error">/error.jsp</result>
</action>
</package>
5. 对于spring配置的简化
可以设置beans的default-autowire="byName",让spring帮你建立bean之间的依赖关系,那就不需要在定义bean的时候写<property>标签来显式定义依赖关系,只需要在bean类中增加依赖的bean的set方法即可。当然,如果这样设置的话,会使得配置文件难于理解,复杂的应用还是在配置文件中声明bean的依赖关系比较好。