一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Spring之WEB模塊配置詳解

Spring之WEB模塊配置詳解

2021-02-27 11:18冰河winner JAVA教程

這篇文章主要介紹了Spring之WEB模塊配置詳解,簡單介紹了其繼承方式,代理方式,以及相關詳細配置代碼,具有一定借鑒價值,需要的朋友可以了解下。

Spring框架七大模塊簡單介紹

Spring中MVC模塊代碼詳解

Spring的WEB模塊用于整合Web框架,例如Struts1、Struts2、JSF等

整合Struts1

繼承方式

Spring框架提供了ActionSupport類支持Struts1的Action。繼承了ActionSupport后就能獲取Spring的BeanFactory,從而獲得各種Spring容器內的各種資源

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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中的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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自帶的字符過濾器-->
  <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結合使用,需要在web.xml中添加OpenSessionInViewFilter過濾器,將session范圍擴大到JSP層,防止拋出延遲加載異常

?
1
2
3
4
5
6
7
8
<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><!-- 對Struts 1的Action啟用-->
</filter-mapping>

代理方式

繼承方式融入Spring非常簡單,但是缺點是代碼與Spring發生了耦合,并且Action并沒有交給Spring管理,因此不能使用Spring的AOP、IoC特性,使用代理方式則可以避免這些缺陷

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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");
  }
}

這個Action沒有與Spring發生耦合,只是定義了一個ICatService屬性,然后由Spring負責注入

struts-congfig.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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不會用type屬性指定的類來創建CatAction,而是到Spring配置中尋找,因此Spring中必須配置CatAction -->
<!-- Spring中配置Action使用的是name屬性而不是id,Spring會截獲"/cat.do"的請求,將catService通過setter方法注入到CatAction中,并調用execute()方法-->
<bean name="/cat" class=" com.clf.spring.CatAction">
  <property name="catService" ref="catService" />
</bean>

web.xml的配置與上面的繼承方式相同

使用代理方式的Action可以配置攔截器等Spring特性,例如給CatAction配置方法前攔截器和返回后攔截器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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的常量,把值設為spring,表示該Action由Spring產生。然后把<action/>的class屬性改為catAction,Struts2將會到Spring中尋找名為catAction的bean

?
1
2
3
4
5
6
7
8
9
<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配置

?
1
2
3
<bean id="catAction" scope="prototype" class="com.clf.spring.CatAction">
  <property name="catService" ref="catService"></property>
</bean>

web.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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>

總結

以上就是本文關于Spring之WEB模塊配置詳解的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。

參考:

Spring框架web項目實戰全代碼分享

原文鏈接:http://blog.csdn.net/u012152619/article/details/44258343

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩视频免费一区二区三区 | 精品视频国产 | 美国女网址www呦女 美国复古性经典xxxxx | 国产精品成人一区二区1 | 1313午夜精品理伦片 | 国产高清视频在线 | 美女大逼逼 | 亚洲欧美国产另类 | 天作谜案免费完整版在线观看 | 亚洲品质水蜜桃 | 我的家教老师 | www.天天操| 日韩 国产 欧美 | 日本老妇成熟 | 黄+色+性+人免费 | 香蕉动漫库 | 国产欧美日韩精品一区二 | 国产在线精品成人一区二区三区 | 冰雪奇缘1完整版免费观看 变形金刚第一部 | 视频在线网站 | 好妈妈7在线观看高清 | 激情小视频 | 我的美女奴隶 | 教室里的激情电影 | 国产精品合集久久久久青苹果 | 国产一区二区三区水野朝阳 | 女人是男人的未来1分49分 | 极品ts赵恩静和直男激战啪啪 | 美女跪式抽搐gif动态图 | 亚洲精品第五页中文字幕 | 免费看60分钟大片视频播放 | 秋霞在线观看成人高清视频51 | 国内精品伊人久久大香线焦 | 国产色网址 | 嫩草影院永久在线一二三四 | 欠操h| 天天夜夜草草久久伊人天堂 | 精品国产自在在线在线观看 | 91国内在线国内在线播放 | 欧美巨胸 | 色综合综合 |