java structs spring_Java进阶知识20 Struts2和Spring整合在一起

1、概述

1、Spring负责对象创建

2、Struts2负责用Action处理请求

3、整合的关键点:让Struts2框架Action对象的创建交给Spring完成。

2、整合实例

需要用到的 jar包

152fea8e55461ad745b678387bde68fc.png    

560d140f3984ff3a9e52e0bf34e7f6ba.png 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 page

14

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 page

13

14

15

16

17

18

19

22

23

24

25

26 登录成功!27

28

测试结果图:

a8ba02f9cd3a11ba005d82c4dea87088.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值