ITeye博客

在struts的action中使用spring的IOC容器管理类

pignut_wang · 2007-02-18 13:44 · struts+spring · Spring · Struts · 配置管理 · IOC · Bean · Markdown

struts-config.xml 配置文件如下

xml 代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
  3. <struts-config>
  4. <data-sources />
  5. <form-beans >
  6. <form-bean name="TestForm" type="com.gavin.struts.form.TestForm" />
  7. </form-beans>
  8. <global-exceptions />
  9. <global-forwards />
  10. <action-mappings >
  11. <action
  12. attribute="TestForm"
  13. name="TestForm"
  14. path="/test"
  15. scope="request"
  16. type="org.springframework.web.struts.DelegatingActionProxy"
  17. validate="false">
  18. <forward name="ok" path="/form/test.jsp" />
  19. </action>
  20. </action-mappings>
  21. <message-resources parameter="com.gavin.struts.ApplicationResources" />
  22. </struts-config>

 

其中最关键的地方是 action 的 type="org.springframework.web.struts.DelegatingActionProxy"

必须设置为spring的代理

spring的配置文件如下

xml 代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  6. <bean name="/test" class="com.gavin.struts.action.TestAction">
  7. <property name="log">
  8. <ref bean="Log" />
  9. </property>
  10. </bean>
  11. <bean id="Log" class="com.gavin.service.Log" />
  12. </beans>

 

注意:

action 对应的类必须 设置name属性,而不是id ,这里的name必须和struts配置文件中action的path相同

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。