spring 整合struts

 一、spring容器的创建方式
    1.直接在web.xml中配置创建spring容器
        1.1 用ServletContextListener方式实现
                <listener>
                    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                </listener>
              如果有多个配置文件需要载入,使用<context-param>元素,ContextLoaderListener加载时,会查找名为contextConfigLocation的参数,所以,有下面:
                <context-param>
                        <param-name>contextConfigLocation</param-name>
                        <param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
                </context-param>
        1.2 用load-on-startup Servlet方式实现
                <servlet>
                    <servlet-name>context</servlet-name>
                    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
                    <load-on-startup>1</load-on-startup>
                </servlet>
    2.用struts plugin创建spring容器
        在struts-config.xml中加入
        <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
            <set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml(用于配置表示层上下文),/WEB-INF/applicationContext.xml"/>
        </plug-in>


二.问题
    如何让控制器访问到spring容器中的业务逻辑组件?有两种方式2.1,2.2
        2.1 spring管理控制器,并利用IOC为控制器注入逻辑组件
            2.1.1 使用spring的DelegatingRequestProcessor
                        在struts-config.xml中加入
                            <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
                     而后,struts会接取用户请求转发到spring context下的bean,根据bean的name来匹配,因此在struts的action中配置type是没用的!
                如下: 
                        <action path="/login" name="loginForm" scope="requerst" validate="true" input="/login.jsp">
                            <forward name="input" path="/login.jsp"/>
                            <forward name="welcom" path="/welcom.jsp"/>
                        </action>
           然后在web.xml中配置:
            <filter>
                    <filter-name>requestContextFilter</filter-name>
                    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
            </filter>
            <filter-mapping>
                   <filter-name>requestContextFilter</filter-name>
                    <url-pattern>/*</url-pattern>
            </filter-mapping>


           2.1.2   使用DelegatingActionProxy(为了避免应用程序本身就扩展了RequetstProcessor,DelegatingRequestProcessor就用不成了)
                DelegatingActionProxy被配置成struts的Action(即所有请求先被ActionServlet得到),转发到相应的Action, 而Action的实现全都是DelegatingActionProxy,DelegatingActionProxy再把请求转发给spring容器的Action.
                DelegatingActionProxy不需要在struts-config.xml中配置<controller>元素,只需将action type进行如下配置:
                <action path="/login" name="loginForm" scope="requerst" validate="true" input="/login.jsp" type="org.springframework.web.struts.DelegatingActionProxy">
                            <forward name="input" path="/login.jsp"/>
                            <forward name="welcom" path="/welcom.jsp"/>
                        </action>

       2.2  使用srping的ActionSupport代替struts 的Action
            这种方式下,struts的Action不受spring Ioc容器管理,Action代码与spring API部分耦合(造成代码污染),但其增强了代码的可读性,Action代码中显示调用业务逻辑组件,无需等容器注入。
            例如:
            public class LoginAction extends ActionSupport {   //继承spring的ActionSupport         
                   public LoginAction()
                    {
                        //不可在构造方法中调用getWebApplicationContext()
                      } 
                    private LoginService loginService;     //将业务逻辑组件对象作为成员变量
                    public LoginService getLoginService()
                    {
                        return (LoginService)getWebApplicationContext().getBean("loginService");
                    }
                    //重写execute()方法
                }
            struts-config.xml中<action>元素正常配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值