【校招VIP】Spring MVC相关注解运用 —— 中篇

01月20日 收藏 0 评论 0 java开发

【校招VIP】Spring MVC相关注解运用 —— 中篇

转载声明:文章来源https://blog.csdn.net/qq_53317005/article/details/130502645

一、RESTful风格支持
1.1 RESTful风格介绍
RESTful风格是一种URL路径的设计风格。在RESTful风格的URL路径中,网络上的任意数据都可以看成一个资源,它可以是一段文本、一张图片,也可以是一个Java对象。而每个资源都会占据一个网络路径,无论对该资源进行增删改查,访问的路径是一致的。
传统URL:
查找id为1的学生:

1
http://localhost:8080/student/findById?id=30

删除id为1的学生:

1
http://localhost:8080/student/deleteById?id=30

RESTful风格URL:
查找id为30的学生:

1
http://localhost:8080/student/30

删除id为30的学生:

1
http://localhost:8080/student/30

那么如何区分对该资源是哪一种操作?通过请求方式不同,判断进行的是什么操作。
之前我们学过两种请求方式,GET请求和POST请求,而访问RESTful风格的URL一共有四种请求方式:
1)GET请求:查询操作
2)POST请求:新增操作
3)DELETE请求:删除操作
4)PUT请求:修改操作
RESTful风格URL:
查找id为30的学生:

1
http://localhost:8080/student/30 GET方式请求

删除id为30的学生:

1
http://localhost:8080/student/30 DELETE方式请求

RESTful风格的优点:
结构清晰、符合标准、易于理解、扩展方便。

1.2 postman使用
默认情况下浏览器是无法发送DELETE请求和PUT请求的,我们可以使用Postman工具发送这些请求。这里我已经把该工具上传到我的资源里面去了,有需要的读者可以去下载:
点击new-collection创建请求集合 


添加请求

保存请求到集合,以后可以随时发送该请求

测试:


二、@PathVariable
作用:在RESTful风格的URL中获取占位符的值
位置:方法参数前
属性:
value:获取哪个占位符的值作为参数值,如果占位符和参数名相同,可以省略该属性。

2.1 实例程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.example.controller;
  
import com.example.domain.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
  
import javax.servlet.http.HttpServletRequest;
  
@Controller
@RequestMapping("/student")
// 模拟学生的增删改查控制器
public class StudentController {
    // 路径的{id}表示占位符,最后会封装到方法的参数中使用
    // 删除学生
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String deleteStudent(@PathVariable("id") int id, HttpServletRequest request){
        System.out.println("删除id为"+id+"的学生");
        String str = "删除id为"+id+"的学生";
        request.setAttribute("delete",str);
        return "student";
    }
  
    // 如果占位符和参数名相同,可以省略@PathVariable的value属性
    // 根据id查询学生
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String findStudentById(@PathVariable int id,HttpServletRequest request){
        request.setAttribute("get","根据id查询学生:"+id);
        System.out.println("根据id查询学生\t"+id);
        return "student";
    }
  
    // 新增学生
    @RequestMapping(value = "/{id}",method = RequestMethod.POST)
    public String addStudent(@PathVariable int id, Student student, HttpServletRequest request){
        request.setAttribute("add",student.toString());
        System.out.println("新增学生:"+student+"\t"+id);
        return "student";
    }
  
    // 修改学生
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public String updateStudent(@PathVariable int id,Student student,HttpServletRequest request){
        System.out.println("修改学生\t"+id+"\t"+student);
        request.setAttribute("update","修改学生:"+student);
        return "student";
    }
}

JSP页面:

1
2
3
4
5
6
7
8
9
10
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 
 
    <title>student</title>
 
 
    <h1>Add:${requestScope.add}</h1>
    <h1>Delete:${requestScope.delete}</h1>
    <h1>Update:${requestScope.update}</h1>
    <h1>Get:${requestScope.get}</h1>

2.2 测试结果 





可以看到都是页面都是随着不同的请求方式,出来也页面也不一样。
看一下控制台是否打印对应的日志:

也都是成功显示的了。

三、@PostMapping、@GetMapping、@PutMapping、@DeleteMapping
为了简化请求方式@RequestMapping的写法,就产生了了这四个注解。写法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
为了简化请求方式@RequestMapping的写法,就产生了了这四个注解。写法如下:
 
@Controller
@RequestMapping("/student")
// 模拟学生的增删改查控制器
public class StudentController {
    // 路径的{id}表示占位符,最后会封装到方法的参数中使用
    // 删除学生
    //@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
    public String deleteStudent(@PathVariable("id") int id, HttpServletRequest request){
        System.out.println("删除id为"+id+"的学生");
        String str = "删除id为"+id+"的学生";
        request.setAttribute("delete",str);
        return "student";
    }
 
    // 如果占位符和参数名相同,可以省略@PathVariable的value属性
    // 根据id查询学生
    //@RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    public String findStudentById(@PathVariable int id,HttpServletRequest request){
        request.setAttribute("get","根据id查询学生:"+id);
        System.out.println("根据id查询学生\t"+id);
        return "student";
    }
 
    // 新增学生
    //@RequestMapping(value = "/{id}",method = RequestMethod.POST)
    @PostMapping("/{id}")
    public String addStudent(@PathVariable int id, Student student, HttpServletRequest request){
        request.setAttribute("add",student.toString());
        System.out.println("新增学生:"+student+"\t"+id);
        return "student";
    }
 
    // 修改学生
    //@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @PutMapping("/{id}")
    public String updateStudent(@PathVariable int id,Student student,HttpServletRequest request){
        System.out.println("修改学生\t"+id+"\t"+student);
        request.setAttribute("update","修改学生:"+student);
        return "student";
    }
}

四、@HiddenHttpMethodFilter
由于浏览器form表单只支持GET与POST请求,而DELETE、PUT请求并不支持。SpringMVC有一个过滤器,可以将浏览器的POST请求改为指定的请求方式,发送给的控制器方法。用法如下:
4.1 在web.xml配置过滤器

1
2
3
4
5
6
7
8
9
<!-- 请求方式过滤器 -->
  <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>

4.2 控制器方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.example.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;
  
import javax.servlet.http.HttpServletRequest;
  
@Controller
@RequestMapping("/c7")
public class MyController7 {
    @DeleteMapping("/delete")
    public String testDelete(HttpServletRequest request){
        System.out.println("删除方法");
        request.setAttribute("delete","删除方法");
        return "student";
    }
  
    @PutMapping("/put")
    public String testPut(HttpServletRequest request){
        request.setAttribute("update","修改方法");
        System.out.println("修改方法");
        return "student";
    }
}

4.3 JSP页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 
 
    <title>DELETE、PUT提交</title>
 
 
    <!-- 删除 -->
    <%-- 提交DELETE、PUT请求,表单必须提交方式为post--%>
    <%-- 表单中有一个隐藏域,name值为_method,value值为提交方式--%>
    <form action="/c7/delete" method="post">
        <input type="hidden" name="_method" value="DELETE">
        <input type="submit" value="删除">
    </form>
  
    <!-- 修改 -->
    <form action="/c7/put" method="post">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="修改">
    </form>

4.4 测试结果
我们去访问上面那个jsp:http://localhost:8080/delete-put.jsp


我们点击删除时:

点击修改时:


可以看得到都是成功请求的了。

C 0条回复 评论

帖子还没人回复快来抢沙发