知识点

相关文章

更多

最近更新

更多

【第十章】集成其它Web框架 之 10.2 集成Struts1.x ——跟我学spring3

2019-03-01 15:32|来源: 开涛

10.2  集成Struts1.x

10.2.1  概述

      Struts1.x是最早实现MVC(模型-视图-控制器)模式的Web框架之一,其使用非常广泛,虽然目前已经有Struts2.x等其他Web框架,但仍有很多公司使用Struts1.x框架。

      集成Struts1.x也非常简单,除了通用配置外,有两种方式可以将Struts1.x集成到Spring中:

  • 最简单集成:使用Spring提供的WebApplicationContextUtils工具类中的获取Spring Web容器,然后通过Spring Web容器获取Spring管理的Bean;

  • Struts1.x插件集成:利用Struts1.x中的插件ContextLoaderPlugin来将Struts1.x集成到Spring中。

      接下来让我们首先让我们准备Struts1x所需要的jar包:

1.1、从下载的spring-framework-3.0.5.RELEASE-with-docs.zip中dist目录查找如下jar包,该jar包用于提供集成struts1.x所需要的插件实现等:

org.springframework.web.struts-3.0.5.RELEASE.jar

1.2、从下载的spring-framework-3.0.5.RELEASE-dependencies.zip中查找如下依赖jar包,该组jar是struts1.x需要的jar包:

com.springsource.org.apache.struts-1.2.9.jar                      //struts1.2.9实现包

com.springsource.org.apache.commons.digester-1.8.1.jar    //用于解析struts配置文件

com.springsource.org.apache.commons.beanutils-1.8.0.jar   //用于请求参数绑定

com.springsource.javax.servlet-2.5.0.jar                            //Servlet 2.5 API

antlr.jar                                                                          //语法分析包(已有)

commons-logging.jar                                                       //日志记录组件包(已有)

servlet-api.jar                                                                 //Servlet API包(已有)

jsp-api.jar                                                                      //JSP API包(已有,可选)

commons-validator.jar                                                 //验证包(可选)

commons-fileupload.jar                                               //文件上传包(可选)


10.2.2  最简单集成

只使用通用配置,利用WebApplicationContextUtils提供的获取Spring Web容器方法获取Spring Web容器,然后从Spring Web容器获取Spring管理的Bean。

1、 第一个Action实现:

  1.      

  2. package cn.javass.spring.chapter10.struts1x.action;  

  3. import org.apache.struts.action.Action;  

  4. //省略部分import  

  5. public class HelloWorldAction1 extends Action {  

  6.    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  

  7.  

  8.        WebApplicationContext ctx = WebApplicationContextUtils.  

  9.            getRequiredWebApplicationContext(getServlet().getServletContext());        

  10.        String message = ctx.getBean("message", String.class);  

  11.        request.setAttribute("message",  message);  

  12.        return mapping.findForward("hello");  

  13.    }  

  14. }  

      此Action实现非常简单,首先通过WebApplicationContextUtils获取Spring Web容器,然后从Spring Web容器中获取“message”Bean并将其放到request里,最后转到“hello”所代表的jsp页面。

2、JSP页面定义(webapp/WEB-INF/jsp/hello.jsp):

  1. <%@ page language="java" pageEncoding="UTF-8"  

  2. contentType="text/html; charset=UTF-8" %>  

  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  

  4. "http://www.w3.org/TR/html4/loose.dtd">  

  5. <html>  

  6. <head>  

  7.    <title>Hello World</title>  

  8. </head>  

  9. <body>  

  10.    ${message}  

  11. </body>  

  12. </html>  

3、配置文件定义:

3.1、Spring配置文件定义(resources/chapter10/applicationContext-message.xml):

在此配置文件中定义我们使用的“message”Bean;

  1. <bean id="message" class="java.lang.String">  

  2.    <constructor-arg index="0" value="Hello Spring"/>  

  3. </bean>  

