黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

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

系統(tǒng) 1997 0

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

10.2? 集成Struts1.x

10.2.1? 概述

?????? Struts1.x是最早實(shí)現(xiàn)MVC(模型-視圖-控制器)模式的Web框架之一,其使用非常廣泛,雖然目前已經(jīng)有Struts2.x等其他Web框架,但仍有很多公司使用Struts1.x框架。

?????? 集成Struts1.x也非常簡(jiǎn)單,除了通用配置外,有兩種方式可以將Struts1.x集成到Spring中:

  • 最簡(jiǎn)單集成:使用Spring提供的WebApplicationContextUtils工具類中的獲取Spring Web容器,然后通過(guò)Spring Web容器獲取Spring管理的Bean;
  • Struts1.x插件集成:利用Struts1.x中的插件ContextLoaderPlugin來(lái)將Struts1.x集成到Spring中。

?

??????? 接下來(lái)讓我們首先讓我們準(zhǔn)備Struts1x 所需要的jar 包:

1.1 、從下載的spring-framework- 3.0.5 .RELEASE-with-docs.zip 中dist 目錄查找如下jar 包,該jar 包用于提供集成struts1.x 所需要的插件實(shí)現(xiàn)等:

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實(shí)現(xiàn)包

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

com.springsource.org.apache.commons.beanutils-1.8.0.jar?? //用于請(qǐng)求參數(shù)綁定

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

antlr.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//語(yǔ)法分析包(已有)

commons-logging.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //日志記錄組件包(已有)

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

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

commons-validator.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 驗(yàn)證包(可選)

commons-fileupload.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 文件上傳 包(可選)

?

??

10.2.2? 最簡(jiǎn)單集成

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

?

1、? 第一個(gè)Action 實(shí)現(xiàn):

?

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實(shí)現(xiàn)非常簡(jiǎn)單,首先通過(guò)WebApplicationContextUtils獲取Spring Web容器,然后從Spring Web容器中獲取“message”Bean并將其放到request里,最后轉(zhuǎn)到“hello”所代表的jsp頁(yè)面。

?

2 、JSP 頁(yè)面定義(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 )添加如下內(nèi)容:

?

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前端控制器配置結(jié)束   -->
 
      

?

?????? Struts1.x前端控制器配置了ActionServlet前端控制器,其攔截以.do開頭的請(qǐng)求,Strut配置文件通過(guò)初始化參數(shù)“config”來(lái)指定,如果不知道“config”參數(shù)則默認(rèn)加載的配置文件為“/WEB-INF/ struts-config.xml”。

?

4 、執(zhí)行測(cè)試: 在Web瀏覽器中輸入http://localhost:8080/hello.do可以看到“Hello Spring”信息說(shuō)明測(cè)試正常。

?

?????? 有朋友想問(wèn),我不想使用這種方式,我想在獨(dú)立環(huán)境內(nèi)測(cè)試,沒關(guān)系,您只需將spring/lib目錄拷貝到spring/webapp/WEB-INF/下,然后將webapp拷貝到如tomcat中即可運(yùn)行,嘗試一下吧。

?????? Spring還提供ActionSupport類來(lái)簡(jiǎn)化獲取WebApplicationContext,Spring為所有標(biāo)準(zhǔn)Action類及子類提供如下支持類,即在相應(yīng)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");
    }
}
      

?

和第一個(gè)示例唯一不同的是直接調(diào)用 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 、啟動(dòng)嵌入式Web 服務(wù)器 并在Web瀏覽器中輸入http://localhost:8080/hello2.do可以看到“Hello Spring”信息說(shuō)明Struts1集成成功。

?

這種集成方式好嗎?而且這種方式算是集成嗎?直接獲取Spring Web容器然后從該Spring Web容器中獲取Bean,暫且看作是集成吧,這種集成對(duì)于簡(jiǎn)單操作可以接受,但更復(fù)雜的注入呢?接下來(lái)讓我們學(xué)習(xí)使用Struts插件進(jìn)行集成。

?

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實(shí)現(xiàn)類,默認(rèn)是XmlWebApplicationContext;
  • contextConfigLocation 指定Spring配置文件位置,如果我們的ActionServlet 在 web.xml 里面通過(guò) <servlet-name>hello</servlet-name>指定名字為“hello”,且沒有指定contextConfigLocation,則默認(rèn)Spring配置文件是/WEB-INF/hello-servlet.xml;
  • namespace 因?yàn)槟J(rèn)使用ActionServlet在web.xml定義中的Servlet的名字,因此如果想要使用其他名字可以使用該變量指定,如指定“hello”,將加載的Spring配置文件為/WEB-INF/hello-servlet.xml;

