php 与结合struts2,Struts2框架04 struts和spring整合

目录

1servlet 和 filter 的异同

2 内存中的字符编码

3 gbk和utf-8的特点

4 struts和spring的整合

5 struts和spring的整合步骤

6 springIOC在action类中的使用

7 注解回顾

1 servlet 和 filter 的异同

1.1 相同点

filter完全可以代替servlet使用,但是filter中的某些功能servlet不能实现,而servlet的所有功能filter都能实现

1.2 不同点

1.2.1 概念上

servlet 是运行在服务器端的程序,动态生成web页面

filter 是一段可以复用的代码,不能生成请求和响应,但是可以对相应和请求做相应的处理

1.2.2 生命周期上

servlet 是在web服务器启动或web服务器接收到第一次请求时利用构造器实例化servlet,然后调用init方法进行初始化,之后的每次请求会调用doGet或者doPost方法进行处理;当关闭服务器的时候,调用destroy方法销毁实例

filter 是在web服务器启动时利用构造器实例化filter,然后调用init方法进行初始化,之后的每次请求调用doFilter方法进行处理,关闭服务器的时候,调用destroy方法销毁实例

1.2.3 类型不同

servlet是类、filter是接口

1.3 servlet 、filter的编程实现

392c825d5d19689a76cb598def84f800.gif

616a440a9ead84b8464c625ae0aad28f.gif

1 packagecn.xiangxu;2

3 importjava.io.IOException;4 importjava.io.PrintWriter;5

6 importjavax.servlet.ServletException;7 importjavax.servlet.http.HttpServlet;8 importjavax.servlet.http.HttpServletRequest;9 importjavax.servlet.http.HttpServletResponse;10

11 /**

12 * Servlet implementation class DemoServlet13 */

14 public class DemoServlet extendsHttpServlet {15 private static final long serialVersionUID = 1L;16

17 /**

18 *@seeHttpServlet#HttpServlet()19 */

20 publicDemoServlet() {21 super();22 System.out.println("01 利用构造器实例化");23 }24

25 @Override26 public void init() throwsServletException {27 super.init();28 System.out.println("02 利用init方法初始化");29 }30

31 /**

32 *@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)33 */

34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {35 System.out.println("03 调用处理方法");36

37 response.setContentType("text/html;charset:utf-8"); //设置消息头部分信息

38 response.setCharacterEncoding("utf-8");39

40 PrintWriter out =response.getWriter();41 out.println("hello servlet");42 out.close();43 }44

45 @Override46 public voiddestroy() {47 super.destroy();48 System.out.println("04 利用destroy方法销毁");49 }50

51 /**

52 *@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)53 */

54 protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {55 //TODO Auto-generated method stub

56 doGet(request, response);57 }58

59 }

DemoServlet

b3fb06804f825b5fba021d15e1e37fea.gif

51a4c9d8b674ec7486307df330e83094.gif

1 packagecn.xiangxu;2

3 importjava.io.IOException;4 importjava.io.PrintWriter;5

6 importjavax.servlet.Filter;7 importjavax.servlet.FilterChain;8 importjavax.servlet.FilterConfig;9 importjavax.servlet.ServletException;10 importjavax.servlet.ServletRequest;11 importjavax.servlet.ServletResponse;12 importjavax.servlet.http.HttpServletResponse;13

14 /**

15 * Servlet Filter implementation class DemoFilter16 */

17 public class DemoFilter implementsFilter {18

19 /**

20 * Default constructor.21 */

22 publicDemoFilter() {23 System.out.println("01 利用构造器实例化");24 }25

26 /**

27 *@seeFilter#init(FilterConfig)28 */

29 public void init(FilterConfig fConfig) throwsServletException {30 System.out.println("02 利用init方法初始化");31 }32

33 /**

34 *@seeFilter#doFilter(ServletRequest, ServletResponse, FilterChain)35 */

36 public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain) throwsIOException, ServletException {37 System.out.println("03 调用处理方法");38

39 HttpServletResponse response =(HttpServletResponse)resp;40

41 response.setContentType("text/html;charset:utf-8");42 response.setCharacterEncoding("utf-8");43

44 PrintWriter out =response.getWriter();45 out.println("hello filter");46 out.close();47 }48

