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

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>



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

到此,基本的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方法。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值