?

由于我們的ActionServlet在web.xml中的名字為hello,而我們的配置文件在/WEB-INF/hello-servlet.xml,因此contextConfigLocation和namespace可以不指定,因此最簡(jiǎn)單配置如下:

?

java代碼:
        <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>
      

?

通用配置的Spring Web容器將作為 ContextLoaderPlugin 中創(chuàng)建的Spring Web容器的父容器存在,然而可以省略通用配置而直接在struts配置文件中通過(guò) ContextLoaderPlugin 插件指定所有配置文件。

?

插件已經(jīng)配置了,那如何定義Action、配置Action、配置Spring管理Bean呢,即如何真正集成Spring+Struts1x呢?使用插件方式時(shí)Action將在Spring中配置而不是在Struts中配置了,Spring目前提供以下兩種方式:

  • 將Struts配置文件中的<action>的type屬性指定為DelegatingActionProxy,然后在Spring中配置同名的Spring管理的Action Bean;
  • 使用Spring提供的DelegatingRequestProcessor重載 Struts 默認(rèn)的 RequestProcessor來(lái)從Spring容器中查找同名的Spring管理的Action Bean。

看懂了嗎?好像沒怎么看懂,那就直接上代碼,有代碼有真相。

?

2 、定義Action 實(shí)現(xiàn),由于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 )中進(jìn)行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 對(duì)應(yīng)的Bean

?

java代碼:
        <bean name="/hello3" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">
    <property name="message" ref="message"/>
</bean>
      

?

3.3 、啟動(dòng)嵌入式Web 服務(wù)器 并在Web瀏覽器中輸入http://localhost:8080/hello3.do可以看到“Hello Spring”信息說(shuō)明測(cè)試正常。

?????? 從以上配置中可以看出:

  • Struts配置文件中<action>標(biāo)簽的path屬性和Spring配置文件的name屬性應(yīng)該完全一樣,否則錯(cuò)誤;
  • Struts通過(guò) DelegatingActionProxy 去到Spring Web容器中查找同名的Action Bean;

很簡(jiǎn)單吧,DelegatingActionProxy是個(gè)代理Action,其實(shí)現(xiàn)了Action類,其內(nèi)部幫我們查找相應(yīng)的Spring管理Action Bean并把請(qǐng)求轉(zhuǎn)發(fā)給這個(gè)真實(shí)的Action。

?

?

4 、DelegatingRequestProcessor 方式與Spring 集成:

?

4.1 、首先要替換掉Struts 默認(rèn)的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 )中進(jìn)行Action 定義:

?

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

?

或更簡(jiǎn)單形式:

?

?

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

      

?

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

?

java代碼:
        <bean name="/hello4" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">
    <property name="message" ref="message"/>
</bean>
      

?

4.4 、啟動(dòng)嵌入式Web 服務(wù)器 并在Web瀏覽器中輸入http://localhost:8080/hello4.do可以看到“Hello Spring”信息說(shuō)明Struts1集成成功。

?

從以上配置中可以看出:

  • Struts配置文件中<action>標(biāo)簽的path屬性和Spring配置文件的name屬性應(yīng)該完全一樣,否則錯(cuò)誤;
  • Struts通過(guò) DelegatingRequestProcessor 去到Spring Web容器中查找同名的Action Bean;

很簡(jiǎn)單吧,只是由 DelegatingRequestProcessor 去幫我們查找相應(yīng)的Action Bean,但沒有代理Action了,所以推薦使用該方式。

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

圖10-4 共享及專用Spring Web容器

?

Struts1x與Spring集成到此就完成了,在集成時(shí)需要注意一下幾點(diǎn):

  • 推薦使用ContextLoaderPlugin+DelegatingRequestProcessor方式集成;
  • 當(dāng)有多個(gè)Struts模塊時(shí)建議在通用配置部分配置通用部分,因?yàn)橥ㄓ门渲迷谡赪eb容器中是可共享的,而在各個(gè)Struts模塊配置文件中配置是不可共享的,因此不推薦直接使用ContextLoaderPlugin中為每個(gè)模塊都指定所有配置,因?yàn)? ContextLoaderPlugin 加載的Spring 容器只對(duì)當(dāng)前的ActionServlet 有效對(duì)其他ActionServlet 無(wú)效, 如圖10-4所示。
原創(chuàng)內(nèi)容,轉(zhuǎn)載請(qǐng)注明出處【 http://sishuok.com/forum/blogPost/list/2511.html

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


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論