SpringMVC-基本注解讲解2

徘徊边缘 提交于 2020-02-06 03:45:32

@RequestBody

使用

在这里插入图片描述

案例

   public String testRequestBody(@RequestBody String Body){
        System.out.println(Body);
        return "success";
    }

@PathVariable

在这里插入图片描述

案例

    @RequestMapping(path = "/testPathVariable/{id}")
    public String testPathVariable(@PathVariable(value = "id") String id){
        System.out.println(id);
        return "success";
    }
<a href="/testRequestParam/testPathVariable/10">testRequestParam</a>

测试结果

在这里插入图片描述

@CookieValue

使用

在这里插入图片描述

案例

<%-- 测试@CookieValue --%>
<a href="/testRequestParam/testCookieValue">testCookieValue</a>
    @RequestMapping(path = "/testCookieValue")
    public String testCookieValue(@CookieValue(name = "JSESSIONID") String cookieValue){
        System.out.println(cookieValue);
        return "success";
    }

测试结果在这里插入图片描述

@SessionAttributes

作用

在Session对象中存储键值对数据,但是需要先存在Request对象中,再添加注解@SessionAttributes(name=“键名”)

案例

<%-- 测试SessionAttribute --%>
<a href="/testRequestParam/testAddSession">添加session</a>
<a href="/testRequestParam/testGetSession">获取session</a>
<a href="/testRequestParam/testDelSession">删除session</a>
    @RequestMapping(path = "/testAddSession")
    public String testAddSession(Model model){
       model.addAttribute("学习","SpringMVC");//添加键值对到Request对象中
        return "success";
    }
    @RequestMapping(path = "/testGetSession")
    public String testGetSession(ModelMap modelmap){
        System.out.println(modelmap.get("学习"));
        return "success";
    }
    @RequestMapping(path = "/testDelSession")
    public String testDelSession(SessionStatus status){
        status.setComplete();
        return "success";
    }

测试结果

在这里插入图片描述在这里插入图片描述
注意:使用SessionAttributes注解时,使用Model类的addAttribute()方法添加入Request对象中,使用ModelMap类的get()方法获取,并且@SessionAttributes注解只适用于类

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!