一、@ModelAttribute
在默认情况下,ModelMap 中的属性作用域是 request 级别是,也就是说,当本次请求结束后,ModelMap中的属性将销毁。如果希望在多个请求中共享 ModelMap 中的属性,必须将其属性转存到 session 中,这样ModelMap 的属性才可以被跨请求访问。
Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。
使模型对象的特定属性具有 Session 范围的作用域
- package com.baobaotao.web;
- …
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.SessionAttributes;
- @Controller
- @RequestMapping("/bbtForum.do")
- <span style="color: #008000;">@SessionAttributes("currUser") //①将ModelMap中属性名为currUser的属性
- //放到Session属性列表中,以便这个属性可以跨请求访问</span>
- public class BbtForumController {
- …
- @RequestMapping(params = "method=listBoardTopic")
- public String listBoardTopic(@RequestParam("id")int topicId, User user,
- ModelMap model) {
- bbtForumService.getBoardTopics(topicId);
- System.out.println("topicId:" + topicId);
- System.out.println("user:" + user);
- model.addAttribute("currUser",user); <span style="color: #008000;">//②向ModelMap中添加一个属性</span>
- return "listTopic";
- }
- }
我们在 ② 处添加了一个 ModelMap 属性,其属性名为 currUser,而 ① 处通过 @SessionAttributes 注解将 ModelMap 中名为 currUser 的属性放置到 Session 中,所以我们不但可以在 listBoardTopic() 请求所对应的 JSP 视图页面中通过 request.getAttribute(“currUser”) 和 session.getAttribute(“currUser”) 获取 user 对象,还可以在下一个请求所对应的 JSP 视图页面中通过 session.getAttribute(“currUser”) 或ModelMap#get(“currUser”) 访问到这个属性。
这里我们仅将一个 ModelMap 的属性放入 Session 中,其实 @SessionAttributes 允许指定多个属性。你可以通过字符串数组的方式指定多个属性,如 @SessionAttributes({“attr1”,”attr2”})。此外,@SessionAttributes 还可以通过属性类型指定要 session 化的 ModelMap 属性,如@SessionAttributes(types = User.class),当然也可以指定多个类,如 @SessionAttributes(types = {User.class,Dept.class}),还可以联合使用属性名和属性类型指定:@SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”})。
二、@ModelAttribute
我们可以在需要访问 Session 属性的 controller 上加上 @SessionAttributes,然后在 action 需要的 User 参数上加上 @ModelAttribute,并保证两者的属性名称一致。SpringMVC 就会自动将 @SessionAttributes 定义的属性注入到 ModelMap 对象,在 setup action 的参数列表时,去 ModelMap 中取到这样的对象,再添加到参数列表。只要我们不去调用 SessionStatus 的 setComplete() 方法,这个对象就会一直保留在 Session 中,从而实现 Session 信息的共享。
- @Controller
- <span style="color: #008000;">@SessionAttributes("currentUser")</span>
- public class GreetingController{
- @RequestMapping
- public void hello<span style="color: #008000;">(@ModelAttribute("currentUser")</span> User user){
- //user.sayHello()
- }
- }
相关推荐
主要介绍了spring学习之@SessionAttributes实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:...
主要介绍了详解Spring 注解之@Import 注入的各种花活,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Spring注解@Component、@Repository、@Service、@Controller区别.doc
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
主要介绍了Spring注解之@Lazy注解使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。
主要介绍了浅谈spring注解之@profile,@profile通过配置来改变参数,这里整理的详细的用法,有兴趣的可以了解一下
Spring注解@Resource和@Autowired.doc
主要介绍了spring 注解验证@NotNull等使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
@SessionAttributes原理 默认情况下Spring MVC将模型中的数据存储到request域中。当一个请求结束后,数据就失效了。如果要跨页面使用。...而@SessionAttributes注解就可以使得模型中的数据存储一份到session域中。
SPring注解及页面跳转实例
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
Spring框架中 @Autowired 和 @Resource 注解的区别 在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。 首先,咱们简单了解 @...
主要介绍了详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用,非常具有实用价值,需要的朋友可以参考下
spring-demo13-注解-@Value.zip
Spring注解
spring 支持@RequestBody注解依赖包 、 spring 支持@RequestBody注解依赖包 spring 支持@RequestBody注解依赖包
本篇文章主要介绍最重要的三个Spring注解,也就是@Autowired、@Resource和@Service,具有很好的参考价值。下面跟着小编一起来看下吧