SpringMVC入门

1.1 ApplicationContext应用上下文获取方式

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从
容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置
文件加载多次,应用上下文对象创建多次。
在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加
载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域
中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

1.2 Spring提供获取应用上下文的工具

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监
听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工
WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:
① 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
② 使用WebApplicationContextUtils获得应用上下文对象ApplicationContext

1.3 导入Spring集成web的坐标

1 <dependency>
2     <groupId>org.springframework</groupId>
3     <artifactId>spring-web</artifactId>
4     <version>5.0.5.RELEASE</version>
5 </dependency>

1.4 配置ContextLoaderListener监听器

 1 <!-- 全局参数 -->
 2 <context-param>
 3     <param-name>contextConfigLocation</param-name>
 4     <param-value>classpath:applicationContext.xml</param-value>
 5 </context-param>
 6 <!--Spring 的监听器 -->
 7 <listener>
 8     <listener-class>
 9     org.springframework.web.context.ContextLoaderListener
10     </listener-class>
11 </listener

1.5 通过工具获得应用上下文对象

ApplicationContext applicationContext =  WebApplicationContextUtils.getWebApplicationContext(servletContext);
Object obj = applicationContext.getBean("id");

1.5 知识要点

  Spring集成web环境步骤
    ① 配置ContextLoaderListener监听器
    ② 使用WebApplicationContextUtils获得应用上下

2.1 SpringMVC快速入门

需求:客户端发起请求,服务器端接收请求,执行逻辑并进行视图跳转。
开发步骤:
  ① 导入SpringMVC相关坐标
  ② 配置SpringMVC核心控制器DispathcerServlet
  ③ 创建Controller类和视图页面
  ④ 使用注解配置Controller类中业务方法的映射地址
  ⑤ 配置SpringMVC核心文件 spring-mvc.xml
  ⑥ 客户端发起请求测试

2.2 SpringMVC快速入门

  ① 导入Spring和SpringMVC的坐标

 1 <!--Spring 坐标 -->
 2 <dependency>
 3     <groupId>org.springframework</groupId>
 4     <artifactId>spring-context</artifactId>
 5     <version>5.0.5.RELEASE</version>
 6 </dependency>
 7 <!--SpringMVC 坐标 -->
 8 <dependency>
 9     <groupId>org.springframework</groupId>
10     <artifactId>spring-webmvc</artifactId>
11     <version>5.0.5.RELEASE</version>
12 </dependency>

  ②  导入Servlet和Jsp的坐标

 1 <!--Servlet 坐标 -->
 2 <dependency>
 3     <groupId>javax.servlet</groupId>
 4     <artifactId>servlet-api</artifactId>
 5     <version>2.5</version>
 6 </dependency>
 7 <!--Jsp 坐标 -->
 8 <dependency>
 9     <groupId>javax.servlet.jsp</groupId>
10     <artifactId>jsp-api</artifactId>
11     <version>2.0</version>
12 </dependency>

  ③ 在web.xml配置SpringMVC的核心控制器

 1 <servlet>
 2     <servlet-name>DispatcherServlet</servlet-name>
 3     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4     <init-param>
 5         <param-name>contextConfigLocation</param-name>
 6         <param-value>classpath:spring-mvc.xml</param-value>
 7     </init-param>
 8     <load-on-startup>1</load-on-startup>
 9 </servlet>
10     <servlet-mapping>
11     <servlet-name>DispatcherServlet</servlet-name>
12     <url-pattern>/</url-pattern>
13 </servlet-mapping>            

  ④ 创建Controller和业务方法

  ⑤创建视图页面

  ⑥配置注解(可以在第四步完成)

  ⑦创建spring-mvc.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2 xmlns:mvc="http://www.springframework.org/schema/mvc"
 3 xmlns:context="http://www.springframework.org/schema/context"
 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5 xsi:schemaLocation="http://www.springframework.org/schema/beans
 6 http://www.springframework.org/schema/beans/spring-beans.xsd
 7 http://www.springframework.org/schema/mvc
 8 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context.xsd">
11     <!-- 配置注解扫描 -->
12     <context:component-scan base-package="com.itheima"/>
13 </beans>