49 /**

50 *@seeFilter#destroy()51 */

52 public voiddestroy() {53 System.out.println("04 利用destroy方法销毁");54 }55

56 }

DemoFilter

33baf78649f5faee6b1284530fb29d5e.gif

3d41b92b912d44ab83955fa8fb62500b.gif

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

2

3 servletVsFilter

4

5 index.html

6 index.htm

7 index.jsp

8 default.html

9 default.htm

10 default.jsp

11

12

13 servlet

14 cn.xiangxu.DemoServlet

15

16

17 servlet

18 /servlet.do

19

20

21

22 filter

23 cn.xiangxu.DemoFilter

24

25

26 filter

27 /filter.do

28

29

web.xml

项目结构图

818b1a335899d3183eb4a6d945e716c8.png

servlet效果图

0b985ac5a33675e0fedc3ba3fd9134a0.png

filter效果图

ce8f5b94603c100a8270cf539c6bc04d.png

2 为什么对内存中的字符进行编码

字符在服务器内存中是16位的char,网络传输的单位是8为的byte,我们必须对数据进行拆分才能够进行传输,拆分的过程我们叫做编码

3 描述一下GBK和UTF-8的特点

utf-8是国际化的最优编码方案,包括了所有的字符,中文占3字节

gbk是本土化的最优编码方案,包含的字符较少,中文占2字节

4 struts2 和 spring 整合

4.1 为什么要进行整合

使用spring框架管理组件,实现注入,简化代码

4.2 怎么整合

导包(struts2 - spring - plugin)

配置文件(web.xml  spring-struts.xml)

struts.xml配置文件

5 利用 struts + spring 实现 hello world 案例

5.1 导包

struts2核心包、struts和spring的整合插件包

0568c90e40da05be46e51f2cc84b74c6.png

e44e08a2ba0eff47b96d93b2526b5164.gif

12947cb35728f88782e171ea8d46ddb0.gif

1

2 4.0.0

3 cn.xiangxu

4 ssh02

5 0.0.1-SNAPSHOT

6 war

7

8

9 org.apache.struts

10 struts2-core

11 2.3.8

12

13

14 org.apache.struts

15 struts2-spring-plugin

16 2.3.8

17

18

19

maven依赖文件代码

5.2 配置web.xml

e8f0abd5ae368b54a3b7fd84d946af71.gif

ebfa3978952a97e0a0d8d5ced7c26104.gif

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

2

3 ssh02

4

5 index.html

6 index.htm

7 index.jsp

8 default.html

9 default.htm

10 default.jsp

11

12

13

15

16 org.springframework.web.context.ContextLoaderListener

17

18

19

20

21 contextConfigLocation

22 classpath:spring_*.xml

23

24

25

26

27 mvc

28 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

29

30

31 mvc

