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

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

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

    shenang博客技術(shù)文檔


    理論不懂就實(shí)踐,實(shí)踐不會(huì)就學(xué)理論!

    posts - 35,comments - 55,trackbacks - 0

     

                       整合Springstruts學(xué)習(xí)筆記

    整合共有三種方法:

    使用 Spring ActionSupport類整合 Structs

    使用 Spring DelegatingRequestProcessor

    覆蓋 Struts RequestProcessor Struts Action管理委托給 Spring 框架

    前面二種很簡單,但不是最優(yōu)化的,最優(yōu)優(yōu)化是第三種,有興趣的同志,可以到下面網(wǎng)址去看看

    http://www.ibm.com/developerworks/cn/java/j-sr2.html#N1008C

    下面,我將以一個(gè)實(shí)例簡單介紹一下二者的整合,以作為個(gè)人備忘

    對于像我一樣的新手,大家一起共同學(xué)習(xí),有錯(cuò)誤的地方,還望指教。

    具體步聚如下:

    1、首先是導(dǎo)入需要的包
     看了很多別人的文章,這里都寫得不太清楚,我覺得這是比較重要的一步,如果包導(dǎo)入不對,程序就高度不出來,所以,我比較看重這步,因?yàn)椴皇撬腥硕际呛芮宄@一步,對于像我這種新手還是有用的,呵呵。

    Spring的二個(gè)包:
    spring.jar
    struts2-spring-plugin-2.0.9.jar(其實(shí)這個(gè)包應(yīng)該是struts的包哈)
    Struts的五個(gè)基本包
    commons-logging-1.1.jar
    freemarker-2.3.8.jar
    ognl.jar
    struts2-core-2.0.9.jar
    xwork-2.0.4.jar
    (當(dāng)然,在我寫的這個(gè)小程序中,有一些struts包是用不到的!導(dǎo)入了包,下一步就開始了!)

    2、配置Spring監(jiān)聽器.
    向web.xml加入下面語句:

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

    下面是完整的web.xml 為了方便大家調(diào)試,我給出全碼如下:

     

     1 <?xml version="1.0" encoding="UTF-8"?>   
     2 <web-app id="WebApp_ID" version="2.4"   
     3     xmlns="http://java.sun.com/xml/ns/j2ee"   
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
     6     <display-name>struts2tutorial</display-name>   
     7         
     8     <filter>   
     9         <filter-name>struts2</filter-name>   
    10         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
    11     </filter>   
    12         
    13     <filter-mapping>   
    14         <filter-name>struts2</filter-name>   
    15         <url-pattern>*.action</url-pattern>   
    16     </filter-mapping>   
    17         
    18     <welcome-file-list>   
    19         <welcome-file>index.jsp</welcome-file>   
    20     </welcome-file-list>   
    21         
    22     <listener>   
    23         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
    24     </listener>   
    25         
    26 </web-app>


    3、利用Spring配置文件來注冊對象

    在原有的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     <!-- 下面這句表明對象是由spring負(fù)責(zé)產(chǎn)生的.加上這句后,struts會(huì)產(chǎn)生讓spring負(fù)責(zé) 
     6         產(chǎn)生bean,如果spring不能產(chǎn)生bean,則由struts自己產(chǎn)生.也可以在struts.properties 
     7         文件內(nèi)定義這個(gè)屬性.-->
     8     <constant name="struts.objectFactory.spring.autoWire" value="type" />
     9     <constant name="struts.objectFactory" value="spring" />
    10     <!--
    11     <constant name="objectFactory" value="spring"></constant>
    12     -->
    13     <package name="struts2tutoial" extends="struts-default"
    14         namespace="/">
    15         <!-- 注意,現(xiàn)在action的class屬性不再是類的名字了,而是在spring中的bean的id 
    16             詳細(xì)信息請看下面的spring的bean配置文件applicationContext.xml -->
    17         <action name="HelloWorld" class="helloWorld">
    18             <result>/helloWorld.jsp</result>
    19         </action>
    20         <!-- Add your actions here -->
    21     </package>
    22 </struts>
    23 

    看到上文中的
    8     <constant name="struts.objectFactory.spring.autoWire" value="type" />
     9     <constant name="struts.objectFactory" value="spring" />
    10     <!--
    11     <constant name="objectFactory" value="spring"></constant>
    12     -->

    這個(gè)地方只需要注釋中的也行!

    4、配置applicationContext.xml

    注意,這個(gè)文件一定要放在WEB-INF下面,否則會(huì)出錯(cuò),默認(rèn)情況下,容器會(huì)到WEB-INF目錄下面去尋找applicationContext.xml文件。如果我們想指定別的地方的配置文件或者指定多個(gè)配置文件,可以通過在web.xml文件中定義 context-param元素來指定,除非你在web.xml作如下配置

     1 <context-param>
     2 
     3     <param-name>contextConfigLocation</param-name>
     4  <!-- 下面指的是文件存放路徑,下面我還用了一個(gè)通配路徑,classpath下的所在形如:applicationContext-XX.xml都引入-->
     5     <param-value>
     6         /WEB-INF/applicationContext.xml,classpath:applicationContext-*.xml
     7     </param-value>
     8 
     9  </context-param>
    10 


    下面是我的appliccationContext.xml全碼:

    <?xml version="1.0" encoding="UTF-8"?>   
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">   
    <beans>   
        
    <bean id="helloWorld" class="com.tianji.test.HelloWorld"></bean>   
    </beans>
    <!--上面的解釋下:在前面說了,這個(gè)bean中的ID,必須與struts中的action指定的class值一樣,此bean中的class就是你要處理的action類,如果你在action時(shí)有傳值,則在此bean中加入形如:
    <property name="message">
        
    <value>i love you</value></property>
    形式的語句!
     
    -->


    5、寫一個(gè)action類
    HelloWord.java

     1 package com.tianji.test;    
     2 import com.opensymphony.xwork2.ActionSupport;    
     3 public class HelloWorld extends ActionSupport {    
     4    
     5     public static final String MESSAGE = "Struts is up and running ";    
     6    
     7     public String execute() throws Exception {    
     8         setMessage(MESSAGE);    
     9         return SUCCESS;    
    10     }    
    11    
    12     private String message;    
    13    
    14     public void setMessage(String message){    
    15         this.message = message;    
    16     }    
    17    
    18     public String getMessage() {    
    19         return message;    
    20     }    
    21 }   
    22 
    23 

    這個(gè)不用解釋哈!

    6、創(chuàng)建HelloWord.jsp

    1 <%@ taglib prefix="s" uri="/struts-tags" %>   
    2 <html>   
    3     <head>   
    4         <title>Hello World!</title>   
    5     </head>   
    6     <body>   
    7         <h2><s:property value="message" /></h2>   
    8     </body>   
    9 </html>  

    發(fā)布,運(yùn)行服務(wù)器,在瀏覽器中打開:
    http://localhost:8080/love/HelloWorld.action

    注意修改上面紅色部份!

    結(jié)果:成功!

    Struts is up and running````````


     

     


     

    posted on 2009-03-25 16:11 重慶理工小子 閱讀(703) 評論(3)  編輯  收藏 所屬分類: Spring2 、Struts2 、技術(shù)整合

    FeedBack:
    # re: 整合Spring和struts學(xué)習(xí)筆記
    2009-03-25 17:54 | 學(xué)習(xí)的人
    好文章,我也在學(xué)習(xí),謝謝樓主!我是第一個(gè)評論的,呵呵!  回復(fù)  更多評論
      
    # re: 整合Spring和struts學(xué)習(xí)筆記
    2009-03-25 17:55 | 關(guān)注
    謝謝,關(guān)注中!  回復(fù)  更多評論
      
    # re: 整合Spring和struts學(xué)習(xí)筆記
    2009-03-25 18:04 | 重慶理工小子
    @學(xué)習(xí)的人

    由于剛學(xué),文章寫得不好!謝謝!

    大家一起進(jìn)步!
      回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲av中文无码字幕色不卡| 成年女人免费视频播放体验区| 亚洲国产精品一区二区三区在线观看| 亚洲精品99久久久久中文字幕| 日本最新免费网站| 中文字幕在线视频免费| 亚洲成在人线在线播放无码| 在线观看亚洲人成网站| 一本色道久久综合亚洲精品高清| 免费无码黄动漫在线观看| 6080午夜一级毛片免费看6080夜福利 | JLZZJLZZ亚洲乱熟无码| 好大好硬好爽免费视频| 亚洲精品国产免费| 久别的草原电视剧免费观看| 精品免费久久久久国产一区 | 国产在线观看免费视频软件| 西西人体大胆免费视频| 亚洲欧美国产日韩av野草社区| 亚洲经典在线中文字幕| 亚洲国产成人久久综合碰碰动漫3d| 国产成人精品日本亚洲专区61| 日韩精品亚洲专区在线观看| 日韩一级视频免费观看| 18禁超污无遮挡无码免费网站国产 | 成人免费无码视频在线网站| 无码国产精品一区二区免费| 亚洲第一网站免费视频| 国产大片91精品免费观看不卡| 99在线观看精品免费99| 一级毛片免费毛片一级毛片免费| a毛片免费全部播放完整成| 高清永久免费观看| 中文字幕免费观看视频| a国产成人免费视频| 国产午夜无码精品免费看| 国产免费一区二区三区不卡 | 亚洲视频人成在线播放| 亚洲偷自拍拍综合网| 在线亚洲精品自拍| 国产亚洲精品岁国产微拍精品|