Spring Web MVC(1)

目录

1.什么是MVC

2.什么是Spring Web MVC

3.SpringMVC和MVC的关系

 4.使用SpringMVC

4.1创建SpringMVC

4.2建立链接

4.3注解介绍

4.3.1@RequestMapping

4.3.2@RestController

4.4获取参数

4.4.1获取单个参数

4.4.2传递参数

4.4.3传递对象

4.4.4后端参数映射

4.4.5传递数组

4.4.6传递集合


1.什么是MVC

MVC全称:Model View Controller

View:程序中专门用来与浏览器进行交互,展示数据资源.

Model:程序的主体部分,用来处理程序中数据逻辑的部分.

Controller:相当于一个分发器,用来连接视图和模型.

举个例子:我们去公司面试,HR会安排面试所需要用的会议室,根据候选人去通知不同的部门来安排面试,面试结束,由HR来通知结果.

                   HR就是View,接待与通知.

                   不同的部门就是Controller,HR根据候选人来选择对应的部门进行面试.

                   面试官就是Model,处理面试的这个事情. 


2.什么是Spring Web MVC

Spring Web MVC是基于Servlet API构建的原始框架,简称Spring MVC,即Spring Web MVC 是⼀个 Web 框架.

掌握Spring MVC就需要理解1.MVC,2.Web框架

在创建SpringBoot项目时,我们勾选的23选项Spring Web就是SpringMVC的框架~

不要懵~SpringBoot只是实现SpringMVC的其中一种方式而已~

用一张图来更好地诠释Spring实现MVC:

掌握的三个方面:建立连接、请求、响应.


3.SpringMVC和MVC的关系

MVC是一种思想,SpringMVC是MVC思想的一种具体实现.

即SpringMVC是实现了MVC思想,并继承了Serlvet API的web框架.


4.使用SpringMVC

4.1创建SpringMVC

首先在SpringBoot项目时,再添加依赖的时候,添加SpringWeb.


4.2建立链接

创建一个HelloController类,实现用户用过浏览器和程序之间的交互:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    @RequestMapping("/sayHello")
    //方法名和路径名无需一致
    public String sayHello() {
        return "hello,SpringBoot";
    }
}

直接访问:

http://127.0.0.1:8080/sayHello

 就可以看到返回的数据了:


4.3注解介绍

4.3.1@RequestMapping

@RequestMapping:用来注册接口的路由映射的.

路由映射:当用户访问一个URL时,将用户的请求对应到程序中的某个类的某个方法时的过程

以上述代码为例:表示服务收到请求时,路径/sayHello的请求就会调用SayHello这个方法的代码

注意:

1.@RequestMapping的URL路径最前面加不加/(斜杠)都可以!但是在企业开发的角度来讲,统一要求:前加后不加~

@RestController
public class HelloController {
    @RequestMapping("sayHello")
    public String sayHello() {
        return "hello,SpringBoot";
    }
}

2.@RequestMapping注解中没有写属性名,默认是value;若要限制请求方式,就要使用method属性~

     只想支持某种方法的话,那就method = RequestMethod.方法

     GET:method = RequestMethod.GET;POST:method = RequestMethod.POST

@RestController
public class HelloController {
    @RequestMapping(value = "/sayHello",method = RequestMethod.GET)
    public String sayHello() {
        return "hello,SpringBoot";
    }
}

3.@RequestMapping:即可以修饰类(请求初始信息),也可以修饰方法(请求具体信息);当修饰类和方法时,访问的地址是类路径 + 方法路径.以及@RequestMapping 的URL路径也可以是多层路径, 最终访问时, 依然:类路径 + 方法路径

@RequestMapping("/Hi/MVC")
@RestController
public class HelloController {
    @RequestMapping("/sayHello")
    public String sayHello() {
        return "hello,SpringBoot";
    }
}

访问:http://127.0.0.1:8080/Hi/MVC/sayHello


4.3.2@RestController

上述中,@RequestMapping已经满足了我们的访问要求,那为什么还要加@RestController呢?

当我们把@RestController删除后访问发现程序报错404,这个就是@RestController的作用~


