SpringBoot 学习笔记08之Web开发的请求参数处理的基本注解

Learn from:尚硅谷

注解

@PathVariable、
可以获取path中我们选择自定义的地方的数据

@RequestHeader、
可以获取请求头中的数据

@RequestParam、
可以获取url的请求参数

@CookieValue、
用于获取cookie的值

    @GetMapping("/anima/{id}/create/{name}")
    public Map<String, Object> getAni(@PathVariable String id,
                                      @PathVariable String name,
                                      @PathVariable Map<String, Object> pv,
                                      @RequestHeader("User-Agent") String userAgent,
                                      @RequestHeader Map<String, Object> rh,
                                      @RequestParam("year") String year,
                                      @RequestParam("likes") Integer likes,
                                      @RequestParam Map<String, Object> rp,
                                      @CookieValue("Idea-216775ce") String Idea,
                                      @CookieValue("Idea-216775ce") Cookie cookie){


        Map<String, Object> mp = new HashMap<>();
        mp.put("anima", id);
        mp.put("create", name);
        mp.put("pv",pv);
        mp.put("user-agent",userAgent);
        mp.put("RequestHeader", rh);
        mp.put("year",year);
        mp.put("likes", likes);
        mp.put("RequestParam", rp);
        mp.put("idea", Idea);
        mp.put("CookieValue", cookie);
        return mp;
    }

http://localhost:8888/anima/2021/create/化物语?year=2021&likes=22223

下面是返回的数据
在这里插入图片描述

@RequestBody
用于获取请求体

    @PostMapping("/save")
    public Map<String, Object> postMethod(@RequestBody String content){
        Map<String, Object> mp = new HashMap<>();
        mp.put("content", content);
        return mp;
    }

访问的表单

<form action="/save" method="post">
    用户名<input name="username"><br>
    密  码<input name="passwd"><br>
    <input type="submit" value="提交">
</form>

提交
在这里插入图片描述
返回的是表单的数据的请求参数
在这里插入图片描述

@RequestAttribute
获取request域的属性
这里实现了一个转发的功能。

    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){
        request.setAttribute("msg","成功了。。。");
        request.setAttribute("code","200");
        return "forward:/success"; //转发到success请求
    }

    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute("msg") String msg,
                       @RequestAttribute("code") String code,
                       HttpServletRequest request){
        Map<String, Object> mp = new HashMap<>();
        Object msg1 = request.getAttribute("msg");
        mp.put("msg1",msg1);
        mp.put("msg",msg);
        return mp;
    }

在这里插入图片描述

@MatrixVariable、
用来使用矩阵变量的方法,可以在当cookies禁用的时候使用url拼接jsessionid来获取数据。

需要注意的是该模式一般情况下是被禁用的
需要自己在容器里添加一个组件WebMvcConfigurer,并设置它的urlPathHelper的RemoveSemicolonContent为false。

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer(){
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer){
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }

下面是两种情况

    // http://localhost:8888/cars/sell;low=20;brand=audi,byd
    @GetMapping("/cars/{path}")
    public Map<String,Object> carsSell(@MatrixVariable("low") Integer low,
                                       @MatrixVariable("brand") List<String> brand,
                                       @PathVariable("path") String path){


        Map<String,Object> mp = new HashMap<>();
        mp.put("low", low);
        mp.put("brand",brand);
        mp.put("path",path);
        return mp;
    }

    // 不同的path可以使用相同的value值
    // http://localhost:8888/boss/2;name=zshh/5;name=koyomi
    @GetMapping("/boss/{bossId}/{empId}")
    public Map<String,Object> boss(@MatrixVariable(value = "name", pathVar = "bossId") String empName,
                                   @MatrixVariable(value = "name", pathVar = "empId") String bossName){

        Map<String,Object> mp = new HashMap<>();
        mp.put("empName",empName);
        mp.put("bossName",bossName);
        return mp;
    }

第一种情况,单个属性多个值。
在这里插入图片描述第二种情况,在不同的path中设置请求参数
在这里插入图片描述

@ModelAttribute、
被@ModelAttribute注解的方法所在方法的controller内的所有方法在执行前都会被执行。因此对于一个controller映射多个URL的用法来说,要谨慎使用。

    @ModelAttribute
    public void accessname(@CookieValue("Idea-216775ce") Cookie cookie){

        System.out.println(cookie.getName());
    }

在该方法存在的Controller内的所有请求访问前都会输出
Idea-216775ce

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值