1. Spring MVC 概述
1.1 什么是Spring MVC
-
Spring MVC 是 Spring 提供的一个基于 MVC 设计模式的轻量级 Web 开发框架,本质上相当于 Servlet。
-
由于 Spring MVC 本身就是 Spring 框架的一部分,所有与 Spring 框架是无缝集成。是当今业界最主流的 Web 开发框架。
1.2 Spring MVC的执行流程
-
Spring MVC 是 Spring 提供的一个基于 MVC 设计模式的轻量级 Web 开发框架,本质上相当于 Servlet。
-
由于 Spring MVC 本身就是 Spring 框架的一部分,所有与 Spring 框架是无缝集成。是当今业界最主流的 Web 开发框架。
1.2 Spring MVC的执行流程
-
用户点击某个请求路径,发起一个 HTTP request 请求,该请求会被提交到 DispatcherServlet(前端控制器);
-
由 DispatcherServlet 请求一个或多个 HandlerMapping(处理器映射器),并返回一个执行链(HandlerExecutionChain)。
-
DispatcherServlet 将执行链返回的 Handler 信息发送给 HandlerAdapter(处理器适配器);
-
HandlerAdapter 根据 Handler 信息找到并执行相应的 Handler(常称为 Controller);
-
Handler 执行完毕后会返回给 HandlerAdapter 一个 ModelAndView 对象(Spring MVC的底层对象,包括 Model 数据模型和 View 视图信息);
-
HandlerAdapter 接收到 ModelAndView 对象后,将其返回给 DispatcherServlet ;
-
DispatcherServlet 接收到 ModelAndView 对象后,会请求 ViewResolver(视图解析器)对视图进行解析;
-
ViewResolver 根据 View 信息匹配到相应的视图结果,并返回给 DispatcherServlet;
-
DispatcherServlet 接收到具体的 View 视图后,进行视图渲染,将 Model 中的模型数据填充到 View 视图中的 request 域,生成最终的 View(视图);
-
视图负责将结果显示到浏览器(客户端)。
2. 环境搭建
-
项目名:maven_mvc_anno
-
拷贝坐标
war
UTF-8
1.8
1.8
false
org.springframework
spring-context
5.2.4.RELEASE
org.springframework
spring-aspects
5.2.4.RELEASE
org.springframework
spring-test
5.2.4.RELEASE
org.springframework
spring-webmvc
5.2.4.RELEASE
org.springframework
spring-jdbc
5.2.4.RELEASE
mysql
mysql-connector-java
8.0.24
com.alibaba
druid
1.2.4
org.mybatis
mybatis
3.5.7
tk.mybatis
mapper
3.5.2
com.github.pagehelper
pagehelper
3.7.5
org.mybatis
mybatis-spring
2.0.5
junit
junit
4.12
com.fasterxml.jackson.core
jackson-databind
2.10.2
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
commons-fileupload
commons-fileupload
1.4
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
javax.servlet
jstl
1.2
org.slf4j
slf4j-log4j12
1.7.10
项目名:maven_mvc_anno
拷贝坐标
配置WEB支持
2.1 步骤
-
步骤:
-
编写controller:处理入口
-
编写jsp页面:显示内容
-
拷贝核心配置类:(取代springmvc.xml)
-
拷贝核心配置类:(取代web.xml)
-
编写首页,部署项目,启动访问
2.1.1 实现
步骤:
-
编写controller:处理入口
-
编写jsp页面:显示内容
-
拷贝核心配置类:(取代springmvc.xml)
-
拷贝核心配置类:(取代web.xml)
-
编写首页,部署项目,启动访问
1、拷贝核心配置类:MvcConfiguration用于扫描controller
package liu.mvc.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "liu.mvc.controller")
public class MvcConfiguration {
}
2.拷贝核心配置类:WebInitializer1
package liu.mvc.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebInitializer1 implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//1 配置spring工厂
AnnotationConfigWebApplicationContext application = new AnnotationConfigWebApplicationContext();
// 注册所有的配置类
application.register(MvcConfiguration.class);
//2 post中文乱码
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding", new CharacterEncodingFilter("UTF-8"));
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
//3 核心控制器
ServletRegistration.Dynamic mvcServlet = servletContext.addServlet("springmvc", new DispatcherServlet(application));
mvcServlet.addMapping("*.action");
mvcServlet.setLoadonStartup(2); //tomcat启动时,执行servlet的初始化方法
}
}
3.编写jsp页面:显示内容
位置:/web/pages/list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
展示列表页面
4.编写首页,部署项目,并启动访问
位置:/web/index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
首页
3. Spring MVC 基础
3.1 请求路径:@RequestMapping
3.1.1 窄化请求路径
-
@RequestMapping放在类名上边,设置请求前缀
-
@RequestMapping放在方法名上边,设置方法对应请求路径。
-
完整请求:前缀 + 请求路径
-
需求:使用 /demo01/index.action访问首页
package liu.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/demo1")
public class Demo1IndexController {
@RequestMapping("/index")
public String index(){
return "/index.jsp";
}
}
3.1.1 窄化请求路径
-
@RequestMapping放在类名上边,设置请求前缀
-
@RequestMapping放在方法名上边,设置方法对应请求路径。
-
完整请求:前缀 + 请求路径
-
需求:使用 /demo01/index.action访问首页
package liu.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/demo1")
public class Demo1IndexController {
@RequestMapping("/index")
public String index(){
return "/index.jsp";
}
}
@RequestMapping放在类名上边,设置请求前缀
@RequestMapping放在方法名上边,设置方法对应请求路径。
完整请求:前缀 + 请求路径
需求:使用 /demo01/index.action访问首页
访问路径:
窄化
3.1.2 多路径映射
-
@RequestMapping 允许配置多个访问路径
-
需求:使用 /demo01/index2.action访问首页
@RequestMapping({"/index","/index2"})
public String index(){
return "/index.jsp";
}
@RequestMapping 允许配置多个访问路径
需求:使用 /demo01/index2.action访问首页
访问路径:
多路径
3.1.3 请求方法限定
@RequestMapping 默认支持各种请求方式的访问,可以通过 method属性限制不同的请求方式。
| 注解 | 描述 |
|---|---|
| @RequestMapping(method = RequestMethod.GET) | 只有get请求方式可访问 |
| @RequestMapping(method = RequestMethod.POST) | 只有post请求方式可访问 |
| @RequestMapping(method={RequestMethod.GET,RequestMethod.POST}) | get和post都可访问 |
使用/demo01/post.action,以不同的请求方式访问首页
-
get请求抛异常
-
post允许访问
@RequestMapping(value = "/post",method = RequestMethod.POST)
访问路径:
//post请求方式访问
//get请求方式访问,会报异常 get请求访问
3.2 参数绑定
3.2.1 简单数据类型
-
在控制器的方法中,只要有对应的参数名,spring mvc就可以自动完成参数封装
-
在控制器的方法中,只要有对应的参数名,spring mvc就可以自动完成参数封装
1)基本操作
-
访问`/demo02/findById.action?id=1,设置参数id
package liu.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/demo2")
public class Demo2ParamConroller {
@RequestMapping({"/findById"})
public String findById(String id){
System.out.println("id:"+id);
return "/index.jsp";
}
}
访问路径
简单数据
ps :参数名和变量名必须保持一致。
2)支持的数据类型
| 数据类型 | 取值 |
|---|---|
| 整型 | Integer、int |
| 字符串 | String |
| 单精度 | Float、float |
| 双精度 | Double、double |
| 布尔型 | Boolean、boolean |
3)自定义变量名:@RequestParam
-
如果请求参数名和方法参数名不一致时,需要使用@RequestParam标记对应关系。
-
需求:使用变量name接收参数id的值
@RequestMapping("/findById2")
public String findById2(@RequestParam("id") String uid){
System.out.println("uid:"+uid);
return "/index.jsp";
}
访问路径:
简单数据uid
效果展示:



