使用spring 的action-servlet.xml解決struts線程問題
<bean name="/register" class="com.action.RegisterAction" singleton="false">
<property name="businessService">
<ref bean="businessService"/>
</property>
</bean>
RegisterAction是Action的實現類,businessService是業務邏輯,Spring把businessService注入到Action中,在Action中只要寫businessService的get和set方法就可以了,同時action的bean設爲singleton="false",這樣每次新建一個實例,從而解決了Struts中Action的線程同步問題
1.struts 中實現接口
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml" />
</plug-in>
2.將<action type 變爲
= "org.springframework.web.struts.DelegatingActionProxy"
3.編寫action-servlet.xml 如:
<bean name="/Analyse" class="com.apple.struts.action.IndexAnalyseAction" singleton="false">
<property name="flowService">
<ref bean="flowService"/> //此處對應applicationContext.xml
</property>
</bean>
4.web.xml中配置
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>