Spring学习点滴,《Spring in Action》笔记(三)2011-08-18 unmi.cc 隔叶黄莺第十章. 使用其他 Web 框架41. Spring 提供了两种与 Struts 集成的方式1) 让你的 Action 继承 org.springframework.web.struts.ActionSupport2) 将请求委托给作为 Spring Bean 管理的 Struts action 来自理(P312)42. 为了让 Struts 能访问 Spring 管理的 Bean,必须在 struts-config.xml 中注册一个知道 Spring 上下文的 ContextLoaderPlugIn,用的是 WebApplicationContext:(P312)
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-propertyset-property="contextConfigLocation" value="/WEB-INF/training-servlet.xml,/WEB-INF/..."/>
</plugin-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property="contextConfigLocation" value="/WEB-INF/training-servlet.xml,/WEB-INF/..."/>
</plugin-in>
43. ActionSupport 重载了 setServlet()方法,获取bean的方式为调用 ActionSupport 的 getWebApplicationContext().getBean() (P313)44. 继承 Spring 提供的 ActionSupport 让 Struts与Spring 紧密耦合,而且 Action 还负责查找 Bean,这也违背了IoC原则(P313)45. 使用委托 Action:struts-config.xml 中每个 path 都指定 type 为 org.springframework.web.struts.DelegatingActionProxy,实际的 Action 实例由Spring 来管理,即 所有的 Action 实例是配置在 Spring 上下文文件中,它们之间用 path<->name 来映射,这种方 式并不好看(P315)46. 使用请求委托,只在 struts-config.xml 中配置 DelegatingRequestProcess 或 DelegatingTilesRequestProcessor 作为控制器,其余配置不变,如<action path="/listCourses" type="com.unmi.MyCoursesAction"/> 其实 type 属性是被所配置的 controller 忽略掉了,所以可 省去 type属性,真正的Action也是由Spring来配置装配,也是通过 path--name 来对应。这种做法就是 不需要为每一个 <action .../> 指定 org.springframework.web.struts.DelegatingActionProxy 。(P315)