SpringMVC
Spring与Web环境集成
springboot 添加webapp目录-百度经验 (baidu.com)
ApplicationContext应用上下文获取方式
应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件)方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多次,应用上下文对象创建多次,影响性能。
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
创建监听器
在Web项目中使用ServletContextListener监听Web应用的启动,当Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
Spring提供获取应用上下文的工具
上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:
①在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
导入Spring集成web的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--全局参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
②使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
ApplicationContext applicationContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
Object obj = applicationContext.getBean("id");
SpringMVC简介
概述
MVC(Model View Controller) 模型,视图,控制器
SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 中。
SpringMVC 已经成为目前最主流的MVC框架之一,并且随着Spring3.0 的发布,全面超越 Struts2,成为最优秀的 MVC 框架。它通过一套注解,让一个简单的 Java 类成为处理请求的控制器,而无须实现任何接口。同时它还支持 RESTful 编程风格的请求。
快速入门
需求:客户端发起请求,服务器端接收请求,执行逻辑并进行视图跳转。
开发步骤
![image-20210815170227082](https://picturebedzhanghui.oss-cn-hangzhou.aliyuncs.com/img/image-20210815170227082.png)
①导入SpringMVC相关坐标
②配置SpringMVC核心控制器DispathcerServlet
③创建Controller类和视图页面
④使用注解配置Controller类中业务方法的映射地址
⑤配置SpringMVC核心文件 spring-mvc.xml
⑥客户端发起请求测试
代码实现
①导入Spring和SpringMVC的坐标、导入Servlet和Jsp的坐标
<!--Spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--SpringMVC坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--Springweb坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--Servlet坐标-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--Jsp坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
②在web.xml配置SpringMVC的核心控制器 DispatcherServlet
<!-- SpringMVC的前端控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
为DispatcherServlet提供初始化参数的
设置springmvc配置文件的路径
name是固定的,必须是contextConfigLocation
value指的是SpringMVC配置文件的位置
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--
指定项目启动就初始化DispatcherServlet
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!--
/ 表示当前servlet映射除jsp之外的所有请求(包含静态资源)
*.do 表示.do结尾的请求路径才能被SpringMVC处理(老项目会出现)
/* 表示当前servlet映射所有请求(包含静态资源,jsp),不应该使用其配置DispatcherServlet
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
③创建Controller和业务方法和配置映射地址和注解
@Controller
public class UserController {
/**
* //请求映射
* @return
*/
@RequestMapping("/quick")
public String save(){
System.out.println("Controller save running");
return "success.jsp";
}
}
③创建视图页面success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>
④创建spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开始扫描Controller包
SpringMVC基于Spring容器,所以在进行SpringMVC操作时,需要将Controller存储到Spring容器中,如果使用@Controller注解标注的话 -->
<context:component-scan base-package="controller"></context:component-scan>
</beans>
⑤访问测试地址
http://localhost:8081/SpringMVC1_war_exploded/quick
遇到tomact无法启动的原因
[(12条消息) Tomcat启动报错:一个或多个筛选器启动失败。由于之前的错误,Context]启动失败_xzl的博客-CSDN博客_一个或多个筛选器启动失败
SpringMVC流程视图
![image-20210806151232413](https://picturebedzhanghui.oss-cn-hangzhou.aliyuncs.com/img/image-20210806151232413.png)
-
服务器启动
- 加载web.xml中DispatcherServlet
- 读取spring-mvc.xml中的配置,加载所有com.itheima包中所有标记为bean的类
- 读取bean中方法上方标注@RequestMapping的内容
-
处理请求
- DispatcherServlet配置拦截所有请求 /
- 使用请求路径与所有加载的@RequestMapping的内容进行比对执行对应的方法
- 根据方法的返回值在webapp目录中查找对应的页面并展示
SpringMVC组件解析
![image-20210806152037059](https://picturebedzhanghui.oss-cn-hangzhou.aliyuncs.com/img/image-20210806152037059.png)
-
前端控制器:DispatcherServlet
用户请求到达前端控制器,它就相当于 MVC 模式中的 C,DispatcherServlet 是整个流程控制的中心,由它调用其它组件处理用户的请求,DispatcherServlet 的存在降低了组件之间的耦合性。
-
处理器映射器:HandlerMapping
HandlerMapping 负责根据用户请求找到 Handler 即处理器,SpringMVC 提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。
-
处理器适配器:HandlerAdapter
通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。
-
处理器:Handler
它就是我们开发中要编写的具体业务控制器。由 DispatcherServlet 把用户请求转发到 Handler。由Handler 对具体的用户请求进行处理。
-
视图解析器:View Resolver
View Resolver 负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名,即具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
6.视图:View
视图,最终产出结果, 常用视图如jsp、 html
SpringMVC注解解析
@RequestMapping
设置请求映射规则
作用:用于建立请求 URL 和处理请求方法之间的对应关系
位置:方法上或者是类上
- 类上,请求URL 的第一级访问目录。此处不写的话,就相当于应用的根目录
- 方法上,请求 URL 的第二级访问目录,与类上的使用@ReqquestMapping标注的一级目录一起组成访问虚拟路径
属性:
-
value:只写单个属性时可以省略
@Controller @RequestMapping(value = "user") //@RequestMapping("user") public class UserController { /** * 请求映射 * @return */ @RequestMapping(value = "quick") @RequestMapping("quick") public String save(){ System.out.println("Controller save running"); return "/success.jsp"; } }
-
method:可以用来指定可处理的请求方式。
注意:我们可以也可以运用如下注解来进行替换
@PostMapping 等价于 @RequestMapping(method = RequestMethod.POST)
@GetMapping 等价于 @RequestMapping(method = RequestMethod.GET)
@PutMapping 等价于 @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping 等价于 @RequestMapping(method = RequestMethod.DELETE)
-
params: 指定请求参数,对请求参数进行一些限制
例如:
params = {"username"}
,表示请求参数必须有usernameparams = {"moeny!100"}
,表示请求参数中money不能是100params = {"!username"}
, 表示不能有username这个参数
SpringMVC的数据响应
页面跳转
- 直接返回字符串
在进行请求地址映射后,返回一个静态页面的路径如下
@Controller
@RequestMapping(value = "user")
public class UserController {
/**
* 请求映射
* @return
*/
// @RequestMapping(value = "quick",method = RequestMethod.GET)
@GetMapping(value = "quick",params = "name")
public String save(){
System.out.println("Controller save running");
return "/success.jsp";
}
}
- 默认的跳转其实是转发的方式跳转的。
- 可以在路径前加上forwar:,这样SpringMVC也会帮我们进行请求转发。
return "forward:/success.jsp";
- 如果想实现重定向跳转则可以在跳转路径前加上
redirect:
进行标识。 - 这样SpringMVC就会帮我们进行重定向跳转
return "redirect:/success.jsp";
地址发生了改变,但是跳转的还是原来的界面。
下面进行一些优化,把跳转地址的前缀和后缀省略(放入到配置文件中);
-
配置spring-mvc.xml如下
<!-- 配置内部资源视图解释器:映射地址跳转对应静态页面 return "/success.jsp";--> <bean id="viewResolver" class="org.sp