32 /*33 34

35

36

web.xml配置文件源代码

5.2.1 配置spring监听

9ab35d3acc509865be9e68190273bc3f.png

所需类的位置

752d4bbb14d3ece2bb9a0f32674761b8.png

5.2.2 配置Spring配置文件的位置

84fdaf214f84d3e76efc9631233b639f.png

配置文件所处位置

adce39755181a343f31a4eb75dcb7f21.png

5.2.3 配置主控制器和过滤条件

ad1b9816a8658e0ac6c2d05a2979b8da.png

所需类的位置

250f68560fa0f3dca86f426f1b1380a4.png

5.3 配置spring_context.xml

只需要在里面配置组件扫描即可

6d7c1b68201bb1367ee3b2810dab6a55.gif

4cfaf696d0db683b6c40e05ec3db9a66.gif

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

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee"

5 xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"

6 xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"

7 xmlns:jpa="http://www.springframework.org/schema/data/jpa"

8 xsi:schemaLocation="9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd10 http://www.springframework.org/schema/context/spring-context-3.0.xsd11 http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd12 http://www.springframework.org/schema/jee/spring-jee-3.0.xsd13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd14 http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd15 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd16 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd17 http://www.springframework.org/schema/util/spring-util-3.0.xsd">

18

19

20

21

22

spring配置文件代码

5.4 配置struts.xml

cb5328974f4472766dbf920fe1ce1d97.gif

88d3944e97b08608d656fb06b6484caa.gif

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 /WEB-INF/jsp/msg.jsp11

12

13

14

15

struts配置文件

5.5 编写action类

554159fdb6c36ff98ded898937175dd2.gif

e07da8d4b4f7be82d3be263d9c8fcae3.gif

1 packagecn.xiangxu;2

3 public classTestAction {4 publicString execute() {5 System.out.println("struts整合spring");6 return "success";7 }8 }

action类代码

5.6 编写所需的JSP文件

9d98ccfad60dcd432856ef0b378d76f6.gif

69003d32dcbb50c868325d6f26e5e78c.gif

1

2 pageEncoding="utf-8"%>

3

4

5

6

7

test

8

9

10

恭喜你,struts整合spring成功!

11

12

jsp文件代码

项目结构图

63257c6a7d4cd374c70aa1ecd09ebd48.png

6 struts整合spring后就可以使用springIOC啦

例如:持久层的action类中通过依赖注入服务层的类,从而就可以使用服务层的相关属性和方法啦

6.1 案例改进所需的业务层类

fb4afa6796099b42c06c1fd23918ac84.gif

688f927e9882965e7575e19163c2cdde.gif

1 packagecn.xiangxu.service;2

3 importorg.springframework.stereotype.Service;4

5 @Service6 public classTestService {7 public voidtest() {8 System.out.println("我是业务层的数据哟。");9 }10 }

增加的service类代码

6.2 案例改进一

在action类上添加@Controller注解,这样spring容器就会自动给其增加一个bean

在action类上添加@Scope("prototype")注解,这样就能让每一个请求都有一个action处理类,如果不写就是所有的请求公用一个action处理类(即:默认时使用单例模式)

在action类中依赖注入业务层,从而实现控制反转

fd49f6a0a5214688a227fd9c7f5bbafb.gif

63bc97a6c72ef37b9b5f158eb714e275.gif

1 packagecn.xiangxu.action;2

3 importjavax.annotation.Resource;4

5 importorg.springframework.context.annotation.Scope;6 importorg.springframework.stereotype.Controller;7

8 importcn.xiangxu.service.TestService;9

10 @Controller11 @Scope("prototype")12 public classTestAction {13

14 @Resource(name="testService")15 privateTestService testService;16

17 publicString execute() {18 System.out.println("struts整合spring");19 testService.test();20 return "success";21 }22 }

action类改进

6.3 案例改进二

struts中的action标签中的class属性值可以用相应bean的id代替

aca446a057e1e88f8f7226c49747750a.gif

7484dc5fee999d947f498152156b4bc9.gif

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 /WEB-INF/jsp/msg.jsp11

12

13

14

15

sturts配置文件代码改进

7 注解回顾

@Component@Controller@Service@Repository

@Resource@Scope

@Component通用的标记(不推荐使用,菜鸟可以使用,【AOP编程时可以使用】)

格式1:@Controller("指定id")

格式2:@Controller  这种方式时使用默认的id,即:类名小写后的值

@Controller作用于控制层

@Service作用于业务层

@Repository作用于持久层

@Scope避免线程的安全问题,指定值为“prototype",这样每个请求都会对应一个action处理类;如果不写的话就是单例模式(即:所有请求对应一个action处理类)

格式:@Scope("prototype")

@Resource依赖注入时的声明

格式:@Resource(name="依赖注入的id")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值