`

spring整合struts

阅读更多

此篇文章为转载,留作记录,spring和struts1.X版本的集成有三种方式,而Struts 2(或者叫做WebWork 2),只需要在struts.prperties中加入struts.objectFactory=spring,指定spring作为Struts的bean工厂就可以了

Sping和Struts1.X集成的三种方法:

   1,使用Spring 的 ActionSupport

    2,使用Spring 的 DelegatingRequestProcessor 类。

    3,全权委托。

    无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。 就是在 struts 中加入一个插件。

    struts-config.xml中

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>

    spring 的配置文件被作为参数配置进来。这样可以省略对web.xml 文件中的配置。确保你的applicationContext.xml 在WEB-INF目录下面

    1、使用Spring的ActionSupport .

    Spring 的ActionSupport 继承至 org.apache.struts.action.Action ActionSupport的子类可以或得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量。

    这是一个 servlet 的代码:

public class LoginAction extends org.springframework.web.struts.ActionSupport { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
//获得 WebApplicationContext 对象             
WebApplicationContext ctx = this.getWebApplicationContext(); LoginDao dao = (LoginDao) ctx.getBean("loginDao");
   User u = new User(); u.setName(loginForm.getName());
u.setPwd(loginForm.getPwd());
if(dao.checkLogin(u)){
return mapping.findForward("success");
}else{
return mapping.findForward("error"); }

applicationContext.xml 中的配置
<beans>
<bean id="loginDao" class="com.cao.dao.LoginDao"/>
</beans>

    这中配置方式同直接在web.xml文件配置差别不大。注意:Action继承自 org.springframework.web.struts.ActionSupport 使得struts和spring耦合在一起。

    但实现了表示层和业务逻辑层的解耦(LoginDao dao = (LoginDao) ctx.getBean("loginDao"))。

    2、使用Spring 的 DelegatingRequestProcessor 类

    DelegatingRequestProcessor 继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。

    sturts-config.xml 中 <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> 通过 <controller >来替代org.apache.struts.action.RequestProcessor 的请求处理。

public class LoginAction extends Action {
//利用spring来注入这个对象。
private LoginDao dao ; public void setDao(LoginDao dao) {
System.out.println("执行注入");
this.dao = dao;
}

public LoginDao getDao() {
return dao;
}

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub //WebApplicationContext ctx = this.getWebApplicationContext();
//LoginDao dao = (LoginDao) ctx.getBean("loginDao"); User u = new User(); u.setName(loginForm.getName());
u.setPwd(loginForm.getPwd()); //直接用dao来调用spring会将这个对象实例化。
if(dao.checkLogin(u)){
return mapping.findForward("success");
}else{
return mapping.findForward("error"); LoginAction extends Action 说明 struts 每有和spring 耦合。 applicationContext.xml 中的配置。
<beans>
<bean id="loginDao" class="com.cao.dao.LoginDao"/> <bean name="/login" class="com.cao.struts.action.LoginAction">
<property name="dao">
<ref local="loginDao"/>
</property>
</bean>
</beans>

    这里 name="/login" 与struts 中的path匹配class="com.cao.struts.action.LoginAction" 与struts 中的type匹配

    还要为 LoginAction 提供必要的setXXX方法。获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。

    3,全权委托:

    Action 的创建和对象的依赖注入全部由IOC容器来完成。使用Spring的DelegatingAcionProxy来帮助实现代理的工作org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action .

    全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。

<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.cao.struts.form.LoginForm" />

</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings >
<!-- type指向的是spring 的代理类 -->
<action
attribute="loginForm"
input="login.jsp"
name="loginForm"
path="/login"
scope="request"

    type="org.springframework.web.struts.DelegatingActionProxy" > <forward name="success" path="/ok.jsp" />
<forward name="error" path="/error.jsp" />
</action> </action-mappings>
<message-resources parameter="com.cao.struts.ApplicationResources" />

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>

</struts-config>
不同之处
1, <action>中 type指向的是spring 的代理类

2, 去掉struts-config.xml中 <controller >

    三种整和方式中我们优先选用全权委托的方式。

    理由:

    1,第一种使得过多的耦合了Spring和Action .

    2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。

    总结一下:

    整合工作中的步骤:

    1,修改struts-config.xml

    2,配置applicationContext.xml

    3,为Action添加get/set方法 来获得依赖注入的功能。

整合第一步就是用struts加载spring的应用环境。

在 struts-config.xml中配置

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>

其中applicationContext.xml 必须在WEB-INF目录下面。若有多个,直接在value后,用逗号隔开。

第一种:每个Action不继承Struts提供的Action类,使用Spring提供的ActionSupport,这种方式使Struts与Spring耦合度高。不提倡。

第二种:让Spring管理Struts的Action。那么这样就有一个问题,怎样使前台提交过来的请求转发给Spring。

我们知道当使用Struts作为MVC框架时,客户端的HTTP请求都是直接向 ActionServlet请求的,因此关键就是让ActionServlet将请求转发给Spring容器中的Action。这可以利用Spring的另一个扩展点:通过扩展RequestProcessor完成,使用扩展的RequestProcessor替换Struts的 RequestProcessor。Spring完成了这种扩展,Spring提供的DelegatingRequestProcessor继承 Request- Processor。为了让Struts使用DelegatingRequestProcessor,还需要在struts-config.xml文件中增加如下一行:

<controller processorClass="org.springframework.web.struts.Delegating RequestProcessor"/>

使用spring的RequestProcessor替换struts原有的RequestProcessor

完成这个设置后,Struts会将截获到的用户请求转发到Spring下的bean,根据bean的name属性来匹配。而Struts中的action配置则无须配置class属性,即使配置了class属性也没有任何用处。

第三种:使用DelegatingActionProxy

使用DelegatingRequestProcessor简单方便,但有一个缺点,RequestProcessor是Struts的一个扩展点,也许应用程序本身就需要扩展RequestProcessor,而 DelegatingRequest- Processor已经使用了这个扩展点。

DelegatingRequestProcessor直接替换了原有的 RequestProcessor,在请求转发给action之前,转发给Spring管理的Bean;而DelegatingActionProxy则被配置成Struts的Action,即所有的请求先被ActionServlet截获,请求被转发到对应的Action,而action的实现类全都是 DelegatingActionProxy,DelegatingActionProxy再将请求转发给Spring容器的Action。

可以看出,使用DelegatingActionProxy比使用DelegatingRequestProcessor要晚一步转发到Spring的context。但通过这种方式可以避免占用扩展点。

与使用DelegatingRequestProcessor相比,使用DelegatingActionProxy仅需要去掉controller配置元素,并将所有的action实现类改为DelegatingActionProxy即可。

这种策略的性能比前一种策略的效果要差一些,因为需要多创建一个Delegating- ActionProxy实例。而且,J2EE应用中Action非常多,这将导致大量创建DelegatingActionProxy实例—这对性能的影响不可避免。


注意:使用DelegatingActionProxy的整合策略,可避免占用Struts的RequestProcessor扩展点,但降低了整合性能

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics