文章目录
- 一、测试多控制器@RequesMapping
- 二、测试@RequestMapping位置
- 三.测试value属性
- 四.测试@RequestMapping的Method属性
- 五.@RequestMapping的params属性
- 六.测试@RequestMapping中的占位符
提示:以下是本篇文章正文内容,下面案例可供参考
springMVC @RequesMapping常见报错状态码
| 报错 | 结论 |
|---|---|
| 浏览器报错405 | 若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性 |
| 页面报错400 | 请求满足@RequestMapping注解的value和method属性,但是不满足params属性 |
| 页面报错404 | 当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性或value路径不匹配 |
一、测试多控制器@RequesMapping
结论:@RequestMapping 映射访问绝对路径必须唯一!
二、测试@RequestMapping位置
修改为
访问成功
结论:@RequestMapping @RequestMapping标识一个类:设置映射请求的请求路径的初始信息 @RequestMapping标识一个方法:设置映射请求路径的具体信息 即先访问类路径,才能访问方法上的映射路径
三.测试value属性
@Controller
//@RequestMapping("/hello")
public class controller {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping(value = {"/welcome2022","test"})
public String success(){
return "success";
}
}
<h1>首页</h1>
<a th:href="@{/welcome2022}">测试@RequestMapping的value属性-->/welcome2022</a></br>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
结论:value属性提供了组件复用. @RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求 @RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射
四.测试@RequestMapping的Method属性
package com.vector.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
//@RequestMapping("/hello")
public class controller {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping(value = {"/welcome2022","test"},method = {RequestMethod.GET})
public String success(){
return "success";
}
}
如果不写Method默认get提交
若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错405:Request method 'POST' not supported
对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解
处理get请求的映射–>@GetMapping
处理post请求的映射–>@PostMapping
处理put请求的映射–>@PutMapping
处理delete请求的映射–>@DeleteMapping
五.@RequestMapping的params属性
可以识别判断符
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/(usename='user',password=123456)}">测试@RequestMapping中params方法</a>
</body>
</html>
package com.vector.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
//@RequestMapping("/hello")
public class controller {
@RequestMapping(value = {"/"},params = {"username=user","password=123456"})
public String index(){
return "index";
}
@RequestMapping(value = {"/welcome2022","test"})
public String success(){
return "success";
}
}
结论:相当于一个token令牌.
六.测试@RequestMapping中的占位符
<a th:href="@{/testRest/1/admin}">测试路径中的占位符-->/testRest</a><br>
@RequestMapping("/testRest/{id}/{username}")
//@PathVariable注解将占位符中的值与形参进行绑定
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
System.out.println("id:"+id+",username:"+username);
return "success";
}
//最终输出的内容为-->id:1,username:admin
使用占位符必须包含占位符这一级的路径.