3.2、struts配置文件定义(resources/chapter10/struts1x/struts-config.xml):

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <!DOCTYPE struts-config PUBLIC  

  3.          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"  

  4.          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">  

  5. <struts-config>  

  6.  <action-mappings>  

  7.    <action path="/hello" type="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction1">  

  8.       <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  

  9.    </action>  

  10.  </action-mappings>  

  11. </struts-config>  

3.3、web.xml部署描述符文件定义(webapp/WEB-INF/web.xml)添加如下内容:

  1. <!-- Struts1.x前端控制器配置开始   -->  

  2.       <servlet>  

  3.        <servlet-name>hello</servlet-name>  

  4.        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  

  5.        

  6.        <init-param>  

  7.            <param-name>config</param-name>  

  8.            <param-value>  

  9.                      /WEB-INF/classes/chapter10/struts1x/struts-config.xml  

  10.             </param-value>  

  11.        </init-param>  

  12.        <load-on-startup>1</load-on-startup>  

  13.       </servlet>  

  14.       <servlet-mapping>  

  15.        <servlet-name>hello</servlet-name>  

  16.        <url-pattern>*.do</url-pattern>  

  17.      </servlet-mapping>  

  18. <!-- Struts1.x前端控制器配置结束   -->  

  19.  

      Struts1.x前端控制器配置了ActionServlet前端控制器,其拦截以.do开头的请求,Strut配置文件通过初始化参数“config”来指定,如果不知道“config”参数则默认加载的配置文件为“/WEB-INF/ struts-config.xml”。

4、执行测试:在Web浏览器中输入http://localhost:8080/hello.do可以看到“Hello Spring”信息说明测试正常。

      有朋友想问,我不想使用这种方式,我想在独立环境内测试,没关系,您只需将spring/lib目录拷贝到spring/webapp/WEB-INF/下,然后将webapp拷贝到如tomcat中即可运行,尝试一下吧。

      Spring还提供ActionSupport类来简化获取WebApplicationContext,Spring为所有标准Action类及子类提供如下支持类,即在相应Action类后边加上Support后缀:

  • ActionSupport

  • DispatchActionSupport

  • LookupDispatchActionSupport

  • MappingDispatchActionSupport

具体使用方式如下:

1、Action定义

  1. package cn.javass.spring.chapter10.struts1x.action;  

  2. //省略import  

  3. public class HelloWorldAction2 extends ActionSupport {  

  4. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  

  5.        WebApplicationContext ctx = getWebApplicationContext();      

  6.        String message = ctx.getBean("message", String.class);  

  7.        request.setAttribute("message", message);  

  8.        return mapping.findForward("hello");  

  9.    }  

  10. }  

和第一个示例唯一不同的是直接调用getWebApplicationContext()即可获得Spring Web容器。

2、修改Struts配置文件(resources/chapter10/struts1x/struts-config.xml)添加如下Action定义:

  1. <action path="/hello2" type="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction2">  

  2.    <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  

  3. </action>  

3、启动嵌入式Web服务器并在Web浏览器中输入http://localhost:8080/hello2.do可以看到“Hello Spring”信息说明Struts1集成成功。

这种集成方式好吗?而且这种方式算是集成吗?直接获取Spring Web容器然后从该Spring Web容器中获取Bean,暂且看作是集成吧,这种集成对于简单操作可以接受,但更复杂的注入呢?接下来让我们学习使用Struts插件进行集成。

10.2.2  Struts1.x插件集成

Struts插件集成使用ContextLoaderPlugin类,该类用于为ActionServlet加载Spring配置文件。

1、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中配置插件:

  1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">  

  2.    <set-property property="contextClass" value="org.springframework.web.context.support.XmlWebApplicationContext"/>  

  3.    <set-property property="contextConfigLocation" value="/WEB-INF/hello-servlet.xml"/>  

  4.    <set-property property="namespace" value="hello"/>  

  5. </plug-in>  

  • contextClass可选,用于指定WebApplicationContext实现类,默认是XmlWebApplicationContext;

  • contextConfigLocation指定Spring配置文件位置,如果我们的ActionServlet 在 web.xml 里面通过 <servlet-name>hello</servlet-name>指定名字为“hello”,且没有指定contextConfigLocation,则默认Spring配置文件是/WEB-INF/hello-servlet.xml;

  • namespace因为默认使用ActionServlet在web.xml定义中的Servlet的名字,因此如果想要使用其他名字可以使用该变量指定,如指定“hello”,将加载的Spring配置文件为/WEB-INF/hello-servlet.xml;

