目录
4.@PostMapping、@GetMapping、@PutMapping、 @DeleteMapping
在contrller包下创建MyController3控制类
父路径是/c3,子路径是/annotation1,所以函数annotation1的路径是/c3/annotation1
- package com.first.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
-
- @Controller
- @RequestMapping("/c3")
- public class MyController3 {
- /*
- 访问路径为 /c3/annotation1
- 支持post和get请求
- 请求时必须带有age参数
- 请求时必须带有User-agent请求头
- */
- @RequestMapping(value = "/annotation1",
- //get和post都写进来表示get或post都行
- method = {RequestMethod.GET,RequestMethod.POST},
- //规定必须发送的请求参数
- params = {"username"},
- //规定请求必须包含的请求头
- headers = {"User-agent"})
- public String annotation1(String username){
- System.out.println(username);
- //会跳转到http://localhost:8080/first.jsp
- return "first";
- }
- }
输出:访问http://localhost:8080/c3/annotation1?username=kd
且控制台输出kd
在前面咱们已经知道如果方法的参数和请求参数名称一致的话,那么SpringMVC可以自动进行对应封装,就像上面那个函数一样,如果不一致的情况下还想封装的话就得用RequestParam。
- /*
- 定义请求的参数名为username,默认值为"默认名",不是必须的参数(即url不传参也不会报错)
- */
- @RequestMapping("/annotation2")
- public String annotation2(@RequestParam(name = "username",defaultValue = "默认名",required = false) String name){
- System.out.println(name);
- //跳转到first.jsp
- return "first";
- }
访问http://localhost:8080/c3/annotation2,输出:

控制台输出:
默认名
访问:http://localhost:8080/c3/annotation2?username=kd
控制台输出:kd
- /*
- 获取User-Agent请求头
- 获取JSESSIONID的Cookie值
- */
- @RequestMapping("/annotation3")
- public String annotation3(@RequestHeader("User-Agent") String userAgent, @CookieValue("JSESSIONID") String jSessionId){
- System.out.println(userAgent);
- System.out.println(jSessionId);
- return "first";
- }
控制台输出:
- Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
- 247A4659A05334A8CF1C68B77AABF6D1
- package com.first.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.SessionAttributes;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
-
- @Controller
- @RequestMapping("/c4")
- // 将模型中的name数据保存到session中
- @SessionAttributes("name")
- public class MyController4 {
- @RequestMapping("/t1")
- public String t1(Model model){
- //model中保存name数据
- model.addAttribute("name","英语");
- return "first";
- }
-
- @RequestMapping("/t2")
- public String t2(HttpServletRequest request){
- System.out.println(request.getAttribute("name"));
- return "first";
- }
-
- @RequestMapping("/t3")
- public String t3(HttpSession session){
- System.out.println(session.getAttribute("name"));
- return "first";
- }
- }
访问http://localhost:8080/c4/t2,控制台输出:
因为存放在Model中只有同一个request中才能共享数据,所以t2拿不到name
null
访问http://localhost:8080/c4/t3,控制台输出:
英语
- package com.first.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- @RequestMapping("/c5")
- public class MyController5 {
- @ModelAttribute
- public void before(){
- System.out.println("前置方法");
- }
- @RequestMapping("/t1")
- public String t1(){
- System.out.println("t1");
- return "first";
- }
- }
- 前置方法
- t1
- package com.first.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- @RequestMapping("/c6")
- public class MyController6 {
- // 前置方法向Model中设置数据
- @ModelAttribute
- public void before(Model model){
- model.addAttribute("name","梦露");
- }
- // 该参数不是从请求中获取,而是从Model中获取
- @RequestMapping("/t1")
- public String t1(@ModelAttribute("name") String name){
- System.out.println(name);
- return "first";
- }
- }
输出

梦露
传统URL:
- 查找id为1的学生: http://localhost:8080/student/findById?id=30
- 删除id为1的学生: http://localhost:8080/student/deleteById?id=30
RESTful风格URL:
- 查找id为30的学生: http://localhost:8080/student/30
- 删除id为30的学生: http://localhost:8080/student/30
RESTful风格URL:
- 查找id为30的学生: http://localhost:8080/student/30 GET方式请求
- 删除id为30的学生: http://localhost:8080/student/30 DELETE方式请求
点击New

选择Collection,可以将常用的访问链接放到此集合下,以后访问直接在此集合下访问就行,方便了很多,不用每次都手动输入url

为集合起个名

点击加号新增请求