4.4获取参数

4.4.1获取单个参数

@RequestMapping("/param")
@RestController
public class ParamController {
    @RequestMapping("/m1")
    public String m1(String name) {
        return "接收到的参数name:" + name;
    }
}

 http://127.0.0.1:8080/param/m1?name=zhangsan访问 http://127.0.0.1:8080/param/m1?name=zhangsan 

发现后端成功接收到了name参数的值,在赋值时一定要看好参数是否一致.

在使用基本类型来接受参数时,参数必传(除boolean类型),否则会报500错误;当类型不匹配时,会报400错误.


4.4.2传递参数

正常传递

    @RequestMapping("/m1/int")
    public Object method1GetInt(int age){
        return "接收到参数age:" + age;
    }
}

通过Fiddler观察请求和响应,HTTP响应状态码为200,Content-Type为text/html

当不传递某个参数时:

通过Fiddler观察请求和响应,HTTP响应状态码为500

HTTP响应状态码为500:服务器出现内部错误. 一般是服务器的代码执行过程中遇到了一些特殊情况(服务器异常崩溃)会产生这 个状态码.

那么该怎么解决呢?

打开你的IDEA,观察日志!从下往上看,按照报错的信息解决错误即可~

int类型的参数'age',虽然为可选的, 但由于被声明为基本类型⽽不能转换为空值. 考虑将其声明为对应基本类型的包装类型.

传递参数类型不匹配

通过Fiddler观察请求和响应,HTTP响应状态码为400

HTTP响应状态码为400:客户端错误,表示服务器无法处理请求.

其中4xx里的404状态:表示用户访问的资源不存在,大概率是 URL 的路径写的不正确,比如注解,url单词拼写错误

对于包装类型, 如果不传对应参数,Spring 接收到的数据则为null 所以企业开发中,对于参数可能为空的数据,建议使用包装类型!


4.4.3传递对象

创建一个Person对象:

package com.example.demo.controller;

public class Person {
    Integer id;
    String name;
    Integer age;

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

传递对象代码实现:

    @RequestMapping("/m4")
    public String m4(Person person) {
        return "接收到的参数person:" + person.toString();
    }

访问 http://127.0.0.1:8080/param/m4?id=5&name=dake&age=18


4.4.4后端参数映射

    @RequestMapping("/m11")
    public Object method_4(@RequestParam("time") String createtime) {
        return "接收到参数createtime:" + createtime;
    }

访问​​​​​​http://127.0.0.1:8080/param/m11?time=2023-10-29

如果浏览器使用createtime进行参数传递呢?

错误日志信息:显示time不存在

以上说明:@RequestParam

1.使用@RequestParam 进行参数重命名时, 请求参数只能和 @RequestParam 声明的名称一致, 才能进行参数绑定和赋值.

2. 使用@RequestParam 进行参数重命名时, 参数就变成了必传参数.

那么如何修改必传参数为非必传呢?

直接ctrl左键进入required内部发现,required 的默认值为true, 表示含义就是: 该注解修饰的参数默认为必传

那我们可以通过设置 @RequestParam 中的 required=false 来避免不传递时报错

注解属性赋值时, 没有指明key的话, 默认为value属性. 如果需要有多个属性进行赋值时, 需要写上key!


4.4.5传递数组

    @RequestMapping("/m6")
    public String m6(String[] arrayParam) {
        return "接收到的arrayParam:" + Arrays.toString(arrayParam) + ",长度是:" + arrayParam.length;
    }

访问http://127.0.0.1:8080/param/m6?arrayParam=zhangsan,lisi,wangwu


4.4.6传递集合

    @RequestMapping("/m7")
    public String m7(@RequestParam List<String> ListParam) {
        return "接收到的ListParam:" + ListParam + ",长度是:" + ListParam.size();
    }

访问http://127.0.0.1:8080/param/m7?ListParam=zhangsan,lisi,wangwu

也可以通过Postman传参

Spring Web MVC(1)篇到此结束啦~ 2篇马上就来~

点个关注不迷路~谢谢各位大佬的支持!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值