由于我们的ActionServlet在web.xml中的名字为hello,而我们的配置文件在/WEB-INF/hello-servlet.xml,因此contextConfigLocation和namespace可以不指定,因此最简单配置如下:

  1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>  

通用配置的Spring Web容器将作为ContextLoaderPlugin中创建的Spring Web容器的父容器存在,然而可以省略通用配置而直接在struts配置文件中通过ContextLoaderPlugin插件指定所有配置文件。

插件已经配置了,那如何定义Action、配置Action、配置Spring管理Bean呢,即如何真正集成Spring+Struts1x呢?使用插件方式时Action将在Spring中配置而不是在Struts中配置了,Spring目前提供以下两种方式:

  • 将Struts配置文件中的<action>的type属性指定为DelegatingActionProxy,然后在Spring中配置同名的Spring管理的Action Bean;

  • 使用Spring提供的DelegatingRequestProcessor重载 Struts 默认的 RequestProcessor来从Spring容器中查找同名的Spring管理的Action Bean。

看懂了吗?好像没怎么看懂,那就直接上代码,有代码有真相。

     

2、定义Action实现,由于Action将在Spring中配置,因此message可以使用依赖注入方式了:

  1. package cn.javass.spring.chapter10.struts1x.action;  

  2. //省略  

  3. public class HelloWorldAction3 extends Action {  

  4.    private String message;  

  5.    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  

  6.  

  7.        request.setAttribute("message", message);  

  8.        return mapping.findForward("hello");  

  9.    }  

  10.    public void setMessage(String message) {//有setter方法,大家是否想到setter注入  

  11.        this.message = message;  

  12.    }  

  13. }  

3、DelegatingActionProxy方式与Spring集成配置:

3.1、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中进行Action定义:

  1. <action path="/hello3" type="org.springframework.web.struts.DelegatingActionProxy">  

  2.    <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  

  3. </action>  

  4.  

3.2、在Spring配置文件(webapp/WEB-INF/hello-servlet.xml)中定义Action对应的Bean

  1. <bean name="/hello3" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">  

  2.    <property name="message" ref="message"/>  

  3. </bean>  

3.3、启动嵌入式Web服务器并在Web浏览器中输入http://localhost:8080/hello3.do可以看到“Hello Spring”信息说明测试正常。

      从以上配置中可以看出:

  • Struts配置文件中<action>标签的path属性和Spring配置文件的name属性应该完全一样,否则错误;

  • Struts通过DelegatingActionProxy去到Spring Web容器中查找同名的Action Bean;

很简单吧,DelegatingActionProxy是个代理Action,其实现了Action类,其内部帮我们查找相应的Spring管理Action Bean并把请求转发给这个真实的Action。

4、DelegatingRequestProcessor方式与Spring集成:

4.1、首先要替换掉Struts默认的RequestProcessor,在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中添加如下配置:

  1. <controller>  

  2.    <set-property property="processorClass"  

  3.         value="org.springframework.web.struts.DelegatingRequestProcessor"/>  

  4. </controller>  

4.2、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中进行Action定义:

  1. <action path="/hello4" type=" cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">  

  2.    <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  

  3. </action>  

或更简单形式:

  1. <action path="/hello4">  

  2.    <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  

  3. </action>  

4.3、在Spring配置文件(webapp/WEB-INF/hello-servlet.xml)中定义Action对应的Bean

  1. <bean name="/hello4" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">  

  2.    <property name="message" ref="message"/>  

  3. </bean>  

4.4、启动嵌入式Web服务器并在Web浏览器中输入http://localhost:8080/hello4.do可以看到“Hello Spring”信息说明Struts1集成成功。

