日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring集成struts2

發(fā)布時間:2023/11/29 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring集成struts2 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Struts2前身是WebWork,核心并沒有改變,其實(shí)就是把WebWork改名為struts2,與Struts1一點(diǎn)關(guān)系沒有。

?

Struts2中通過ObjectFactory接口實(shí)現(xiàn)創(chuàng)建及獲取Action實(shí)例,類似于Spring的IoC容器,所以Action實(shí)例可以由ObjectFactory實(shí)現(xiàn)來管理,因此集成Spring的關(guān)鍵點(diǎn)就是如何創(chuàng)建ObjectFactory實(shí)現(xiàn)來從Spring容器中獲取相應(yīng)的Action Bean。

?

Struts2提供一個默認(rèn)的ObjectFactory接口實(shí)現(xiàn)StrutsSpringObjectFactory,該類用于根據(jù)Struts2配置文件中相應(yīng)Bean信息從Spring 容器中獲取相應(yīng)的Action。

?

因此Struts2.x與Spring集成需要使用StrutsSpringObjectFactory類作為中介者。

?

接下來讓我們首先讓我們準(zhǔn)備Struts2x所需要的jar

準(zhǔn)備Struts2.x需要的jar包,到Struts官網(wǎng)http://struts.apache.org/下載struts-2.2.1.1版本,拷貝如下jar包到項(xiàng)目的lib目錄下并添加到類路徑:

?

lib\struts2-core-2.2.1.1.jar ?????????????//核心struts2包

lib\xwork-core-2.2.1.1.jar??? ??????????//命令框架包,獨(dú)立于Web環(huán)境,為Struts2

//提供核心功能的支持包

lib\freemarker-2.3.16.jar????????????? ?//提供模板化UI標(biāo)簽及視圖技術(shù)支持

lib\ognl-3.0.jar??????????????????? ???//對象圖導(dǎo)航工具包,類似于SpEL

lib\ struts2-spring-plugin-2.2.1.1.jar???? ?//集成Spring的插件包

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

lib\commons-fileupload-1.2.1.jar??????? //用于支持文件上傳的包

?

10.3.2? 使用ObjectFactory集成

1、Struts2.x的Action實(shí)現(xiàn):

