在struts的action中使用spring的IOC容器管理类
struts-config.xml 配置文件如下
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
- <struts-config>
- <data-sources />
- <form-beans >
- <form-bean name="TestForm" type="com.gavin.struts.form.TestForm" />
- </form-beans>
- <global-exceptions />
- <global-forwards />
- <action-mappings >
- <action
- attribute="TestForm"
- name="TestForm"
- path="/test"
- scope="request"
- type="org.springframework.web.struts.DelegatingActionProxy"
- validate="false">
- <forward name="ok" path="/form/test.jsp" />
- </action>
- </action-mappings>
- <message-resources parameter="com.gavin.struts.ApplicationResources" />
- </struts-config>
其中最关键的地方是 action 的 type="org.springframework.web.struts.DelegatingActionProxy"
必须设置为spring的代理
spring的配置文件如下
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
- <bean name="/test" class="com.gavin.struts.action.TestAction">
- <property name="log">
- <ref bean="Log" />
- </property>
- </bean>
- <bean id="Log" class="com.gavin.service.Log" />
- </beans>
注意:
action 对应的类必须 设置name属性,而不是id ,这里的name必须和struts配置文件中action的path相同