paulwong

          struts2筆記:與spring2集成


          struts2基本上就是webwork的翻版,所以遷移過(guò)程倒是很簡(jiǎn)單,只需要修改下配置文件和一些包名就可以了。如果在Eclipse、Netbeans這些集成開(kāi)發(fā)工具的幫助下,記不清包名也很容易找到想要的類(lèi)的,呵呵。


          在Eclipse下建立一個(gè)Dynamic Web Application。


          從struts2.0.6的lib目錄中復(fù)制下面的庫(kù)文件到WEB-INF/lib目錄下:
          commons-logging-1.1.jar
          freemarker-2.3.8.jar
          ognl-2.6.9.jar
          struts-api-2.0.6.jar
          struts-core-2.0.6.jar
          struts-spring-plugin-2.0.6.jar
          xwork-2.0.0.jar


          從spring中l(wèi)ib目錄中復(fù)制下面的庫(kù)文件到WEB-INF/lib目錄下:
          spring.jar


          修改web.xml,增加一個(gè)struts的分派器filter,映射所有的url-pattern,再增加一個(gè)spring的ContextLoaderListener監(jiān)聽(tīng)器。修改后的內(nèi)容如下:


          <?xml version="1.0" encoding="UTF-8"?>   
          <web-app id="WebApp_ID" version="2.4"   
              xmlns
          ="http://java.sun.com/xml/ns/j2ee"   
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"   
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
              
          <display-name>struts2tutorial</display-name>   
                  
              
          <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>*.action</url-pattern>   
              
          </filter-mapping>   
                  
              
          <welcome-file-list>   
                  
          <welcome-file>index.jsp</welcome-file>   
              
          </welcome-file-list>   
                  
              
          <listener>   
                  
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
              
          </listener>   
                  
          </web-app>   

           

          寫(xiě)一個(gè)簡(jiǎn)單的Action,HelloWorld:


          package tutorial;    
          import com.opensymphony.xwork2.ActionSupport;    
          public class HelloWorld extends ActionSupport {    
             
              
          public static final String MESSAGE = "Struts is up and running ";    
             
              
          public String execute() throws Exception {    
                  setMessage(MESSAGE);    
                  
          return SUCCESS;    
              }
              
             
              
          private String message;    
             
              
          public void setMessage(String message){    
                  
          this.message = message;    
              }
              
             
              
          public String getMessage() {    
                  
          return message;    
              }
              
          }
             


          在源文件路徑下(項(xiàng)目的src目錄)增加struts.xml配置action。這個(gè)文件是集成spring的關(guān)鍵所在,這里面描述有如何將spring2集成到struts2的相關(guān)信息:


          <!DOCTYPE struts PUBLIC    
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
              "http://struts.apache.org/dtds/struts-2.0.dtd"
          >   
          <struts> 
              
          <!-- 下面這句表明對(duì)象是由spring負(fù)責(zé)產(chǎn)生的.加上這句后,struts會(huì)產(chǎn)生讓spring負(fù)責(zé)產(chǎn)生bean,如果spring不能產(chǎn)生bean,則由struts自己產(chǎn)生.也可以在struts.properties文件內(nèi)定義這個(gè)屬性.--> 
              
          <constant name="objectFactory" value="spring"></constant>   
              
          <package name="struts2tutoial" extends="struts-default" namespace="/">   
                  
          <!-- 注意,現(xiàn)在action的class屬性不再是類(lèi)的名字了,而是在spring中的bean的id,詳細(xì)信息請(qǐng)看下面的spring的bean配置文件applicationContext.xml --> 
                  
          <action name="HelloWorld" class="helloWorld">   
                      
          <result>/helloWorld.jsp</result>   
                  
          </action>   
                  
          <!-- Add your actions here -->   
              
          </package>   
          </struts>   


          在WEB-INF/目錄下增加spring的bean配置文件applicationContext.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="tutorial.HelloWorld"></bean>   
             
          </beans>  


          配置很簡(jiǎn)單,只有一個(gè)bean。

          最后,在WebContent目錄下增加helloWorld.jsp:


          <%@ taglib prefix="s" uri="/struts-tags" %>   
             
          <html>   
              
          <head>   
                  
          <title>Hello World!</title>   
              
          </head>   
              
          <body>   
                  
          <h2><s:property value="message" /></h2>   
              
          </body>   
          </html> 

          posted on 2007-07-16 23:38 paulwong 閱讀(571) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): J2EE

          主站蜘蛛池模板: 沛县| 漠河县| 吉林省| 大荔县| 中牟县| 眉山市| 双流县| 庆元县| 耒阳市| 峡江县| 苗栗县| 德昌县| 长宁区| 永年县| 武城县| 宣化县| 吉木萨尔县| 凤阳县| 沽源县| 兰考县| 白朗县| 浏阳市| 尚义县| 九江市| 梓潼县| 高碑店市| 泗阳县| 长葛市| 延津县| 乐山市| 涞源县| 屏东市| 台北县| 从江县| 布拖县| 潢川县| 延吉市| 连南| 桃江县| 武宁县| 金昌市|