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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Struts2学习笔记——Struts2与Spring整合

發(fā)布時(shí)間:2025/6/15 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2学习笔记——Struts2与Spring整合 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Struts2與Spring整合后,可以使用Spring的配置文件applicationContext.xml來(lái)描述依賴(lài)關(guān)系,在Struts2的配置文件struts.xml來(lái)使用Spring創(chuàng)建的bean。

?

1、導(dǎo)入依賴(lài)包

除了導(dǎo)入Struts2和Spring的核心庫(kù)之外,還要導(dǎo)入commons-logging和struts2-spring-plugin包,否則啟動(dòng)會(huì)出異常

?

2、web.xml的配置

既然有Struts2,核心攔截器的配置是不可少的

<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern> </filter-mapping>

?

通過(guò)配置ContextLoaderListener監(jiān)聽(tīng)器,使容器啟動(dòng)時(shí),自動(dòng)加載applicationContext配置,

因?yàn)樗鼘?shí)現(xiàn)了ServletContextListener這個(gè)接口,容器啟動(dòng)時(shí)會(huì)自動(dòng)執(zhí)行它實(shí)現(xiàn)的方法。

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

?

默認(rèn)情況下,會(huì)加載WEB-INF/applicationContext.xml這個(gè)文件,我們可以通過(guò)配置contextConfigLocation參數(shù)改變配置文件的路徑

?

?

<context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/applicationContext.xml</param-value> </context-param>

以上配置均在web.xml文件的<web-app></web-app>區(qū)域

?

3、測(cè)試類(lèi)

在瀏覽器請(qǐng)求一個(gè)Action方法,在Action方法內(nèi)向一個(gè)對(duì)象請(qǐng)求一個(gè)List,然后轉(zhuǎn)到index.jsp頁(yè)面,在頁(yè)面中輸出Action請(qǐng)求到的List。

通過(guò)Spring依賴(lài)配置,控制Action請(qǐng)求的對(duì)象。

首先要編寫(xiě)一個(gè)接口,Action方法依賴(lài)這個(gè)接口,通過(guò)調(diào)用接口中的方法獲取List

public interface IocTestInterface {public List getList(); }

下面編寫(xiě)Action類(lèi),這個(gè)類(lèi)繼承ActionSupport類(lèi),并覆蓋其中的execute方法,

execute方法執(zhí)行時(shí),調(diào)用實(shí)現(xiàn)了上述接口對(duì)象的getList方法獲取List

public class IocAction extends ActionSupport {private IocTestInterface iti;private List list;public List getList() {return list;}public void setList(List list) {this.list = list;}public IocTestInterface getIti() {return iti;}public void setIti(IocTestInterface iti) {this.iti = iti;}public String execute() throws Exception {this.setList(iti.getList());return super.execute();} }

編寫(xiě)用來(lái)顯示運(yùn)行結(jié)果的jsp文件

遍歷list,并將每個(gè)元素作為一行來(lái)顯示

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><body>This is my JSP page. <br><br><s:iterator value="list" id="current"><li><s:property value="current"/></li></s:iterator></body> </html>

系統(tǒng)的結(jié)構(gòu)就是這樣。下面編寫(xiě)兩個(gè)實(shí)現(xiàn)IocTestInterface接口的類(lèi),用來(lái)提供數(shù)據(jù)

public class IocTestImpl implements IocTestInterface {public List getList() {List l = new ArrayList();l.add("abc");l.add("def");l.add("hig");return l;} }

?

public class IocTest2Impl implements IocTestInterface {public List getList() {List l = new ArrayList();l.add("123");l.add("456");l.add("789");return l;} }

4、編寫(xiě)applicationContext.xml配置依賴(lài)關(guān)系

<beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean name="IocAction" class="sy.struts2.ioc.IocAction"><property name="iti"><bean class="sy.struts2.ioc.IocTestImpl"></bean></property></bean></beans>

文件配置了id為IocAction的bean,路徑為剛剛編寫(xiě)的Action類(lèi)的路徑,其中iti對(duì)象(請(qǐng)求數(shù)據(jù)的對(duì)象)配置為IocTestImpl(這里使用了匿名bean)

?

5、編寫(xiě)struts.xml配置文件

首先要告知Struts 2運(yùn)行時(shí)使用Spring來(lái)創(chuàng)建對(duì)象

在<struts></struts>區(qū)域加入以下配置

<constant name="struts.objectFactory" value="spring" />

創(chuàng)建package并配置Action

<package name="hs" extends="struts-default"><action name="ioc" class="IocAction"><result>/index.jsp</result></action> </package>

?

6、發(fā)布并運(yùn)行

發(fā)布后啟動(dòng)Tomcat,用瀏覽器打開(kāi)地址http://localhost:8080/StrutsIoc/ioc.action,獲得了下面的頁(yè)面

修改spring配置文件applicationContext.xml中配置

<bean name="IocAction" class="sy.struts2.ioc.IocAction"><property name="iti"><bean class="sy.struts2.ioc.IocTest2Impl"></bean> </property> </bean>

只是將注入到IocAction中的IocTestImpl修改為IocTest2Impl,也就是使用了另一個(gè)實(shí)現(xiàn)了IocTestInterface接口的類(lèi)

重啟服務(wù)器,再次打開(kāi)剛才的地址

這也就是spring的“控制反轉(zhuǎn)”

總結(jié)

以上是生活随笔為你收集整理的Struts2学习笔记——Struts2与Spring整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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