struts与spring的整合

405 阅读1分钟

步骤

  1. 导包 asm-3.3.jar asm操作字节码相关的包

    asm-commons-3.3.jar

    asm-tree-3.3.jar

    commons-fileupload-1.3.2.jar 文件上传

    commons-io-2.2.jar 文件io

    commons-lang-2.1.jar java.lang包的加强支持包

    commons-lang3-3.2.jar

    commons-logging-1.2.jar 通用

    freemarker-2.3.22.jar

    javassist-3.11.0.GA.jar

    ognl-3.0.19.jar

    spring-aop-4.3.10.RELEASE.jar

    spring-beans-4.3.10.RELEASE.jar

    spring-context-4.3.10.RELEASE.jar

    spring-core-4.3.10.RELEASE.jar

    spring-expression-4.3.10.RELEASE.jar 表达式

    spring-web-4.3.10.RELEASE.jar

    struts2-core-2.3.32.jar

    struts2-spring-plugin-2.3.32.jar

    struts对spring支持的插件(很重要),暂时不导入,会报错

    xwork-core-2.3.32.jar

  2. 引入struts2 2.1 在web.xml中引入struts2的核心过滤器

 <!-- 1.引入struts2 -->
  <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>/*</url-pattern>
  </filter-mapping>
2.2 开发action,在struts.xml中配置action
```
public class UserAction {
private IUser service = new UserService();
public IUser getService() {
	return service;
}
public void setService(IUser service) {
	this.service = service;
}
public String login() {
	System.out.println("请求登录");
	service.login();
	return "success";
}

}

  1. 引入spring 3.1 创建主配置文件application-context.xml
    <?xml version="1.0" encoding="UTF-8"?>
<!-- beans spring配置文件的根标签 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">
	
	<!-- dao -->
		<bean id="dao" class="com.dyr.dao.UserDao"></bean>
	<!-- service 自动装配autowire-->	
		<bean id="service" class="com.dyr.service.UserService" autowire="byName"></bean>
	<!-- action -->	
		<bean id="userAction" class="com.dyr.action.UserAction" autowire="byName" scope="prototype"></bean>
		
</beans>

3.2 创建bean类,测试spring是否引入成功

  1. 整合框架 4.1在ewb.xml中引入spring的核心配置文件、监听器

    4.2在配置文件中配置bean,引入struts2-spring-plugin-2.3.32.jar

    4.3配置dao、service、action的bean

  2. 在struts.xml对应的action中将class的值修改为action bean的id

  3. 运行发送请求测试