- package com.first.controller;
-
- import com.first.domain.Student;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
-
- @Controller
- @RequestMapping("/student")
- // 模拟学生的增删改查控制器
- public class StudentController {
- // 路径中的{id}表示占位符,最后会封装到方法的参数中使用
- // 删除学生
- @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
- public String deleteStudent(@PathVariable(value = "id") int id){
- System.out.println("删除id为"+id+"的学生");
- return "first";
- }
- // 如果占位符和参数名相同,可以省略@PathVariable的value属性
- // 根据id查询学生
- @RequestMapping(value = "/{id}",method = RequestMethod.GET)
- public String findStudentById(@PathVariable int id){
- System.out.println(id);
- System.out.println("根据id查询学生");
- return "first";
- }
- // 新增学生
- @RequestMapping(value = "/{id}",method = RequestMethod.POST)
- public String addStudent(@PathVariable int id, Student student){
- System.out.println(id);
- System.out.println(student);
- System.out.println("新增学生");
- return "first";
- }
- // 修改学生
- @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
- public String updateStudent(@PathVariable int id, Student student){
- System.out.println(id);
- System.out.println(student);
- System.out.println("修改学生");
- return "first";
- }
- }
使用postman进行测试,点击加号新建一个测试,查询学生为例

选择请求方式,并输入URL,并Ctrl+S,然后为这个测试命名并选择一个集合,然后保存

其他三个操作也与此差不多,需要传入参数时就在key和value处添加即可,如新增学生,不过如果写入的地址中有?后面的内容,那么会自动解析到key和value中,如果只写了http://localhost:8080/,那么在key和value中添加值后也会自动添加到url中。

四个都完成后,看左边会发现集合下有四个测试

启动服务器,在postman中执行四个测试,idea控制台输出如下:
- 1
- 根据id查询学生
- 删除id为1的学生
- 1
- Student{id=1, name='小明', sex='男', address=null}
- 新增学生
- 1
- Student{id=1, name='小白', sex='男 ', address=null}
- 修改学生
- package com.first.controller;
-
- import com.first.domain.Student;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
-
- @Controller
- @RequestMapping("/student")
- // 模拟学生的增删改查控制器
- public class StudentController {
- // 路径中的{id}表示占位符,最后会封装到方法的参数中使用
- // 删除学生
- //@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
- @DeleteMapping("/{id}")
- public String deleteStudent(@PathVariable(value = "id") int id){
- System.out.println("删除id为"+id+"的学生");
- return "first";
- }
- // 如果占位符和参数名相同,可以省略@PathVariable的value属性
- // 根据id查询学生
- //@RequestMapping(value = "/{id}",method = RequestMethod.GET)
- @GetMapping("/{id}")
- public String findStudentById(@PathVariable int id){
- System.out.println(id);
- System.out.println("根据id查询学生");
- return "first";
- }
- // 新增学生
- //@RequestMapping(value = "/{id}",method = RequestMethod.POST)
- @PostMapping("/{id}")
- public String addStudent(@PathVariable int id, Student student){
- System.out.println(id);
- System.out.println(student);
- System.out.println("新增学生");
- return "first";
- }
- // 修改学生
- //@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
- @PutMapping("/{id}")
- public String updateStudent(@PathVariable int id, Student student){
- System.out.println(id);
- System.out.println(student);
- System.out.println("修改学生");
- return "first";
- }
- }
用postman测试,和上节一样,成功!
- <!-- 请求方式过滤器 -->
- <filter>
- <filter-name>httpMethodFilter</filter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>httpMethodFilter</filter-name>
- <!--所有路径都过滤-->
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- package com.first.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.DeleteMapping;
- import org.springframework.web.bind.annotation.PutMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- @RequestMapping("/c7")
- public class MyController7 {
- @DeleteMapping("/delete")
- public String testDelete(){
- System.out.println("删除方法");
- return "first";
- }
- @PutMapping("/put")
- public String testPut(){
- System.out.println("修改方法");
- return "first";
- }
- }
- <%--
- Created by IntelliJ IDEA.
- User: ASUS
- Date: 2022/6/23
- Time: 21:03
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>DELETE、PUT提交</title>
- </head>
- <body>
- <!-- 删除 -->
- <%-- 提交DELETE、PUT请求,表单必须提交方式为post --%>
- <%-- 表单中有一个隐藏域,name值为_method,value值为提交方式 --%>
- <form action="/c7/delete" method="post">
- <input type="hidden" name="_method" value="DELETE">
- <input type="submit" value="删除">
- </form>
- <hr/>
- <!-- 修改 -->
- <form action="/c7/put" method="post">
- <input type="hidden" name="_method" value="PUT">
- <input type="submit" value="修改">
- </form>
- </body>
- </html>
测试,访问 http://localhost:8080/first2.jsp

点击两个按钮分别输出
- 删除方法
- 修改方法
说明post被过滤而成了delete和put。