<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    本站不再更新,歡迎光臨 java開發技術網
    隨筆-230  評論-230  文章-8  trackbacks-0

    整合 Spring 和 Strust2 一起工作

    關鍵字:   struts2 spring    
    在Struts2中整合Spring的IoC支持是一件十分簡單的事情。讓我們一步一步來實現:
    1)復制struts2-spring-plugin-x-x-x.jar和相應的spring.jar到/WEB-INF/lib目錄下。
    2)在struts.properties中設置struts.objectFactory屬性值

    struts.properties
    1. struts.objectFactory = spring  

    或者是在XML文件中進行常量配置

    struts.xml
    1. <struts>  
    2.     <constant name="struts.objectFactory" value="spring" />  
    3. </struts>  

    3)配置Spring監聽器

    web.xml
    1. <listener>  
    2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    3. </listener>  

    通過Spring配置來注冊對象

    applicationContext.xml
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE beans PUBLIC    
    3.     "-//SPRING//DTD BEAN//EN"   
    4.     "http://www.springframework.org/dtd/spring-beans.dtd">  
    5. <beans default-autowire="autodetect">  
    6.     <bean id="hello" class="hpfyeah.struts2.spring.HelloWorldAction"/>  
    7. </beans>  

    當然你也可以擁有更多的Spring配置文件。在web.xml中進行下列設置,從而使Spring的ApplicationContext通過匹配所給定模式的文件來初始化對象

    web.xml
    1. <!-- 用來定位Spring XML文件的上下文配置 -->  
    2. <context-param>  
    3.     <param-name>contextConfigLocation</param-name>  
    4.     <param-value>
    5.         /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
    6.     </param-value>  
    7. </context-param>  

    4)修改你的Struts配置文件

    struts.xml
    1. <!DOCTYPE struts PUBLIC      
    2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"      
    3.     "http://struts.apache.org/dtds/struts-2.0.dtd">     
    4. <struts>     
    5.     <include file="struts-default.xml"/>     
    6.     <package name="default" extends="struts-default">     
    7.         <action name="pureStruts" class="hpfyeah.struts2.spring.HelloWorldAction">
    8.             <result>hello.jsp</result>     
    9.         </action>  
    10.         <action name="springStruts" class="hello">      
    11.             <result>hello.jsp</result>     
    12.         </action>     
    13. </struts>  

    默認情況下,Spring從上面顯示的applicationContext.xml文件中尋找為hello所做的配置

    5)好了,現在你的Struts2和Spring就能正常的一起工作了。有幾個配置技術點需要詳細說明下:
    裝配模式。你可以通過設置修改struts.properties中下列屬性的值來改變裝配模式。

    name 按照你的action的屬性的名字和Spring里的bean的名字匹配,如果匹配就自動裝配。這是缺省的
    type 按照你的action的屬性的類型,在Spring注冊的bean中查找,如果相同就自動裝配。這需要你在Spring中僅注冊了一個此類型的bean
    auto Spring會試圖自動監測來找到最好的方法自動裝配你的action
    constructor Spring會自動裝配bean的構造函數的參數

    是否使用類緩存。你可以通過設置修改struts.properties中下列屬性的值來改變是否使用Spring自身的類緩存機制。可以設定的值為true或false,默認為true。

    struts.properties
    1. struts.objectFactory.spring.useClassCache = false 
    按照以上步驟做了,但出錯、異常信息如下
    Messages:
    1. springhello
    2. Action class [springhello] not found
    File: com/caucho/loader/DynamicClassLoader.java
    Line number: 1,124
    posted on 2007-10-24 17:54 有貓相伴的日子 閱讀(2651) 評論(2)  編輯  收藏 所屬分類: spring

    評論:
    # re: struts2和spring2整合(轉) 2007-11-12 23:04 | 有貓相伴的日子
    找到原因了,修改web.xml文件,把
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml
    </param-value>
    </context-param>

    放最前面  回復  更多評論
      
    # re: struts2和spring2整合(轉) 2008-07-16 14:36 | vist
    @有貓相伴的日子
    這樣能行,8可能吧~??  回復  更多評論
      
    本站不再更新,歡迎光臨 java開發技術網
    主站蜘蛛池模板: 久久av免费天堂小草播放| 亚洲性色高清完整版在线观看| 亚洲精品成a人在线观看☆| 国产成人精品久久免费动漫| 亚洲综合在线视频| 在线人成免费视频69国产| 亚洲第一极品精品无码久久| 黄页免费在线观看| 91亚洲va在线天线va天堂va国产| 三年片在线观看免费观看大全动漫| 阿v视频免费在线观看| 日韩免费一区二区三区| 含羞草国产亚洲精品岁国产精品| 成人永久免费福利视频网站| 在线观看亚洲视频| 亚洲精品黄色视频在线观看免费资源 | 国产中文在线亚洲精品官网| 有色视频在线观看免费高清在线直播| 亚洲av午夜精品一区二区三区| 一级日本高清视频免费观看| 亚洲精品白浆高清久久久久久| 99在线热视频只有精品免费| 亚洲不卡中文字幕| 国产精品麻豆免费版| 免费一级毛片在线播放视频免费观看永久| 亚洲国产中文字幕在线观看 | 美女被免费视频网站| 国产亚洲精午夜久久久久久| 成人久久免费网站| 亚洲一区二区三区91| 又色又污又黄无遮挡的免费视| 西西人体免费视频| 亚洲人成在久久综合网站| 免费人成无码大片在线观看| 日本免费中文视频| 亚洲一区无码中文字幕| 亚洲免费闲人蜜桃| 欧亚一级毛片免费看| 久久亚洲精品中文字幕| 在线免费观看国产视频| 免费看搞黄视频网站|