引言
所谓MVC是一种软件的设计模型,他把软件系统分为三部分,View(视图),Controller(控制器),Model(模型),他们之间的关系是

spring mvc全称为spring web mvc,而spring在实现mvc的时候,也根据自己的特点来进行一些改变,这是更贴合spring的mvc的实现

spirng mvc中controller实现了选择模型和视图的功能,而在spring mvc中已经支持了视图的处理,具体是通过委派给集成的第三方库来实现数据的渲染,比如返回一个类,控制器只需要直接return这个类就好,spring mvc会自动的选择对应的json转换视图模型来对这个类进行转换并返回响应
并且提供了比servlet更加简单的web开发,比如
// 原生 Servlet:手动解析参数
String userId = request.getParameter("userId");
// Spring MVC:自动绑定参数
@GetMapping("/user")
public String getUser(@RequestParam String userId) {
// 直接使用 userId
}
且spring mvc使用注解开发,减少了一半多的代码量,而且能够根据请求头来自动的进行格式转换
也就是可以不用特别放回json/xml格式的数据
spring mvc中的常见注解
使用@RequestMapping来进行url路由映射

p这个方法的url就是http://127.0.0.1:8080/b1/p,因为是默认在本地的8080端口运行的项目
但是单单这样写还不能接受http请求,还需要加上Controller注解表示这个类是个控制器,因为发起的请求是发送给控制器的,那么就还需要加上Controller注解

@RestController表示的是这个类返回的是数据,而@Controller表示返回的是html页面
对于前端参数的接收
一个参数

这是前端发起的请求一定要和后端的名字相同
数组接收

只能接收get请求


只能接收post请求


将接收到的数据赋给另一变量

集合的接收,使用@RequestParam来绑定参数

json的接收

获取url中的参数 使用@PathVariable注解

文件的接收 @RequestPart

服务端Cookie的获取

HttpServletRequest类是servlet的类,记录的发来的请求的所有信息,在这之上的获取值得方法其实都是封装了这个类的方法,可以说你想从请求中获取什么都可以在这里获取到
还有个对应的类HttpServletResponse,这个返回的响应的servlet类,你想返回什么也都可以这个类中,你可以设置返回的状态码
设置cookie

直接获得某个key对应的Cookie值

返回html页面 要是使用Controller这个标签,且返回页面的路径是在项目的main/resource/static下的文件
end~~ 下一篇使用这些实现一些案例