(2).普通参数与基本注解
(1.1)、注解:
注解通常有三个源码属性。 name="" 接受的名字? value="" 接受的名字? name和value因为互相为别名所以两者都一样的作用,用谁都一样 required ="" 是否必须接受到值? 1. 主要用于Rest风格传参方面。 @PathVariable: 假如说方法里面有一个 Map<String,String>的参数,那么SPringBoot会自动帮助我们以键值对的方式进行自动收集里面的数据。 2. 主要用于获取请求头 @RequestHeader: 加入方法里面有一个Map<String,String>的参数,那么所有的请求头都会放进去。 3.获取HttpRequest设置的值(这是一个系列的) @RequestAttribute: 主要是获取setAttribute(k,v)的值 4.获取非REST风格传参的参数 @RequestParam: 假如方法里面有一个Map<String,String>的参数,那么获取到的参数都会放在这里 5. 矩阵注解 @MatrixVariable: 假如方法里面有一个 6.获取Cookie的值 @CookieValue: 假如方法里面有一个Cookie对象,那么获取到的cookie的名字和值都会放在这里。 7. 获取POST的非REST值 @RequestBody: 假如是POST请求,那么会获取到POST的非REST的参数值。 8. 重定向注解 @RedirectAttributes : 通过addAtribute()方法给重定向进行自动拼接
- 非矩阵注解
非@RequestAttribute的后端代码
package com.jsxs.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Author Jsxs * @Date 2023/7/3 16:14 * @PackageName:com.jsxs.controller * @ClassName: ParamterTestController * @Description: TODO * @Version 1.0 */ @Controller @ResponseBody public class ParameterTestController { // 1. @PathVariable 注解 : 获取REST的值 @GetMapping("/car/{id}/owner/{username}/{df}") public Map<String, Object> getCar1(@PathVariable("id") Integer id, @PathVariable("username") String username, @PathVariable("df") String df, @PathVariable Map<String, String> mp) { HashMap<String, Object> map = new HashMap<>(); map.put("id", id); map.put("name", username); map.put("df", df); map.put("mp", mp); return map; } // 2.@RequestHeader 注解 : 获取请求头的信息 @GetMapping("/car/{id}/owner/{username}") public Map<String, Object> getCar2(@PathVariable("id") Integer id, @PathVariable("username") String username, @PathVariable Map<String, String> mp, @RequestHeader("User-Agent") String userAgent, @RequestHeader Map<String, String> header) { HashMap<String, Object> map = new HashMap<>(); map.put("id", id); map.put("name", username); map.put("mp", mp); map.put("userAgent", userAgent); map.put("header", header); return map; } // 3. @RequestParam 注解 : 获取GET拼接的提交的值 @GetMapping("/car/{id}/owner3/{username}") public Map<String, Object> getCar3(@PathVariable("id") Integer id, @PathVariable("username") String username, @PathVariable Map<String, String> mp, @RequestHeader("User-Agent") String userAgent, @RequestHeader Map<String, String> header, @RequestParam("age") Integer age, @RequestParam("inters") List<String> inters, @RequestParam Map<String, String> params ) { HashMap<String, Object> map = new HashMap<>(); map.put("id", id); map.put("name", username); map.put("mp", mp); map.put("userAgent", userAgent); map.put("header", header); map.put("age", age); map.put("inters", inters); map.put("params", params); return map; } // 4. @CookieValue :获取指定的cookie值 @GetMapping("/car/{id}/owner4/{username}") public Map<String, Object> getCar4(@PathVariable("id") Integer id, @PathVariable("username") String username, @PathVariable Map<String, String> mp, @RequestHeader("User-Agent") String userAgent, @RequestHeader Map<String, String> header, @RequestParam("age") Integer age, @RequestParam("inters") List<String> inters, @RequestParam Map<String, String> params, @CookieValue("Idea-d024f886") String cookie_ga, @CookieValue("Idea-d024f886") Cookie cookie ) { HashMap<String, Object> map = new HashMap<>(); map.put("id", id); map.put("name", username); map.put("mp", mp); map.put("userAgent", userAgent); map.put("header", header); map.put("age", age); map.put("inters", inters); map.put("params", params); map.put("cookie_ga", cookie_ga); System.out.println("通过注解获取到Idea-d024f886的cookie对象为:" + cookie); return map; } // 5.@RequestBody 注解: 主要作用获得POST提交的数据 @PostMapping("/save") public Map postMethod(@RequestBody String content) { Map<String, Object> map = new HashMap<>(); map.put("content", content); return map; } }
@RequestAttribute的后端代码
package com.jsxs.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.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.Map; /** * @Author Jsxs * @Date 2023/7/3 18:34 * @PackageName:com.jsxs.controller * @ClassName: RequestController * @Description: TODO 1.HttpServletRequest 经过一次转发就失效(利用这个重定向是获取不到值的,因为重定向属于第二次转发了) 2.HttpSession 浏览器关闭失效 3. HttpServletContent 服务器关闭 * @Version 1.0 */ @Controller public class RequestController { @Resource HttpSession session; @GetMapping("/goto") public String goToPage(HttpServletRequest httpServletRequest){ httpServletRequest.setAttribute("info","转发成功了.."); httpServletRequest.setAttribute("msg","jsxs"); // 切记如果我们没有使用thymeleaf的话,是不能实现前后端跳转的。 (下面会显示找不到MVC) return "forward:/success"; // 转发到 /success 请求;并不是转发到success页面的。 } @ResponseBody @GetMapping("/success") public Map<String, Object> SuccessPage(HttpServletRequest httpServletRequest, @RequestAttribute("msg") String name){ String info = (String)httpServletRequest.getAttribute("info"); System.out.println(info+" "+name); HashMap<String, Object> map = new HashMap<>(); map.put("info_代码方式",info); map.put("msg_注解方式",name); return map; } }
index.html 前端
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>你好</h1> <form action="/user" method="post"> <button type="submit">Post方式进行跳转</button> </form> <form action="/user" method="get"> <button type="submit">Get方式进行跳转</button> </form> <form action="/user" method="post"> <input name="aaaa" type="hidden" value="DELETE"> <button type="submit">DELETE方式进行跳转</button> </form> <form action="/user" method="post"> <input name="aaaa" type="hidden" value="PUT"> <button type="submit">PUT方式进行跳转</button> </form> <ul> <a href="http://localhost:8080/car/1/owner/jsxs/df"> @PathVariable注解</a> </ul> <ul> <a href="http://localhost:8080/car/1/owner/jsxs"> @PathVariable注解 + @RequestHeader</a> </ul> <ul> <a href="http://localhost:8080/car/1/owner3/jsxs?age=18&inters=basketball&inters=game"> @PathVariable注解 + @RequestHeader + @RequestParam</a> </ul> <ul> <a href="http://localhost:8080/car/1/owner4/jsxs?age=18&inters=basketball&inters=game"> @PathVariable注解 + @RequestHeader + @RequestParam + @CookieValue</a> </ul> <form method="post" action="/save"> <input value="liming" name="user" type="hidden"> <input value="123456" name="password" type="hidden"> <button type="submit">@RequestBody注解</button> </form> <ul> <a href="http://localhost:8080/goto">@RequestAttribute注解(这是一个系列的中的其中一个)</a> </ul> </body> </html>
- 矩阵注解
我们要开启矩阵注解的支持,因为SpringBoot默认是关闭的
WebMvcAutoConfiguration类下 -> configurePathMatch()方法体中 -> UrlPathHelper类中 -> removeSemicolonContent属性默认为(true)
假如我们访问矩阵路径的时候报错400 (请求异常) 就是我们的配置矩阵注解的支持。
1. 第一种配置方式 : 继承WebMvcConfigurer 接口 + 实现configurePathMatch方法
2.第二种配置方式: @Bean WebMvcConfigurer接口并重写里面的方法
3.因为JDK1.8支持接口默认方法所以我们不必要重写接口中所有的方法。
配置文件:
package com.jsxs.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.HiddenHttpMethodFilter; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.util.UrlPathHelper; /** * @Author Jsxs * @Date 2023/7/3 11:13 * @PackageName:com.jsxs.config * @ClassName: WebConfig * @Description: TODO * @Version 1.0 */ @Configuration(proxyBeanMethods = false) // 第一种方式 @Configuration + 实现WebMvcConfigurer接口 (因为JDK8允许接口的默认方法和默认实现所以我们不需要将所有方法全部重写) // 第二种方式: @Configuration +@Bean 重新注入我们的组件 public class WebConfig /*implements WebMvcConfigurer */{ @Bean public HiddenHttpMethodFilter hiddenHttpMethodFilter(){ HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter(); hiddenHttpMethodFilter.setMethodParam("aaaa"); return hiddenHttpMethodFilter; } // @Override // public void configurePathMatch(PathMatchConfigurer configurer) { // UrlPathHelper helper = new UrlPathHelper(); // helper.setRemoveSemicolonContent(false); // configurer.setUrlPathHelper(helper); // } @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer(){ @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper helper = new UrlPathHelper(); helper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(helper); } }; } }


