Spring与Struts2 的整合使用

项目结构

  Spring与Struts2 的整合使用-LMLPHP

  导进相关的依赖包:pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com</groupId>
<artifactId>spring-web-struts2</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打成war包-->
<packaging>war</packaging> <dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.12</version>
</dependency> <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.12</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>

struts2 与spring整合的步骤

  1:配置监听器ContextLoaderListener,以便创建出Spring的容器对象web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--配置监听器ContextLoaderListener,以便创建出Spring的容器对象-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--访问的配置路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationCtx.xml</param-value>
</context-param> <!--struts2过滤器-->
<filter>
<filter-name>sss</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sss</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

  2:添加struts-spring的依赖,以便改变默认的Struts的实例化Action类规则(上面已配置)

  1. 因为读取到的值就是bean的名字

  2. 利用WebApplicationContextUtils的方法就可以实例化action类

  3. 实例化action类的时候,就可以注入依赖的Service类型

     <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.12</version>
</dependency>

  创建UserDao接口

 package dao;

 public interface UserDao {
void insert();
}

  实现UserDao:UserDaoImpl

 package dao;

 public class UserDaoImpl implements UserDao {
public void insert() {
System.out.println("dao insert----");
}
}

  创建UserService接口

 package service;

 public interface UserService {
void insert();
}

  UserServiceImpl

 package service;

 import dao.UserDao;

 public class UserServiceImpl implements UserService {
private UserDao userDao ; public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void insert() {
userDao.insert();
}
}

  把UserDaoImpl和UserServiceImpl让Spring管理:applicationCtx.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.xsd"> <bean id="userDao" class="dao.UserDaoImpl"></bean>
<bean id="userService" class="service.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean> <!--记得设定Action(也就是Controller)的设置,要设置为非单利,一般就设置为prototype-->
<bean id="sencondController" class="com.SecondController" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
</beans>

  配置struts并且让Spring管理

 <?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="demo" extends="struts-default">
<action name="second" class="sencondController">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>

Spring与Struts2 的整合使用-LMLPHP

  SecondController

 package com;

 import service.UserService;

 public class SecondController {

     public SecondController() {
System.out.println("second contructor-----");
} private UserService userService; public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
} public String execute(){
System.out.println("execute----");
userService.insert();
return "success";
} }

个人笔记,请勿喷

05-26 05:04