静安免费网站制作专业网站设计专业服务
阿华代码,不是逆风,就是我疯
你们的点赞收藏是我前进最大的动力!!
希望本文内容能够帮助到你!!
目录
一:什么是Spring Web MVC
1:Servlet
2:总结
二:MVC
1:定义
2:解释
3:Spring MVC和MVC的关系
三:Spring MVC
1:SpringBoot 和 SpringMVC之间的关系
四:实践
1:建立连接
2:@RequestMapping注解介绍
(1)既可以修饰类也可以修饰方法
(2)既支持get也支持post请求
3:RequestController注解
4:传递参数
(1)参数使用包装类型
(2)传参顺序不影响结果
5:传递对象
6:Requestparam
(1)后端参数映射
(2)更改为非必要传参
7:传递数组
8:传递集合
9:传递JSON数据
(1)传递失败
(2)RequestBody
10:JSON字符串和Java对象的转换
(1)第三方工具
(2)Person类
(3)ObjectMapper类
一:什么是Spring Web MVC
 Spring Web MVC 是基于 Servlet API 构建的原始 Web 框架,从⼀开始就包含在 Spring 框架中。它的正式名称“Spring Web MVC”来⾃其源模块的名称(Spring-webmvc),但它通常被称为"Spring  
 
 MVC". 
 
 
1:Servlet
 Servlet 是⼀种实现动态⻚⾯的技术. 准确来讲Servlet是⼀套 Java Web 开发的规范Servlet 规范的,产品包括 Tomcat、Weblogic、Jetty、Jboss、WebSphere 等 
 
 
2:总结
 从上述定义我们可以得出⼀个信息: Spring Web MVC 是⼀个 Web 框架.  
  下⾯咱们简称之为: Spring MVC 
  二:MVC
1:定义
 MVC 是 Model View Controller 的缩写,它是软件⼯程中的⼀种软件架构设计模式,它把软件系统分为模型、视图和控制器三个基本部分 
 
 2:解释
 View(视图) 指在应⽤程序中专⻔⽤来与浏览器进⾏交互,展⽰数据的资源.  
   Model(模型) 是应⽤程序的主体部分,⽤来处理程序中数据逻辑的部分.  
   Controller(控制器)可以理解为⼀个分发器,⽤来决定对于视图发来的请求,需要⽤哪⼀个模型  
  来处理,以及处理完后需要跳回到哪⼀个视图。即⽤来连接视图和模型 
  3:Spring MVC和MVC的关系
 三:Spring MVC
 MVC 是⼀种架构设计模式, 也⼀种思想, ⽽ Spring MVC 是对 MVC 思想的具体实现. 除此之外, Spring MVC还是⼀个Web框架 
  1:SpringBoot 和 SpringMVC之间的关系
 Spring Boot 只是实现Spring MVC的其中⼀种⽅式⽽已.  
  Spring Boot 可以添加很多依赖, 借助这些依赖实现不同的功能. Spring Boot 通过添加Spring Web  
  MVC框架, 来实现web功能 
  总结来说,Spring MVC 是⼀个实现了 MVC 模式的 Web 框架 
 四:实践
1:建立连接
 在 Spring MVC 中使⽤  @RequestMapping  来实现 URL 路由映射 ,也就是浏览器连接程序的作⽤ 
 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@RestController
public class UserController {
// 路由器规则注册@RequestMapping("/sayHi")public String sayHi(){return "hello,Spring MVC";}
}  接下来访问:  http://127.0.0.1:8080/sayHi , 就可以看到程序返回的数据了 
 
 2:@RequestMapping注解介绍
 @RequestMapping  是 Spring Web MVC 应⽤程序中最常被⽤到的注解之⼀,它是⽤来注册接⼝的  
  路由映射的.  
  表⽰服务收到请求时, 路径为 /sayHi 的请求就会调⽤ sayHi 这个⽅法的代码. 
  路由映射: 当⽤⼾访问⼀个 URL 时, 将⽤⼾的请求对应到程序中某个类的某个⽅法的过程就叫路由映射 
 (1)既可以修饰类也可以修饰方法
 (2)既支持get也支持post请求
 
 
 
 @RequestMapping("/request")
@RestController
public class RequestController {@RequestMapping("/hello")public String say(){return "hello byte";}@RequestMapping("/r0")public String say1(){return "hello byte";}@RequestMapping("/r1")public String r1(String name){return name;}@RequestMapping("/r2")public int r2(int age){return age;}@RequestMapping("/r3")public String r3(Integer age){return "接收到的参数为:"+age;}
}
  <!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form action = "/request/hello" method="get"><input type="submit" value="发送请求">
</form></body>
</html>  (3)括号中的地址前可加/也可不加,最好加上 
  3:RequestController注解
⼀个项⽬中, 会有很多类, 每个类可能有很多的⽅法
 Spring会对所有的类进⾏扫描, 如果类加了注解@RestController, Spring才会去看这个类⾥⾯的⽅法有没有加 @RequestMapping  这个注解, 当然他的作⽤不⽌这⼀点, 咱们先⽤, 后⾯再详细讲 
  4:传递参数
(1)参数使用包装类型
 对于包装类型, 如果不传对应参数,Spring 接收到的数据则为null  
  所以企业开发中,对于参数可能为空的数据,建议使⽤包装类型 
  (2)传参顺序不影响结果
 当有多个参数时,前后端进⾏参数匹配时,是以参数的名称进⾏匹配的,因此参数的位置是不影响后端获取参数的结果 
  5:传递对象
