Spring之WEB模塊配置詳解
Spring的WEB模塊用于整合Web框架,例如Struts1、Struts2、JSF等
整合Struts1
繼承方式
Spring框架提供了ActionSupport類(lèi)支持Struts1的Action。繼承了ActionSupport后就能獲取Spring的BeanFactory,從而獲得各種Spring容器內(nèi)的各種資源
import org.springframework.web.struts.ActionSupport;
public class CatAction extends ActionSupport{
public ICatService getCarService(){
return (ICatService) getWebApplicationContext().getBean("catService");
}
public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
CatForm catForm = (CatForm) form;
if("list".equals(catForm.getAction())){
returnthis.list(mapping,form,request,response);
}
}
public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
CatForm catForm = (CatForm) form;
ICatService catService =getCatService();
List<Cat> catList =catService.listCats();
request.setAttribute("carList",catList);
return mapping.find("list");
}
}
Spring在web.xml中的配置
<context-param><!-- Spring配置文件的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener><!-- 使用Listener加載Spring配置文件-->
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter><!-- 使用Spring自帶的字符過(guò)濾器-->
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果與Hibernate結(jié)合使用,需要在web.xml中添加OpenSessionInViewFilter過(guò)濾器,將session范圍擴(kuò)大到JSP層,防止拋出延遲加載異常
<filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support. OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name> hibernateFilter</filter-name> <url-pattern>*.do</url-pattern><!-- 對(duì)Struts 1的Action啟用--> </filter-mapping>
代理方式
繼承方式融入Spring非常簡(jiǎn)單,但是缺點(diǎn)是代碼與Spring發(fā)生了耦合,并且Action并沒(méi)有交給Spring管理,因此不能使用Spring的AOP、IoC特性,使用代理方式則可以避免這些缺陷
public class CatAction extends Action{ //此處繼承的Struts 1的Action
private ICatService catService;
//setter、getter略
public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
CatForm catForm = (CatForm) form;
if("list".equals(catForm.getAction())){
returnthis.list(mapping,form,request,response);
}
}
public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
CatForm catForm = (CatForm) form;
ICatService catService =getCatService();
List<Cat> catList =catService.listCats();
request.setAttribute("carList",catList);
return mapping.find("list");
}
}
這個(gè)Action沒(méi)有與Spring發(fā)生耦合,只是定義了一個(gè)ICatService屬性,然后由Spring負(fù)責(zé)注入
struts-congfig.xml配置
<form-beans>
<form-bean name="catForm" type="com.clf.spring.CatForm">
</form-beans>
<action-mappings>
<action name=" catForm" path="/cat" type="com.clf.spring.CatAction">
<forward name="list" path="/jsp/listCat.jsp"></forward>
</action>
</action-mappings>
<!-- 最核心的配置,該配置把Struts的Action交給Spring代理-->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<!-- controller配置生效后,Action的type屬性就是去作用了,Struts不會(huì)用type屬性指定的類(lèi)來(lái)創(chuàng)建CatAction,而是到Spring配置中尋找,因此Spring中必須配置CatAction -->
<!-- Spring中配置Action使用的是name屬性而不是id,Spring會(huì)截獲"/cat.do"的請(qǐng)求,將catService通過(guò)setter方法注入到CatAction中,并調(diào)用execute()方法-->
<bean name="/cat" class=" com.clf.spring.CatAction">
<property name="catService" ref="catService" />
</bean>
web.xml的配置與上面的繼承方式相同
使用代理方式的Action可以配置攔截器等Spring特性,例如給CatAction配置方法前攔截器和返回后攔截器
<bean id="catBeforeInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor">
<property name="advice">
<bean class="com.clf.spring.MethodBeforeInterceptor" />
</property>
<property name="mappedName" value="*"></property>
</bean>
<bean id="catAfterInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor">
<property name="advice">
<bean class="com.clf.spring.MethodAfterInterceptor" />
</property>
<property name="mappedName" value="*"></property>
</bean>
<bean name="/cat" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<list>
<value> catBeforeInterceptor</value>
<value> catAfterInterceptor</value>
</list>
</property>
<property name="target">
<bean class="com.clf.spring.CatAction">
<property name="catService" ref="catService"></property>
</bean>
</property>
</bean>
整合Struts 2
Spring整合Struts 2需要struts2-spring-2.011.jar包
public class CatAction{
private ICatService catService;
private Cat cat;
//setter、getter略
public String list(){
catService.listCats();
return "list";
}
public String add(){
catService.createCat(cat);
return list();
}
}
struts.xml配置
除了正常的配置之外,還需要<contstant/>添加名為struts.objectFactory的常量,把值設(shè)為spring,表示該Action由Spring產(chǎn)生。然后把<action/>的class屬性改為catAction,Struts2將會(huì)到Spring中尋找名為catAction的bean
<constant name=" struts.objectFactory" value="spring" />
<packagenamepackagename="cat" extends="struts-default">
<action name="*_cat" method="{1}" class="catAction">
<param name="action" >{1}</param>
<result>/list.jsp</result>
<result name="list">/list.jsp</result>
</action>
</package>
Spring配置
<bean id="catAction" scope="prototype" class="com.clf.spring.CatAction"> <property name="catService" ref="catService"></property> </bean>
web.xml配置
<context-param><!-- Spring配置文件的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener><!-- 使用Listener加載Spring配置文件-->
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
總結(jié)
以上就是本文關(guān)于Spring之WEB模塊配置詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。
參考:
淺談Springmvc中的頁(yè)面跳轉(zhuǎn)問(wèn)題
Spring框架web項(xiàng)目實(shí)戰(zhàn)全代碼分享
- 深入淺析Spring-boot-starter常用依賴模塊
- SpringBoot 監(jiān)控管理模塊actuator沒(méi)有權(quán)限的問(wèn)題解決方法
- Spring之ORM模塊代碼詳解
- Spring中MVC模塊代碼詳解
- Spring框架七大模塊簡(jiǎn)單介紹
- SpringBoot創(chuàng)建maven多模塊項(xiàng)目實(shí)戰(zhàn)代碼
- 詳解Maven 搭建spring boot多模塊項(xiàng)目(附源碼)
- 淺談springboot多模塊(modules)開(kāi)發(fā)
- jsp、struts、spring、mybatis實(shí)現(xiàn)前端頁(yè)面功能模塊化拆分的方案
- 七個(gè)Spring核心模塊詳解
相關(guān)文章
SpringBoot 多數(shù)據(jù)源及事務(wù)解決方案小結(jié)
本文主要介紹了多數(shù)據(jù)源管理的解決方案(應(yīng)用層事務(wù),而非XA二段提交保證),以及對(duì)多個(gè)庫(kù)同時(shí)操作的事務(wù)管理,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
IntelliJ IDEA配置Tomcat(完整版圖文教程)
這篇文章主要介紹了IntelliJ IDEA配置Tomcat(完整版圖文教程),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Spring?Boot虛擬線程Webflux在JWT驗(yàn)證和MySQL查詢性能比較
這篇文章主要為大家介紹了Spring Boot虛擬線程與Webflux在JWT驗(yàn)證和MySQL查詢上的性能比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Java concurrency之LockSupport_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency之LockSupport的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
MybatisPlus 自動(dòng)填充的實(shí)現(xiàn)
這篇文章主要介紹了MybatisPlus 自動(dòng)填充的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
SpringBoot基于自定義注解實(shí)現(xiàn)切面編程
這篇文章主要介紹了SpringBoot基于自定義注解實(shí)現(xiàn)切面編程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
關(guān)于spring?boot使用?jdbc+mysql?連接的問(wèn)題
這篇文章主要介紹了spring?boot使用?jdbc+mysql?連接,在這里mysql?8.x版本驅(qū)動(dòng)包,要使用?com.mysql.cj.jdbc.Driver作為驅(qū)動(dòng)類(lèi),文中給大家詳細(xì)介紹,需要的朋友可以參考下2022-03-03

