SSH框架學習(二、在struts基礎上加入spring)

原文轉自:http://blog.csdn.net/wuyt2008/article/details/8236269

spring現在的版本是3.1.3,下載地址http://www.springsource.org/spring-framework#download 

spring的包裏面沒有提供例子,額,這點不如struts。


1、導入包。

首先需要添加struts的spring插件:struts2-spring-plugin-2.3.7.jar

其次,spring還需要一個外部的jar:commons-logging-1.1.1.jar,下載地址http://commons.apache.org/logging/download_logging.cgi

在這裏,spring只需要以下幾個jar



2、新建packa:demo.myssh.business,添加UserService類。

[java] view plaincopy
  1. package demo.myssh.business;  
  2.   
  3. public class UserService {  
  4.   
  5.     public String doing()  
  6.     {  
  7.         return "UserService working";  
  8.     }  
  9. }  

3、爲了讓UserService能夠注入到UserAction,還需要爲UserAction添加註入用的屬性和方法

[java] view plaincopy
  1. package demo.myssh.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import demo.myssh.business.UserService;  
  5.   
  6. @SuppressWarnings("serial")  
  7. public class UserAction extends ActionSupport {  
  8.   
  9.     @Override  
  10.     public String execute() throws Exception {  
  11.   
  12.         this.addActionMessage("UserAction working");  
  13.         // this.addActionMessage("hello world.");  
  14.         this.addActionMessage(userService.doing());// 修改下,確認注入成功。  
  15.   
  16.         return ActionSupport.SUCCESS;  
  17.     }  
  18.   
  19.     // 注入用屬性  
  20.     private UserService userService;  
  21.   
  22.     // 注入用的方法  
  23.     public void setUserService(UserService userService) {  
  24.         this.userService = userService;  
  25.     }  
  26.   
  27. }  

4、在web.xml文件中加入spring的監聽器

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  6.     <display-name>myssh</display-name>  
  7.   
  8.     <filter>  
  9.         <filter-name>struts2</filter-name>  
  10.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</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.     <!-- 加入spring的監聽器 -->  
  19.     <listener>  
  20.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  21.     </listener>  
  22.   
  23.     <welcome-file-list>  
  24.         <welcome-file>index.jsp</welcome-file>  
  25.     </welcome-file-list>  
  26.   
  27. </web-app>  


5、修改struts.xml文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5. <struts>  
  6.     <package name="mysshdemo" extends="struts-default">  
  7.         <action name="user" class="userAction">  
  8.             <result>/index.jsp</result>  
  9.         </action>  
  10.     </package>  
  11. </struts>  



6、在WEB-INF目錄下新建applicationContext.xml文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  6.   
  7.     <bean id="userAction" class="demo.myssh.action.UserAction">  
  8.         <property name="userService" ref="userService" />  
  9.     </bean>  
  10.   
  11.     <bean id="userService" class="demo.myssh.business.UserService"></bean>  
  12. </beans>  

這時候訪問:http://localhost:8080/user.action

到此,基本的struts2+spring3搭建完成。


要點:這裏有3個對應關係一定要注意

1、struts配置文件中,action的class屬性要對應到spring配置文件中的bean的id,這樣,struts就只負責傳遞,而所有servlet都有spring來管理。

我試過,struts.xml中,

不用<action name="user" class="userAction">

用<action name="user" class="demo.myssh.action.UserAction">也能正常運行。但是奇怪的是我之前如果這麼用就無法注入。不確定是struts版本的問題還是哪裏的問題。

個人理解,如果class裏面配置的是個id,那麼,action將有spring來直接管理。如果class裏面配置的是類名,則action將有struts來處理。

推薦還是將action交給spring來管理的好,邏輯上更清晰,也不容易出現奇怪的問題。


2、spring配置文件中,注入類不是直接使用其類名,而是通過bean的id來獲取。要注入哪個類,就在ref中填入那個的bean的id。

3、spring配置中注入屬性的名稱必須和java類裏的屬性名一致,並且,該屬性還要提供set方法。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章