package com.example.springbootmvc;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** Created with IntelliJ IDEA.* Description:* User: Hua YY* Date: 2024-11-04* Time: 21:25*/
@RequestMapping("/request")
@RestController
public class RequestController {@RequestMapping("/hello")public String say(){return "hello byte";}@RequestMapping("/r0")public String say1(){return "hello byte";}@RequestMapping("/r1")public String r1(String name){return name;}@RequestMapping("/r2")public int r2(int age){return age;}@RequestMapping("/r3")public String r3(Integer age){return "接收到的参数为:"+age;}@RequestMapping("/m1")public Object method1(Person p){return p.toString();}
}
 @RequestMapping("/m1")public Object method1(Person p){return p.toString();} 
可以看到, 后端程序正确拿到了Person对象⾥各个属性的值
Spring 会根据参数名称⾃动绑定到对象的各个属性上, 如果某个属性未传递, 则赋值为null(基本类型则赋值为默认初识值, ⽐如int类型的属性, 会被赋值为0)
6:Requestparam
(1)后端参数映射
 某些特殊的情况下,前端传递的参数 key 和我们后端接收的 key 可以不⼀致,⽐如前端传递了⼀个  
  time 给后端,⽽后端是使⽤ createtime 字段来接收的,这样就会出现参数接收不到的情况,如果出现  
  这种情况,我们就可以使⽤  @RequestParam  来重命名前后端的参数值 
 @RequestMapping("r4")public Object r4(@RequestParam("time") String createtime){return "接受到参数createtime:" + createtime;} 
 可以得出结论:  
  1.  使⽤  @RequestParam  进⾏参数重命名时, 请求参数只能和  @RequestParam  声明的名称⼀  
  致, 才能进⾏参数绑定和赋值.  
  2.  使⽤  @RequestParam  进⾏参数重命名时, 参数就变成了必传参数 
 (2)更改为非必要传参
源码

 可以看到  required  的默认值为true, 表⽰含义就是: 该注解修饰的参数默认为必传  
  既然如此, 我们可以通过设置  @RequestParam  中的  required=false  来避免不传递时报错,  
  具体实现如下 
 7:传递数组
@RequestMapping("/r5")public String r5(String[] arrayParam){return Arrays.toString(arrayParam);} 
8:传递集合
@RequestMapping("/r6")public String r6(@RequestParam List<String> listParam){return "size:"+listParam.size()+"   "+"listParam:"+listParam;} 
9:传递JSON数据
 JSON:JavaScript Object Notation 【JavaScript 对象表⽰法】 
  JSON就是⼀种数据格式, 有⾃⼰的格式和语法, 使⽤⽂本表⽰⼀个对象或数组的信息, 因此  
  JSON本质是字符串. 主要负责在不同的语⾔中数据传递和交换.  
 JSON的语法:1. 数据在 键值对 (Key/Value) 中2. 数据由逗号 , 分隔3. 对象⽤ {} 表⽰4. 数组⽤ [] 表⽰5. 值可以为对象, 也可以为数组, 数组中可以包含多个对象
(1)传递失败
@RequestMapping("/r7")public Object r7(Person p){return p.toString();} 可以看到我们用postman发送json请求,服务器并没有收到我们想要的值为什么呢?
 
 因为一个json对象是不能分割的所以左边的这种发送方式不可行 
 (2)RequestBody
加上RequesstBody注解后,才能读取HTTP中body中的json数据
 @RequestMapping("/r8")public Object r8(@RequestBody Person p){return p.toString();} 
10:JSON字符串和Java对象的转换
(1)第三方工具

(2)Person类
在json字符串转换为Java对象的时候,要先进行类加载,我们尽量把无参的构造方法也写入类中,避免后续,传参时,找不到对应的构造方法
package com.example.springbootmvc;import com.fasterxml.jackson.databind.ObjectMapper;/*** Created with IntelliJ IDEA.* Description:* User: Hua YY* Date: 2024-11-06* Time: 17:59*/
public class Person {private String name;private int age;private int password;public Person(){}public Person(String name, int age, int password) {this.name = name;this.age = age;this.password = password;}public String getName() {return name;}public int getAge() {return age;}public int getPassword() {return password;}public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public void setPassword(int password) {this.password = password;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", password=" + password +'}';}} (3)ObjectMapper类
.readValue 字符串转对象
.writeValueAsString() 对象转字符串
package com.example.springbootmvc;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.validation.ObjectError;/*** Created with IntelliJ IDEA.* Description:* User: Hua YY* Date: 2024-11-07* Time: 12:23*/
public class JSONTest {public static void main(String[] args) throws JsonProcessingException {//调用ObjectMapper中的两个方法才能实现Json字符串和java对象的转换ObjectMapper objectMapper = new ObjectMapper();//格式化JSON字符串String jsonStr = "{\"name\":\"zhangsan\",\"age\":15,\"password\":666}";//JSON字符串转化为java对象,先加载Person类在,解析字符串Person p = objectMapper.readValue(jsonStr,Person.class);System.out.println(p.toString());//java对象转化为Json字符串String s = objectMapper.writeValueAsString(p);System.out.println(s);}}
 