从以上配置中可以看出:

  • Struts配置文件中<action>标签的path属性和Spring配置文件的name属性应该完全一样,否则错误;

  • Struts通过DelegatingRequestProcessor去到Spring Web容器中查找同名的Action Bean;

很简单吧,只是由DelegatingRequestProcessor去帮我们查找相应的Action Bean,但没有代理Action了,所以推荐使用该方式。

图10-4 共享及专用Spring Web容器

Struts1x与Spring集成到此就完成了,在集成时需要注意一下几点:

  • 推荐使用ContextLoaderPlugin+DelegatingRequestProcessor方式集成;

  • 当有多个Struts模块时建议在通用配置部分配置通用部分,因为通用配置在正在Web容器中是可共享的,而在各个Struts模块配置文件中配置是不可共享的,因此不推荐直接使用ContextLoaderPlugin中为每个模块都指定所有配置,因为ContextLoaderPlugin加载的Spring容器只对当前的ActionServlet有效对其他ActionServlet无效,如图10-4所示。


本文链接:领悟书生教程网,转自http://sishuok.com/forum/blogPost/list/2511.html

相关问答

更多
  • struts1.x的入口点是一个Servelt struts2.x的入口点是一个Filter struts2.x并不是在struts1.x的基础上修改的,而是在xwork的基础上再加上struts1的一点好的思想
  • 个人感觉 strut2 的mvc 比较强大,也比较地道。 个人认为,配置就要进行集中配置。现在虽然有注解了。但是在后期的维护中也是不怎么方便地(某些情况) 阅读起来 也没 配置文件那样清晰可读。建议楼主将重点放在struts2上。现在大多公司选择的mvc 都是用的truts2
  • 我有,百度云可以分享给你!
  • 一键集成全部,貌似有点想多了吧。虽然目前这3个技术用的比较多,但是大多数时候都是说他们的整合,如果你想更快捷一些,直接拿别人的空项目,那样不就相当于一键集成全部了!
  • hibernate新出了好像是叫hibernate-ogm 吧,不知道有没有正式发布。它支持nosql数据库。 或者你可以使用spring-data-mongodb,这是 spring出的,目前应该的比较多。
  • hibernate新出了好像是叫hibernate-ogm 吧,不知道有没有正式发布。它支持nosql数据库。 或者你可以使用spring-data-mongodb,这是spring出的,目前应该的比较多。这些我都是在慕课网学到的。
  • Struts中的异常配置可分为局部和全局异常。这两种异常配置的方法完全一样,只是配置代码的位置不同。配置局部异常的元素在元素中,而全局异常的元素在元素中。局部异常的优先级大于全局异常的优先级。    下面的代码给出了一个标准的全局异常的配置方法:    
  • 答案部分来自Adobe知识库条目 。 Adobe表示内联IME在Windows上的Flash 10.0中不起作用,也不适用于Mac OS。 他们继续说明Flash Player 10.1在Windows中实现了这一点,但“没有适用于Mac OS的浏览器支持内联IME”。 我在Mac OS和Windows上使用10.0和10.2闪存播放器测试了一个简单的textArea。 所有组合都有效,即IME在线,但Mac上的10.2除外。 无论如何,Mac上的10.2使用系统IME键入聚焦文本组件。 没有适用于Mac ...
  • 我得到了这个问题。 在Readme文件中提到: 对于7Xi / Qi:#FNC IOS ACCEPTOR 000000000000# 对于所有其他(1D扫描仪和8Qi):#FNB00F40002# 对我来说#FNB00F40002#工作,将扫描器从HID模式切换到iOS模式。 现在它的工作。 它也不能用于模拟器。 I got the issue. In Readme file its mentioned : For the 7Xi/Qi: #FNC IOS ACCEPTOR 000000000000# Fo ...
  • 检查您的Java版本以获取Cognos安装和您的应用程序使用的Java版本。 对于Cognos 10.1 :随IBM Cognos BI提供的当前版本是JRE 1.5.0。 对于Cognos 10.2 :随IBM Cognos BI提供的当前版本是JRE 6.0。 Check your java version for the Cognos installation and the java version used by your app. For Cognos 10.1: The current ver ...