spring mvc@SessionAttributes

https://www.cnblogs.com/caoyc/p/5635914.html

@SessionAttributes原理:

默认情况下,spring mvc将模型中的数据存储到request域中。

当一个请求结束后,数据就失效了。

如果要跨页面使用。那么需要使用session。

而@SessionAttributes注解就可以使得模型中的数据存储一份到session域中。

@SessionAttributes参数:

1、names:这是一个字符串数组。里面应写需要存储到session中数据的名称;

2、types:根据指定参数的类型,将模型中对应类型的参数存储到session中;

3、value:其实和names是一样的。

具体代码:

java代码:

@RequestMapping("/test")
public String test(Map<String,Object> map){
    map.put("names", Arrays.asList("caoyc","zhh","cjx"));
    map.put("age", 18);
    return "hello";
}

jsp页面:

1、request中names:${requestScope.names}<br/>
2、request中age:${requestScope.age}<br/>
<hr/>
3、session中names:${sessionScope.names }<br/>
4、session中age:${sessionScope.age }<br/>

显示结果:

总结:

上面代码没有指定@SessionAttributes,所以在session域总无法获取到对应的数据。

下面我们加上@SessionAttributes注解:

@SessionAttributes(value={"names"},types={Integer.class})
@Controller
public class Test {

    @RequestMapping("/test")
    public String test(Map<String,Object> map){
        map.put("names", Arrays.asList("caoyc","zhh","cjx"));
        map.put("age", 18);
        return "hello";
    }
}

再次访问:

可以看到session域中值已经存在。

注意:

@SessionAttributes注解只能在类上使用,不能在方法上使用。

原文地址:https://www.cnblogs.com/arrows/p/10524771.html