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

SSH框架学习(三、在struts基础上加入spring)_Spring


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>





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

<?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

SSH框架学习(三、在struts基础上加入spring)_spring_02

到此,基本的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来管理的好,逻辑上更清晰,也不容易出现奇怪的问题。

SSH框架学习(三、在struts基础上加入spring)_Spring_03


2、spring配置文件中,注入类不是直接使用其类名,而是通过bean的id来获取。要注入哪个类,就在ref中填入那个的bean的id。

SSH框架学习(三、在struts基础上加入spring)_JSP_04

3、spring配置中注入属性的名称必须和java类里的属性名一致,并且,该属性还要提供set方法。

SSH框架学习(三、在struts基础上加入spring)_spring_05