When Spring and Struts2 integration action automatically injected into the problem

   When Struts and the Spring Framework integration, originally handed over by the process of instantiating objects spring to action to do (this process depends on a called struts2-spring-plugin 's jar package, which main function is to achieve just that sentence, but this jar when the package is not generated automatically MyEclipse Spring framework, so the need to manually import the downloaded).

  This package is open will find a man named struts-plugin.xml profile

struts-plugin.xml

 1 <struts>
 2     <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
 3     
 4     <!--  Make the Spring object factory the automatic default -->
 5     <constant name="struts.objectFactory" value="spring" />
 6 
 7     <constant name="struts.class.reloading.watchList" value="" />
 8     <constant name="struts.class.reloading.acceptClasses" value="" />
 9     <constant name="struts.class.reloading.reloadConfig" value="false" />
10 
11     <package name="spring-default">
12         <interceptors>
13             <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
14         </interceptors>
15     </package>    
16 </struts>

This configuration file adds < Constant name = "struts.objectFactory" value = "the Spring" /> , there is no need in struts.xml again added.

 

ApplicationContext.xml

1 ...
2     <bean id="userAction" class="action.UserAction" scope="prototype">
3         <property name="userDAO" ref="userDAO" />
4     </bean>
5 ...

It should be noted, another Spring configuration file must be coupled with the scope attribute value is the 'prototype'. Spring Framework must first know the generation userAction single embodiment, but Structs2 embodiment is a multi-frame, i.e. each request for an action struts2 instantiates an action object, but Spring instantiate only one (i.e. the first time data), which can lead to data errors when multiple requests of happens. And when we put after 'prototype' scope value is set, Spring would like struts2, how many times the request is an example of how many objects of action, independently of each other, the data will not go wrong.

struts.xml

1 ...
2         <action name="login" class="userAction" method="login">
3             <result name="admin">/main.jsp</result>
4             <result name="reader">/reader.jsp</result>
5             <result name="error">/error.jsp</result>
6             <result name="input">/userLogin.jsp</result>
7       </action> 
8 ...   

struts.xml in the class action label can be written directly ApplicationContext.xml the id value of the bean, instantiated by the Spring container, but still managed by Struts2, because ApplicationContext.xml property set in scope, they do not consider data case of an error.

Guess you like

Origin www.cnblogs.com/baikaizhuliangshui/p/11968550.html
Recommended