1.先来看下struts2交给spring前后的代码
struts2单独使用时action由struts2自己负责创建;与spring集成时,action实例由spring负责创建。这导致在两种情况下struts.xml配置文件的略微差异。假如:LoginAction在包cn.edu.jlu.cs.action中。1.
struts2单独使用时,action的class属性为LoginAction的全路径名,如下:...<action
name="login"
class="cn.edu.jlu.cs.action.LoginAction"><result
name="studentSuccess">/student/studentindex.jsp</result>...2.
struts2与spring集成时,class属性是spring的applicationContext.xml中配置的bean的id属性值。---------------------------------------------------------------------------------------//struts.xml...<action
name="login"
class="LoginAction"><result
name="studentSuccess">/student/studentindex.jsp</result>...----------------------------------------------------------------------------------------//applicationContext.xml...<bean
id="LoginAction"
class="cn.edu.jlu.cs.action.LoginAction"
/>...----------------------------------------------------------------------------------------
2.struts1交给spring做法
需要在struts.xml中添加下面这段代码
<controller bufferSize="1024" memFileSize="128K" nocache="true">
<set-property
value="org.springframework.web.struts.DelegatingRequestProcessor"
property="processorClass" />
</controller>
<set-property
value="org.springframework.web.struts.DelegatingRequestProcessor"
property="processorClass" />
</controller>
优点:配置简单。缺点:1、Action与业务组件耦合度降到了代码层次,要求Action中的属性名要和配置文件中业务组件的id保持一致,耦合度高。2、由spring自动装配,依赖关系不明了,可读性差。
第二种,由spring容器控制,Action配看做一个普通的bean实例,需改变scope的默认值。struts.xml中Action的class属性对应spring容器中一个Action实例的id。优点:实现了高度的解耦。缺点:1、配置文件冗余、臃肿。不仅需要在struts.xml中配置Action的信息还需要在spring的配置文件中配置个Action的信息
深入探讨Struts2在独立使用与与Spring集成时,Action实例创建方式的区别,以及如何在两者间进行配置。重点分析了在配置Action时,struts.xml与applicationContext.xml文件的作用与交互,揭示了Action与业务组件的耦合关系及配置的优缺点。
369

被折叠的 条评论
为什么被折叠?



