SpringMVC 中的常用注解和用法

目录

前言:

1.@RequestMapping

2.@RequestParam

3.@RequestBody

4.@PathVariable

5.@RestController(返回页面)

6.@Controller(返回视图)

7.@CookieValue

8.@SessionAttribute


前言:

MVC是⼀种架构设计模式,也⼀种思想,⽽SpringMVC是对MVC思想的具体实现.除此之外,Spring MVC还是⼀个Web框架.

总结来说,Spring MVC是⼀个实现了MVC模式的We框架


1.@RequestMapping

@RequestMapping 是Spring Web MVC应⽤程序中最常被⽤到的注解之⼀,它是⽤来注册接⼝的路由映射的

路由映射:当⽤⼾访问⼀个URL时,将⽤⼾的请求对应到程序中某个类的某个⽅法的过程就叫路由映射
@RequestMapping既可修饰类,也可以修饰⽅法,当修饰类和⽅法时,访问的地址是类路径+⽅法路径

@RequestMapping标识⼀个类:设置映射请求的请求路径的初始信息

@RequestMapping标识⼀个⽅法:设置映射请求请求路径的具体信息

@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping("/sayHi")
    public String sayHi(){
        return "hello,Spring MVC";
    }
}

访问地址:http://127.0.0.1:8080/user/sayHi
 

@RequestMapping 的URL路径也可以是多层路径,最终访问时,依然是类路径+⽅法路径

@RequestMapping("/user/m1")
@RestController
public class UserController {
    @RequestMapping("/say/hi")
    public String sayHi(){
        return "hello,Spring MVC";
    }
}

访问路径:http://127.0.0.1:8080/user/m1/say/hi
 


2.@RequestParam

用于将请求参数绑定到方法参数上(后端参数重命名,传递集合等)

@Controller
public class MyController {
 
    @RequestMapping("/greet")
    public String greet(@RequestParam("name") String name) {
        // Method logic using name parameter
        return "greetPage";
    }
}

 使用@RequestParam 注解的方法参数默认为必填参数

可以使用 required 属性将@RequestParam配置为可选参数:

@Controller
public class MyController {
 
    @RequestMapping("/greet")
    public String greet(@RequestParam(value = "name2", required = false) String name) {
        // Method logic using name parameter
        return "greetPage";
    }
}

3.@RequestBody

用于将 HTTP 请求体中的数据绑定到控制器方法的参数上,即获取 POST 请求中的数据并映射到方法参数。它通常用于处理 JSON 或 XML 格式的请求体数据

@Controller
@RequestMapping("/example")
public class ExampleController {

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public String addUser(@RequestBody User user) {
        // 处理用户数据
        return "User added successfully!";
    }
}

 在上述示例代码中,@RequestBody 用于将 HTTP 请求体中的数据转换成 User 对象,并将其作为参数传递给 addUser() 方法进行处理


4.@PathVariable

path variable:路径变量

和字⾯表达的意思⼀样,这个注解主要作⽤在请求URL路径上的数据绑定,默认传递参数写在URL上,SpringMVC就可以获取到

@RequestMapping("/m8/{id}/{name}")
public String method8(@PathVariable Integer id, @PathVariable("name") String
userName){
    return "解析参数id:"+id+",name:"+userName;
}

使⽤浏览器发送请求:http://127.0.0.1:8080/param/m8/5/zhangsan 


5.@RestController(返回页面)

其方法的返回值会被直接写入 HTTP 响应体中,而不是被视图解析器解析为视图。通常返回的是 JSON 或 XML 数据

@RestController
@RequestMapping("/example")
public class ExampleController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello, World!";
    }
}

6.@Controller(返回视图)

在方法中直接返回对象时,Spring MVC 会将其作为模型数据传递给视图解析器,然后解析为具体的视图

@Controller
public class MyController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "hello.html";
    }
    //返回html页面
}

 当在类注解中加上 @ResponseBody 时,返回的为数据而不是页面

@RestController=@Controller (返回视图)+ @ResponseBody(返回数据)

@Controller
public class MyController {
 
    @ResponseBody 
    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }
    //返回“hello”这个字符串
}

7.@CookieValue

简洁获取Cookie (一次只能获取一次cookie信息)

@RequestMapping("/getCookie")
public String cookie(@CookieValue("bite") String bite) {
    return "bite:" + bite;
}

8.@SessionAttribute

@SessionAttributes 注解用于指定哪些模型属性需要存储在会话

@RequestMapping("/getSess2")
public String sess2(@SessionAttribute(value = "username",required = false)
String username) {
    return "username:"+username;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深鱼~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值