Spring与Struts2 的整合使用

Spring与Struts2 的整合使用

项目结构

  

再Struts2 中(还没有与Spring整合时),它创建Action类的依据

<action name="second" class="com.SecondController">
   <result name="success">/index.jsp</result>
 </action>

同时还有一个点需要注意的就是,struts会每次请求时自动实例化一个新的Action类的对象

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com</groupId>
 8     <artifactId>spring-web-struts2</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <!--打成war包-->
11     <packaging>war</packaging>
12 
13     <dependencies>
14         <dependency>
15             <groupId>javax.servlet</groupId>
16             <artifactId>javax.servlet-api</artifactId>
17             <version>3.1.0</version>
18             <scope>provided</scope>
19         </dependency>
20 
21         <dependency>
22             <groupId>org.springframework</groupId>
23             <artifactId>spring-context</artifactId>
24             <version>5.0.2.RELEASE</version>
25         </dependency>
26         <dependency>
27             <groupId>org.springframework</groupId>
28             <artifactId>spring-web</artifactId>
29             <version>5.0.2.RELEASE</version>
30         </dependency>
31 
32 
33         <dependency>
34             <groupId>org.apache.struts</groupId>
35             <artifactId>struts2-core</artifactId>
36             <version>2.5.12</version>
37         </dependency>
38 
39         <dependency>
40             <groupId>org.apache.struts</groupId>
41             <artifactId>struts2-spring-plugin</artifactId>
42             <version>2.5.12</version>
43         </dependency>
44     </dependencies>
45 
46     <build>
47         <plugins>
48             <plugin>
49                 <artifactId>maven-war-plugin</artifactId>
50                 <version>2.2</version>
51                 <configuration>
52                     <warSourceDirectory>web</warSourceDirectory>
53                 </configuration>
54             </plugin>
55         </plugins>
56     </build>
57 </project>

struts2 与spring整合的步骤

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5          version="3.1">
 6     <!--配置监听器ContextLoaderListener,以便创建出Spring的容器对象-->
 7     <listener>
 8         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 9     </listener>
10     <!--访问的配置路径-->
11     <context-param>
12         <param-name>contextConfigLocation</param-name>
13         <param-value>classpath:applicationCtx.xml</param-value>
14     </context-param>
15     
16     <!--struts2过滤器-->
17     <filter>
18         <filter-name>sss</filter-name>
19         <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
20     </filter>
21     <filter-mapping>
22         <filter-name>sss</filter-name>
23         <url-pattern>/*</url-pattern>
24     </filter-mapping>
25 </web-app>

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

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

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

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

1     <dependency>
2             <groupId>org.apache.struts</groupId>
3             <artifactId>struts2-spring-plugin</artifactId>
4             <version>2.5.12</version>
5         </dependency>

  创建UserDao接口

1 package dao;
2 
3 public interface UserDao {
4     void insert();
5 }

  实现UserDao:UserDaoImpl

1 package dao;
2 
3 public class UserDaoImpl implements UserDao {
4     public void insert() {
5         System.out.println("dao insert----");
6     }
7 }

  创建UserService接口

1 package service;
2 
3 public interface UserService {
4     void insert();
5 }

  UserServiceImpl

 1 package service;
 2 
 3 import dao.UserDao;
 4 
 5 public class UserServiceImpl implements UserService {
 6     private UserDao userDao ;
 7 
 8     public UserDao getUserDao() {
 9         return userDao;
10     }
11 
12     public void setUserDao(UserDao userDao) {
13         this.userDao = userDao;
14     }
15 
16     public void insert() {
17         userDao.insert();
18     }
19 }

  把UserDaoImpl和UserServiceImpl让Spring管理:applicationCtx.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6 
 7     <bean id="userDao" class="dao.UserDaoImpl"></bean>
 8     <bean id="userService" class="service.UserServiceImpl">
 9         <property name="userDao" ref="userDao"></property>
10     </bean>
11 
12     <!--记得设定Action(也就是Controller)的设置,要设置为非单利,一般就设置为prototype-->
13     <bean id="sencondController" class="com.SecondController"  scope="prototype">
14         <property name="userService" ref="userService"></property>
15     </bean>
16 </beans>

  配置struts并且让Spring管理

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 5         "http://struts.apache.org/dtds/struts-2.5.dtd">
 6 
 7 <struts>
 8 
 9     <package name="demo" extends="struts-default">
10         <action name="second" class="sencondController">
11             <result name="success">/index.jsp</result>
12         </action>
13     </package>
14 </struts>

  SecondController

 1 package com;
 2 
 3 import service.UserService;
 4 
 5 public class SecondController {
 6 
 7 
 8     public SecondController() {
 9         System.out.println("second contructor-----");
10     }
11 
12     private UserService userService;
13 
14     public UserService getUserService() {
15         return userService;
16     }
17 
18     public void setUserService(UserService userService) {
19         this.userService = userService;
20     }
21 
22     public  String execute(){
23         System.out.println("execute----");
24         userService.insert();
25         return "success";
26     }
27 
28 
29 }

 个人笔记,请勿喷

原文地址:https://www.cnblogs.com/sunduge/p/8315615.html