Spring Boot 学习(一)@RequestAttribute @MatrixVariable @UrlPathHelper
时间:2022-03-29 01:43
@GetMapping("/send") public String getmessage(HttpServletRequest request) { request.setAttribute("msg","跳转"); //设置参数 request.setAttribute("code",123); return "forward:/success"; //转发到success请求 }
@ResponseBody //注意要加上这个注解,否则会出现500报错 @GetMapping("/success") public Map success(@RequestAttribute("msg") String msg, //通过RequestAttribute获取请求域中的所有值 @RequestAttribute("code") Integer code, HttpServletRequest request) { //获取到send的数据msg Object msg1 = request.getAttribute("msg"); Map<String,Object> map = new HashMap<>(); map.put("reqMethod_msg",msg1); //获取原生servlet中的request的值 map.put("annotation_msg",msg); //获取基于注解额request的值 return map; }
值得注意的是如果只声明了Controller那么要在需要显示数据在浏览器的时候需要加上注解@ResponseBody
package com.sp.Controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestAttribute; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @Controller //这里只声明了Controller注解 public class RequestController { @GetMapping("/send") public String getmessage(HttpServletRequest request) { request.setAttribute("msg","跳转"); request.setAttribute("code",123); return "forward:/success"; //转发到success请求 } @ResponseBody //因此在显示页面数据需要加上注解ResponseBody @GetMapping("/success") public Map success(@RequestAttribute("msg") String msg, //通过RequestAttribute获取请求域中的所有值 @RequestAttribute("code") Integer code, HttpServletRequest request) { //获取到send的数据msg Object msg1 = request.getAttribute("msg"); Map<String,Object> map = new HashMap<>(); map.put("reqMethod_msg",msg1); //获取原生servlet中的request的值 map.put("annotation_msg",msg); //获取基于注解额request的值 return map; } }
注意在@RestController 包含了@Controller 和 @ResponseBody 这两个注解,所以在访问请求的时候可以显示数据ResponseBody
@MatrixVariable 矩阵变量
Spring Boot 禁用了矩阵变量,需要手动开启
有两种写法在
在UrlPathHelper中默认情况下是移除分号内容 @UrlPathHelper // low=34;brand=byd,audi,yd
// SpringBoot默认是禁用了矩阵变量的功能
// 手动开启:对于路径的处理,UrlPathHelper进行解析
// UrlPathHelper -> private boolean removeSemicolonContent = true; (移除分号内的内容)
// 在@Configuration中重写configurePathMatch方法,将removeSemicolonContent改为false
注意:之后在controller中,注意这里的矩阵变量要绑定在路径变量中所以加上{path},否则出现404@GetMapping("/cars/{path}") //矩阵变量必须有url路径变量才能被解析,否则404
public Map carsell(@MatrixVariable("low") Integer low, //不是请求参数,而是矩阵变量,需要加上注解,矩阵变量要绑定在路径变量中 @MatrixVariable("brand") List<String> brand, @PathVariable("path") String path) { //需要加上路径访问变量的注解 Map<String,Object> map = new HashMap<>(); map.put("low",low); map.put("brand",brand); map.put("path",path); return map; }
<a href="/cars/sell;low=34;brand=byd,audi,yd">MatrixVariable(矩阵变量)</a>
/boss/1;age=34/2;age=22 boss1 路径下的age 2路径下的age,
需要注意的是,传递的参数名都一样的时候,需要在矩阵变量的注解中加上pathVar来加以区分,否则会出现404@GetMapping("/boss/{bossId}/{empId}")
public Map bossmessage(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossage, //当传入的参数名一样时,需要哟波pathVar进行区分
@MatrixVariable(value = "age",pathVar = "empId") Integer empage) {
Map<String,Object> map = new HashMap<>();
map.put("bossage",bossage);
map.put("empage",empage);
return map;
}
<a href="/boss/1;age=34/2;age=22">MatrixVariable(矩阵变量2)</a>