spring与struts的整合

Spring与Struts 2整合的三种方式
 

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()可以获得这个变量。

这是一个 Action:
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>

这里 与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 >

 

 

优缺点:

   ActionSupport虽然很方便,但是Struts的Action与Spring耦合在一起,并且Struts的Action不在Spring的控制之中,这样如果更换别的框架,或想使用Spring的AOP都是比较困难的,而且如果是多个动作放在一个Action中,则这种方式就无能为力了。
 使用DelegatingRequestProcessor虽然解决了第一种带来的问题,但是还是有缺点的,那就是开发人员可以自己定义RequestProcessor,这样如果没有使用Struts默认的RequestProcessor,则需要手工来整合Spring和Struts了。
 三者中,使用Spring的DelegatingActionProxy类来整合Spring和Struts的方式最为强大和灵活。

 

 

三种整和方式中我们优先选用全权委托的方式。
理由:
1,第一种使得过多的耦合了Spring和Action .
2,RequestProcessor类已经被代理如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。

总结一下:
整合工作中的步骤:
1,修改struts-config.xml
2, 配置applicationContext.xml
3, 为Action添加get/set方法来获得依赖注入的功能。

 

 

 

 

 

 

struts,spring,hibernate三者也学习得差不多了,是时候该去深入了解如何去用spring来融合其他两个;

 Spring与Struts如何整合(以下技术文档转自网络):

  为了在Struts中加载Spring context,需要在struts-config.xml文件中加入如下部分:

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

    通过Struts的plug-in在Struts和Spring之间提供了良好的结合点。通过plug-in我们实现了Spring context的加载,不过仅仅加载Spring context并没有什么实际的意义,还应该经过配置将Struts的Action交给Spring容器进行管理。 

 
       
< action-mappings>< action path="/login"type="org.springframework.web.struts.DelegatingActionProxy"name="loginForm">< forward name="success" path="/main.jsp" />< forward name="failure" path="/login.jsp" />< /action>
 
      

    在form bean这个节点上与传统的Struts配置没有什么区别,而在Action上面则发生了变化。在传统的action节点上type属性写入action类的完整类名,而和Spring结合后在这点上是使用了Spring提供的DelegatingActionProxy作为action的type属性,DelegatingActionProxy同样是org.apache.struts.action.Action的一个子类,它将把调用请求转交给真正的Action实现。通过这样的方式,Spring获得了Action实例的管理权,它将对Action进行调度,并为Struts提供所需的Action实例。这样,就可以将Action看作是Spring的一个bean,它就可以享受Spring的所有服务,如依赖注入、实例管理、事务管理等。
在applicationContext.xml中相应的配置如下的节点:

 
       
< beans>.......< bean name="/login" class="net.xiaxin.action.LoginAction"singleton="false">< property name="userDAO">< ref bean="userDAOProxy" />< /property>< /bean>< /beans>
 
      

    这样委托以后,修改Struts-config.xml中的action属性,使action的type值不指向具体的实现类,统一改为代理类type="org.springframework.web.struts.DelegatingActionProxy",在applicationContext.xml中的bean name值设定与Struts-config.xml中action path值一样,以使代理类能够根据传入的path在springContext环境中找到相应的bean,并将实例返回给Struts。由于动作是在Spring控制之下,所以Spring可以利用Struts所有功能。由于Spring反转控制特性,使Struts根本感觉不到Spring的存在,并且可以利用Spring动作管理框架的所有优点,比如日志拦截、数据校验、线程安全。
最后这个bean的配置是关键,这个名为“/login”的bean与Struts中的

 
       
< action path="/login" ……>……< /action>
 
      

    节点相对应,这样,Spring Bean Name与Struts Action Path相关联,当Struts加载对应的Action时,DelegatingActionProxy就根据传入的path属性,在Spring Context寻找对应bean,并将其实例返回给Struts。与此同时,还可以看到,"/login" bean 中包含了一个userDAO 引用,Spring 在运行期将根据配置为其提供userDAO 实例,以及围绕userDAO 的事务管理服务。这样一来,对于Struts 开发而言,我们既可以延续Struts 的开发流程,也可以享受Spring 提供的事务管理服务。而bean 的另外一个属性singleton="false",指明了Action 的实例获取方式为每次重新创建。这也解决了Struts中令人诟病的线程安全问题。

    至此,SS组合已经将Struts MVC以及Spring中的Bean管理、事务管理融为一体。如果算上userDAO 中的Hibernate 部分,我们就获得了一个全面、成熟、高效、自顶而下的Web 开发框架。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值