2.3 SpringMVC流程图示

 

 

 3.1 SpringMVC的执行流程

 

 

 

① 用户发送请求至前端控制器DispatcherServlet。
② DispatcherServlet收到请求调用处理器映射器HandlerMapping。
③ 处理器映射器找到具体的处理器Handler(可以根据xml配置、注解进行查找),生成处理器对象及处理器拦截器(如果
有则生成)一并返回给DispatcherServlet。
④ DispatcherServlet调用HandlerAdapter处理器适配器。
⑤ HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)。
⑥ Controller执行完成返回ModelAndView。
⑦ HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。
⑧ DispatcherServlet将ModelAndView传给ViewReslover视图解析器。
⑨ ViewReslover解析后返回具体View。
⑩ DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。DispatcherServlet响应用户。

3.2 SpringMVC组件解析

1. 前端控制器:DispatcherServlet
  用户请求到达前端控制器,它就相当于 MVC 模式中的 C,DispatcherServlet 是整个流程控制的中心,由
  它调用其它组件处理用户的请求,DispatcherServlet 的存在降低了组件之间的耦合性。
2. 处理器映射器:HandlerMapping
  HandlerMapping 负责根据用户请求找到 Handler 即处理器,SpringMVC 提供了不同的映射器实现不同的
  映射方式,例如:配置文件方式,实现接口方式,注解方式等。
3. 处理器适配器:HandlerAdapter
  通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理
  器进行执行。

4. 处理器:Handler
  它就是我们开发中要编写的具体业务控制器。由 DispatcherServlet 把用户请求转发到 Handler。由
  Handler 对具体的用户请求进行处理。
5. 视图解析器:View Resolver
  View Resolver 负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名,即
  具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
6. 视图:View
  SpringMVC 框架提供了很多的 View 视图类型的支持,包括:jstlView、freemarkerView、pdfView等。最
  常用的视图就是 jsp。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程
  序员根据业务需求开发具体的页面

3.3 SpringMVC注解解析

  @RequestMapping
  作用:用于建立请求 URL 和处理请求方法之间的对应关系
  位置

    •  类上,请求URL 的第一级访问目录。此处不写的话,就相当于应用的根目录
    •  方法上,请求 URL 的第二级访问目录,与类上的使用@ReqquestMapping标注的一级目录一起组成访问虚拟路径
    • 属性:
    •  value:用于指定请求的URL。它和path属性的作用是一样的
    •  method:用于指定请求的方式
    •  params:用于指定限制请求参数的条件。它支持简单的表达式。要求请求参数的key和value必须和配置的一模一样
    • 例如:
    •  params = {"accountName"},表示请求参数必须有accountName
    •  params = {"moeny!100"},表示请求参数中money不能是100

1. mvc命名空间引入
命名空间:xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
约束地址:http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
2. 组件扫描
SpringMVC基于Spring容器,所以在进行SpringMVC操作时,需要将Controller存储到Spring容器中,如果使
用@Controller注解标注的话,就需要使用<context:component-scan base-
package=“com.itheima.controller"/>进行组件扫描。

3.4 SpringMVC的XML配置解析

  1. 视图解析器
    SpringMVC有默认组件配置,默认组件都是DispatcherServlet.properties配置文件中配置的,该配置文件地址
    org/springframework/web/servlet/DispatcherServlet.properties,该文件中配置了默认的视图解析器,如下:

      org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

  

      翻看该解析器源码,可以看到该解析器的默认设置,如下:
        REDIRECT_URL_PREFIX = "redirect:" -- 重定向前缀
        FORWARD_URL_PREFIX = "forward:" -- 转发前缀(默认值)
        prefix = ""; -- 视图名称前缀
        suffix = ""; -- 视图名称

    可以通过属性注入的方式修改视图的的前后缀

1 <!-- 配置内部资源视图解析器 -->
2 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
3     <property name="prefix" value="/WEB-INF/views/"></property>
4     <property name="suffix" value=".jsp"></property>
5 </bean>

 

posted @ 2021-09-22 22:55  E_lian  阅读(26)  评论(0编辑  收藏  举报