原文轉自: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類。
- package demo.myssh.business;
- public class UserService {
- public String doing()
- {
- return "UserService working";
- }
- }
3、爲了讓UserService能夠注入到UserAction,還需要爲UserAction添加註入用的屬性和方法
- package demo.myssh.action;
- import com.opensymphony.xwork2.ActionSupport;
- import demo.myssh.business.UserService;
- @SuppressWarnings("serial")
- public class UserAction extends ActionSupport {
- @Override
- public String execute() throws Exception {
- this.addActionMessage("UserAction working");
- // this.addActionMessage("hello world.");
- this.addActionMessage(userService.doing());// 修改下,確認注入成功。
- return ActionSupport.SUCCESS;
- }
- // 注入用屬性
- private UserService userService;
- // 注入用的方法
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
- }
4、在web.xml文件中加入spring的監聽器
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <display-name>myssh</display-name>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <!-- 加入spring的監聽器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
5、修改struts.xml文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="mysshdemo" extends="struts-default">
- <action name="user" class="userAction">
- <result>/index.jsp</result>
- </action>
- </package>
- </struts>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="userAction" class="demo.myssh.action.UserAction">
- <property name="userService" ref="userService" />
- </bean>
- <bean id="userService" class="demo.myssh.business.UserService"></bean>
- </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方法。