SpringMVC-获取Cookie

  • Post author:
  • Post category:其他



一、介绍


SpringMVC提供注解@CookieValue来获取Cookie,用法与注解里的属性跟@RequestParam一样。


二、使用

@Controller
public class MyController {

    @RequestMapping(value = {"/index"})
    public String index(@CookieValue(value = "username", required = false) String username, HttpServletResponse response){
        if (username == null){
            response.addCookie(new Cookie("username", "root"));
        }else {
            System.out.println(username);
        }
        return "index";
    }
}

在这里插入图片描述

在这里插入图片描述



版权声明:本文为qq_25243147原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。