SpringMVC @SessionAttributes注解

@SessionAttributes 注解只能作用到类上

@SessionAttributes(value={“user”},types={String.class})

 

@SessionAttribututes 除了可以通过属性名制定需要放到绘画中的属性外(实际上使用的是value属性值),
 还可以通过模型属性的对象类型制定哪些模型属性需要放到会话中(实际上是用的是types属性值)。

示例代码:

@Controller:

@Controller
@RequestMapping(“/springmvc”)
@SessionAttributes(value={“user”},types={String.class})
public class RequestMappingTest {

@RequestMapping(“/testSessionAttributes”)
public String testSessionAttributes(Map<String , Object> map){
User user = new User(“Tom”, 20, “123456”);
map.put(“user”, user);
map.put(“str”, “allen”);
return SUCCESS;
}

 

index.jsp:

<a href=”springmvc/testSessionAttributes”> test SessionAttributes</a>
<br><br>

 

success.jsp:

使用sessionScope获取SessionAttributes里面的值

<body>

user:${user }
<br><br>

request user:${requestScope.user }
<br><br>

session user:${sessionScope.user }
<br><br>

request str:${requestScope.str }
<br><br>

session str:${sessionScope.str }
</body>

posted on 2018-01-08 14:15 Allen_Zsj 阅读() 评论() 编辑 收藏

版权声明:本文为Allen-Zsj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Allen-Zsj/p/8242428.html