【整理】controller中常用注解

目录

  • [1. @Controller](#1. @Controller)
  • [2. @RestController](#2. @RestController)
  • [3. @RequestMapping](#3. @RequestMapping)
  • [4. @RequestParam](#4. @RequestParam)
  • [5. @PathVariable](#5. @PathVariable)
  • [6. @ModelAttribute](#6. @ModelAttribute)

1. @Controller

@Controller用于标识一个类为Spring MVC的控制器。这个类的方法可以用于http请求,并返回视图或数据

2. @RestController

@RestController=@Controller+@ResponseBody,用于RESTful风格的控制器。这个类的方法可以用于返回JSON或者XML格式的数据

3. @RequestMapping

@RequestMapping用来指定请求的URL路径、HTTP方法以及其他请求条件。

java 复制代码
@Controller
public class MyController {
    // 处理GET请求
    //              指定请求路径         指定请求方法                 指定请求包含参数
    //请求url:http://localhost:8080/greeting?type=example
    @RequestMapping(value = "/greeting", method = RequestMethod.GET, params = "type=example")
    public String greeting() {
    		return "greeting";
    }
}

@GetMapping、@PostMapping、@PutMapping、@DeleteMapping这些注解分别等同于指定了HTTP方法GET、POST、PUT和DELETE的@RequestMapping注解。

4. @RequestParam

@RequestParam用于将请求参数绑定到方法参数上。

java 复制代码
@Controller
public class MyController {
    // 处理GET请求,并从请求中提取参数
    @RequestMapping(value = "/greet", method = RequestMethod.GET)
    public String greet(
    //           指定请求参数名称   参数不必须       默认值
    @RequestParam(name = "name", required = false, defaultValue = "Guest") String name
    ) {
        return "greet:"+name;
    }
}

5. @PathVariable

@PathVariable用于将URL中的占位符参数绑定到方法参数上。

java 复制代码
@RestController
public class MyController {
 	 @GetMapping("/users/{userId}")
    public String getUserById(@PathVariable("userId") String userId) {
        return "User ID: " + userId;
    }
}

6. @ModelAttribute

@ModelAttribute用于将请求参数绑定到模型对象上,并将该对象添加到模型中,以便在视图中使用。

示例

java 复制代码
@Controller
public class MyController {
    @RequestMapping("/")
    public String index() {
        return "user";
    }
    // 处理GET请求,返回表单页面
    @RequestMapping(value = "/userForm", method = RequestMethod.GET)
    public String showForm(Model model) {
        model.addAttribute("user", new User());
        return "user";
    }
    // 处理POST请求,接收表单提交的数据
    @RequestMapping(value = "/submitForm", method = RequestMethod.POST)
    public String submitForm(@ModelAttribute("user") User user, Model model) {
        model.addAttribute("message", "User submitted: " + user.getName());
        return "user";
    }
}

User类

java 复制代码
public class User {
    private String name;
    private String email;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

user.html

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>User Form</h1>
<form action="/submitForm" method="post">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>
    <br>
    <button type="submit">Submit</button>
</form>
<div>
    <h2>Message:</h2>
    <p th:text="${message}"></p>
</div>
</body>
</html>
相关推荐
用户3521802454753 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
昵称为空C3 天前
手撸一个动态 SQL 执行引擎:不重启服务,在线增删改查任意数据库
spring boot·后端
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
于先生吖4 天前
SpringBoot对接大模型开发AI命理测算系统:八字排盘与AI解析接口源码全解
人工智能·spring boot·后端
Flittly4 天前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring
星落zx4 天前
Spring Boot 多模型集成:优雅调用全球主流大模型
人工智能·spring boot·chatgpt
一杯奶茶¥4 天前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目
进阶的小名4 天前
Spring Boot SSE + Nginx 配置:解决 EventSource 不实时返回、连接超时、流式响应被缓冲问题
spring boot·后端·nginx
我登哥MVP4 天前
SpringCloud Alibaba 核心组件解析:服务链路追踪
java·spring boot·后端·spring·spring cloud·java-ee·maven