Java Web整合开发(14) -- Struts 1.x 概述

整合Spring与Struts1的三种方法总结

无论用那种方法来整合,第一步就是要装载spring的应用环境,有三种方式:

#1. struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" 
  "http://struts.apache.org/dtds/struts-config_1_2.dtd"
> <struts-config> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/> </plug-in> </struts-config>

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

#2. web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

#3. web.xml(低版本tomcat)

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <servlet>
        <servlet-name>SpringContextServlet</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    </servlet>
</web-app>

1.使用Spring 的 ActionSupport 。

Struts的Action继承Spring的ActionSupport类,这样Struts 1.x就融入Spring框架中了。

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

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");

        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

<struts-config>
    <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
</struts-config>

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <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类的实现方式相同)。

1) <action>中 type指向的是spring 的代理类

sturts-config.xml

<struts-config>

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

    <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>

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

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

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

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

原文地址:https://www.cnblogs.com/thlzhf/p/4374652.html