controller层常用注解:@RequestMapping、@PostMapping、@GetMapping总结 以及 Spring MVC @GetMapping和@PostMapping注解


地址的父路径。

POST请求和GET请求的区别
可参考:

@GetMapping用于将HTTP get请求映射到特定处理程序的方法注解 具体来说,@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。

@PostMapping用于将HTTP post请求映射到特定处理程序的方法注解 具体来说,@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。

Spring MVC @GetMapping和@PostMapping注解的使用

创建HelloWorldController

  1. package com.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. @Controller
  7. public class HelloWorldController {
  8. //只接受get方式的请求
  9. @GetMapping("/testGetMapping")
  10. public String testGetMapping(Model model) {
  11. model.addAttribute("msg","测试@GetMapping注解");
  12. return "success";
  13. }
  14. //只接受post方式的请求
  15. @PostMapping("/testPostMapping")
  16. public String testPostMapping(Model model) {
  17. model.addAttribute("msg","测试@PostMapping注解");
  18. return "success";
  19. }
  20. }

创建index.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>indextitle>
  8. head>
  9. <body>
  10. <form action="testGetMapping" method="get">
  11. <button>测试@GetMapping注解button>
  12. form>
  13. <br>
  14. <form action="testPostMapping" method="post">
  15. <button>测试@PostMapping注解button>
  16. form>
  17. body>
  18. html>

创建success.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
  4. html>
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <title>successtitle>
  9. head>
  10. <body>
  11. ${requestScope.msg }
  12. body>
  13. html>

启动Tomcat访问index.jsp

点击【测试@GetMapping注解】

点击【测试@PostMapping注解】

原文地址1:https://blog.csdn.net/qq_44837912/article/details/103476053

原文地址2:https://blog.csdn.net/dwenxue/article/details/81586709

原文地址3:https://blog.csdn.net/magi1201/article/details/82226289