亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

【第十章】集成其它Web框架 之 10.2 集成Struts

系統 1610 0

先進行通用配置,? 【第十章】集成其它Web框架 之 10.1 概述 ?

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 實現:

?

java代碼:
        	
package cn.javass.spring.chapter10.struts1x.action;
import org.apache.struts.action.Action;
//省略部分import
public class HelloWorldAction1 extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 
        WebApplicationContext ctx = WebApplicationContextUtils.
            getRequiredWebApplicationContext(getServlet().getServletContext());       
        String message = ctx.getBean("message", String.class);
        request.setAttribute("message",  message);
        return mapping.findForward("hello");
    }
}
      

?????? 此Action實現非常簡單,首先通過WebApplicationContextUtils獲取Spring Web容器,然后從Spring Web容器中獲取“message”Bean并將其放到request里,最后轉到“hello”所代表的jsp頁面。

?

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

?

java代碼:
        <%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Hello World</title>
</head>
<body>
    ${message}
</body>
</html>
      

?

3 、配置文件定義:

?

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

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

?

java代碼:
        <bean id="message" class="java.lang.String">
    <constructor-arg index="0" value="Hello Spring"/>
</bean>
      

?

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

?

java代碼:
        <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
  <action-mappings>
    <action path="/hello" type="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction1">
       <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>
    </action>
  </action-mappings>
</struts-config>
      

?

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

?

java代碼:
        <!-- Struts1.x前端控制器配置開始   -->
       <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
       
        <init-param>
            <param-name>config</param-name>
            <param-value>
                      /WEB-INF/classes/chapter10/struts1x/struts-config.xml
             </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
<!-- Struts1.x前端控制器配置結束   -->
 
      

?

?????? 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 定義

?

java代碼:
        package cn.javass.spring.chapter10.struts1x.action;
//省略import
public class HelloWorldAction2 extends ActionSupport {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        WebApplicationContext ctx = getWebApplicationContext();    
        String message = ctx.getBean("message", String.class);
        request.setAttribute("message", message);
        return mapping.findForward("hello");
    }
}
      

?

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

?

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

?

java代碼:
        <action path="/hello2" type="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction2">
    <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>
</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 )中配置插件:

?

java代碼:
        <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextClass" value="org.springframework.web.context.support.XmlWebApplicationContext"/>
    <set-property property="contextConfigLocation" value="/WEB-INF/hello-servlet.xml"/>
    <set-property property="namespace" value="hello"/>
</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可以不指定,因此最簡單配置如下:

?

java代碼:
        <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 可以使用依賴注入方式了:

?

java代碼:
        package cn.javass.spring.chapter10.struts1x.action;
//省略
public class HelloWorldAction3 extends Action {
    private String message;
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 
        request.setAttribute("message", message);
        return mapping.findForward("hello");
    }
    public void setMessage(String message) {//有setter方法,大家是否想到setter注入
        this.message = message;
    }
}
      

?

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

?

3 .1 、在Struts 配置文件(resources/chapter10/struts1x/struts-config.xml )中進行Action 定義:

?

java代碼:
        <action path="/hello3" type="org.springframework.web.struts.DelegatingActionProxy">
    <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>
</action>
 
      

?

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

?

java代碼:
        <bean name="/hello3" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">
    <property name="message" ref="message"/>
</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 )中添加如下配置:

?

java代碼:
        <controller>
    <set-property property="processorClass"
         value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
      

?

4 .2 、在Struts 配置文件(resources/chapter10/struts1x/struts-config.xml )中進行Action 定義:

?

java代碼:
        <action path="/hello4" type=" cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">
    <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>
</action>
      

?

或更簡單形式:

?

?

java代碼:
        <action path="/hello4">
    <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>
</action>

      

?

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

?

java代碼:
        <bean name="/hello4" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">
    <property name="message" ref="message"/>
</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了,所以推薦使用該方式。

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

圖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

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成人看的一级毛片 | 国产拍在线 | 免费国产成人综合 | 成人看黄色大片 | 久久毛片久久毛 | 国产欧美国产精品第一区 | 俺来也俺来也天天夜夜视频 | 毛片免费永久不卡视频观看 | 日韩欧美高清 | 国产精品久久久久久久久kt | 日韩欧美色视频 | 91精品国产欧美一区二区 | 欧美成人免费全网站大片 | 奇米影视基地 | 国产99视频精品免视看7 | 婷婷四房综合激情五月性色 | 99久久免费国产精品热 | 免费国产精品视频 | 欧美黑人乱大交ⅹxxxxx | 国产精品自拍一区 | 国产色啪午夜免费视频 | 国产精品视频a | 国产一区二区三区免费看 | 天天搞夜夜操 | 婷婷啪啪| 成人啪啪免费看 | 99久久99视频 | 国产在视频线在精品 | 欧美日韩激情在线一区 | 男女www | 天码毛片一区二区三区入口 | 男女啪啪猛烈免费网站 | 精品国产一区二区三区成人 | 色综合天天综合网亚洲 | 国产精品香蕉在线观看首页 | 国产在线公开视频 | 日本不卡视频网站 | 久久网欧美| 五月亭亭激情五月 | 久久加勒比 | 国产高清在线精品一区免费97 |