Java代碼??
  • ??????
  • package?cn.javass.spring.chapter10.struts2x.action;??
  • import?org.apache.struts2.ServletActionContext;??
  • import?com.opensymphony.xwork2.ActionSupport;??
  • public?class?HelloWorldAction?extends?ActionSupport?{??
  • ????private?String?message;??
  • ????@Override??
  • ????public?String?execute()?throws?Exception?{??
  • ????????ServletActionContext.getRequest().setAttribute("message",?message);??
  • ????????return?"hello";??
  • ????}??
  • ????public?void?setMessage(String?message)?{//setter注入??
  • ????????this.message?=?message;??
  • ????}??
  • }??
  • ?

    2、JSP頁面定義,使用Struts1x中定義的JSP頁面“webapp/WEB-INF/jsp/hello.jsp”;

    ?

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

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

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

    ?

    4、Spring Action?配置文件定義(resources/chapter10/hello-servlet.xml):

    Java代碼??
  • <bean?name="helloAction"?class="cn.javass.spring.chapter10.struts2x.action.HelloWorldAction"?scope="prototype">??
  • ????<property?name="message"?ref="message"/>??
  • </bean>??
  • ?

    Struts2的Action在Spring中配置,而且應(yīng)該是prototype,因?yàn)镾truts2的Action是有狀態(tài)的,定義在Spring中,那Struts如何找到該Action呢?

    ?

    ?

    5、struts2配置文件定義(resources/chapter10/struts2x/struts.xml):

    Java代碼??
  • <?xml?version="1.0"?encoding="UTF-8"?>??
  • <!DOCTYPE?struts?PUBLIC??
  • ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"??
  • ????"http://struts.apache.org/dtds/struts-2.0.dtd">??
  • <struts>??
  • ????<constant?name="struts.objectFactory"?value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>??
  • ????<constant?name="struts.devMode"?value="true"/>??
  • ????<package?name="default"?extends="struts-default">??
  • ????????<action?name="hello"?class="helloAction">??
  • ????????????<result?name="hello"?>/WEB-INF/jsp/hello.jsp</result>??
  • ????????</action>??
  • ????</package>??
  • </struts>??
    • struts.objectFactory通過在Struts配置文件中使用常量屬性struts.objectFactory來定義Struts將要使用的ObjectFactory實(shí)現(xiàn),此處因?yàn)樾枰獜腟pring容器中獲取Action對象,因此需要使用StrutsSpringObjectFactory來集成Spring;
    • <action name="hello"?class="helloAction">:StrutsSpringObjectFactory對象工廠將根據(jù)<action>標(biāo)簽的class屬性去Spring容器中查找同名的Action Bean;即本例中將到Spring容器中查找名為helloAction的Bean。

    ?

    6、web.xml部署描述符文件定義(webapp/WEB-INF/web.xml):

    ?

    6.1、由于Struts2只能使用通用配置,因此需要在通用配置中加入Spring Action配置文件(chapter10/struts2x/struts2x-servlet.xml):

    Java代碼??
  • <context-param>??
  • ????<param-name>contextConfigLocation</param-name>??
  • ????<param-value>??
  • ????????classpath:chapter10/applicationContext-message.xml,??
  • ????????classpath:chapter10/struts2x/struts2x-servlet.xml??
  • ????</param-value>??
  • </context-param>??
  • ?????? Struts2只能在通用配置中指定所有Spring配置文件,并沒有如Struts1自己指定Spring配置文件的實(shí)現(xiàn)。

    ?

    6.2、Strut2前端控制器定義,在web.xml中添加如下配置:

    Java代碼??
  • <!--?Struts2.x前端控制器配置開始???-->??
  • <filter>??
  • ????<filter-name>struts2x</filter-name>??
  • ????<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>??
  • ????<init-param>??
  • ??????????????<param-name>config</param-name>??
  • ??????????????<param-value>??
  • ?????????????????????struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml??
  • ???????????????</param-value>??
  • ???????</init-param>??
  • </filter>??
  • <filter-mapping>??
  • ????<filter-name>struts2x</filter-name>??
  • ????<url-pattern>*.action</url-pattern>??
  • </filter-mapping>??
  • <!--?Struts2.x前端控制器配置結(jié)束???-->??
    • FilterDispatcherStruts2前端控制器為FilterDispatcher,是Filter實(shí)現(xiàn),不是Servlet;
    • config通過初始化參數(shù)config指定配置文件為struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml;如果不知道該參數(shù)則默認(rèn)加載struts-default.xml,struts-plugin.xml,struts.xml(位于webapp/WEB-INF/classes下);顯示指定時需要將struts-default.xml,struts-plugin.xml也添加上。
    • *.action將攔截以“.action”結(jié)尾的HTTP請求;
    • struts2xFilterDispatcher前端控制器的名字為struts2x,因此相應(yīng)的Spring配置文件名為struts2x-servlet.xml。

    ?

    7、執(zhí)行測試,在Web瀏覽器中輸入http://localhost:8080/hello.action可以看到“Hello Spring”信息說明Struts2集成成功。

    ?

    ?

    ?

    集成Strut2也是非常簡單,在此我們總結(jié)一下吧:

    • 配置文件位置:

    ? ? ? ? ?Struts配置文件默認(rèn)加載“struts-default.xml,struts-plugin.xml, struts.xml”,其中struts-default.xml和struts-plugin.xml是Struts自帶的,而struts.xml是我們指定的,默認(rèn)位于webapp/WEB-INF/classes下;

    ? ? ? ? ?如果需要將配置文件放到其他位置,需要在web.xml的<filter>標(biāo)簽下,使用初始化參數(shù)config指定,如“struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml”,其中“struts-default.xml和struts-plugin.xml”是不可省略的,默認(rèn)相對路徑是類路徑。

    • 集成關(guān)鍵ObjectFactory在Struts配置文件或?qū)傩晕募惺褂萌缦屡渲弥朗褂肧trutsSpringObjectFactory來獲取Action實(shí)例:

    ?在struts.xml中指定:

    ?

    Java代碼??
  • <constant?name="struts.objectFactory"?value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>??
  • ?

    或在struts.properties文件(webapp/WEB-INF/classes/)中:

    ?

    java代碼: Java代碼??
  • struts.objectFactory=org.apache.struts2.spring.StrutsSpringObjectFactory??
  • ?

    • 集成關(guān)鍵Action定義:

    ? ? ? ? ? ? ? ? StrutsSpringObjectFactory將根據(jù)Struts2配置文件中的<action class=””>標(biāo)簽的classes屬性名字去到Spring配置文件中查找同名的Bean定義,這也是集成的關(guān)鍵。

    ?

    ?

    • Spring配置文件中Action定義:由于Struts2的Action是有狀態(tài)的,因此應(yīng)該將Bean定義為prototype。

    ?

    如圖10-5,Sturt2與Spring集成的關(guān)鍵就是StrutsSpringObjectFactory,注意圖只是說明Struts與Spring如何通過中介者StrutsSpringObjectFactory來實(shí)現(xiàn)集成,不能代表實(shí)際的類交互。

    ?

    總結(jié)

    以上是生活随笔為你收集整理的spring集成struts2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。