Spring Boot 學習(一)@RequestAttribute @MatrixVariable @UrlPathHelper
阿新 • • 發佈:2021-06-29
@GetMapping("/send") public String getmessage(HttpServletRequest request) { request.setAttribute("msg","跳轉"); //設定引數 request.setAttribute("code",123); return "forward:/success"; //轉發到success請求 }
@ResponseBody //注意要加上這個註解,否則會出現500報錯 @GetMapping("/success") publicMap success(@RequestAttribute("msg") String msg, //通過RequestAttribute獲取請求域中的所有值 @RequestAttribute("code") Integer code, HttpServletRequest request) { //獲取到send的資料msg Object msg1 = request.getAttribute("msg"); Map<String,Object> map = newHashMap<>(); 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 禁用了矩陣變數,需要手動開啟
有兩種寫法在@Configuration(proxyBeanMethods = false)註解下
package com.sp.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.util.UrlPathHelper; @Configuration(proxyBeanMethods = false) public class WebConfig /*implements WebMvcConfigurer */{ // 寫法一 // @Override // public void configurePathMatch(PathMatchConfigurer configurer) { //對configurePathMatch進行重寫 // UrlPathHelper urlPathHelper = new UrlPathHelper(); // urlPathHelper.setRemoveSemicolonContent(false); //將RemoveSemicolonContent設定為false,此時矩陣變數才能生效 // configurer.setUrlPathHelper(urlPathHelper); // } // 寫法二 @Bean //自己定義一個元件 public WebMvcConfigurer webMvcConfigurer() { return new WebMvcConfigurer() { public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(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>
@UrlPathHelper