1、概述
1、Spring负责对象创建
2、Struts2负责用Action处理请求
3、整合的关键点:让Struts2框架Action对象的创建交给Spring完成。
2、整合实例
需要用到的 jar包
Spring的配置文件(aaa-bbb.xml)建议分层,方便维护。
配置web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7
8
9 index.jsp
10
11
12
13
14 contextConfigLocation
15 /WEB-INF/classes/beans-*.xml
16
17
18 org.springframework.web.context.ContextLoaderListener
19
20
21
22
23 struts2
24 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
25
26
27 struts2
28 /*
29
30
DAO层
1 //接口
2 public interfaceIUserDao {3 publicString getByName();4 }5
6 //实现
7 public class UserDaoImpl implementsIUserDao {8
9 publicString getByName() {10 return "DSHORE";11 }12 }
Service层
1 //接口
2 public interfaceIUserService {3 publicString getByName();4 }5
6 //实现
7 public class UserServiceImpl implementsIUserService {8
9 //注入值,如果没有使用spring框架,则此处需要new对象,否则拿不到值。
10 private IUserDao userDao;//= new UserDaoImpl();
11
12 publicString getByName() {13 returnuserDao.getByName();14 }15
16 publicIUserDao getUserDao() {17 returnuserDao;18 }19 public voidsetUserDao(IUserDao userDao) {20 this.userDao =userDao;21 }22 }
Action层
1 packagecom.shore.action;2
3 importcom.opensymphony.xwork2.ActionSupport;4 importcom.shore.service.IUserService;5
6 /**
7 *@authorDSHORE/2019-10-268 *9 */
10 public class UserAction extendsActionSupport {11 private static final long serialVersionUID = -8197510116737054459L;12
13 //注入值,如果没有使用spring框架,则此处需要new对象,否则拿不到值。
14 private IUserService userService;//= new UserServiceImpl();
15
16 publicString login() {17 System.out.println("用户名:" +userService.getByName());18 returnSUCCESS;19 }20
21 publicIUserService getUserService() {22 returnuserService;23 }24
25 public voidsetUserService(IUserService userService) {26 this.userService =userService;27 }28 }
struts.xml 配置文件 注意:struts.xml 中 action name 和 bean-action.xml 中的 bean id 名称,一定要保持一致!!!
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5
6
7
8
9
10
11
12
13 /success.jsp
14
15
16
beans-dao.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xsi:schemaLocation="7 http://www.springframework.org/schema/beans8 http://www.springframework.org/schema/beans/spring-beans.xsd9 http://www.springframework.org/schema/tx10 http://www.springframework.org/schema/tx/spring-tx.xsd11 http://www.springframework.org/schema/aop12 http://www.springframework.org/schema/aop/spring-aop.xsd">
13
14
15
beans-service.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:p="http://www.springframework.org/schema/p"
7 xsi:schemaLocation="8 http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans.xsd10 http://www.springframework.org/schema/tx11 http://www.springframework.org/schema/tx/spring-tx.xsd12 http://www.springframework.org/schema/aop13 http://www.springframework.org/schema/aop/spring-aop.xsd">
14
15
16
beans-action.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:p="http://www.springframework.org/schema/p"
7 xsi:schemaLocation="8 http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans.xsd10 http://www.springframework.org/schema/tx11 http://www.springframework.org/schema/tx/spring-tx.xsd12 http://www.springframework.org/schema/aop13 http://www.springframework.org/schema/aop/spring-aop.xsd">
14
15
16
17
index.jsp 页面
1
2
3
7
8
9
10
11 ">
12
13
My JSP 'index.jsp' starting page14
15
16
17
18
19
22
23
24
25
26
27
28
29
30
31
success.jsp 页面
1
2
6
7
8
9
10 ">
11
12
My JSP 'success.jsp' starting page13
14
15
16
17
18
19
22
23
24
25
26 登录成功!27
28
测试结果图: