一、@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")
@SessionAttributes("currUser") //①将ModelMap中属性名为currUser的属性
//放到Session属性列表中,以便这个属性可以跨请求访问
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); //②向ModelMap中添加一个属性
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
@SessionAttributes("currentUser")
public class GreetingController{
@RequestMapping
public void hello(@ModelAttribute("currentUser") User user){
//user.sayHello()
}
}
分享到:
相关推荐
通过ModelAndView、Map与Model、@SessionAttributes和@ModelAttribute注解来处理模型数据;其中,@ModelAttribute是重点,说明其运行流程并进行代码分析,最终给出SpringMVC目标方法的POJO类型参数的入参全过程,并...
• @ModelAttribute • @Cacheable • @CacheFlush • @Resource • @PostConstruct • @PreDestroy • @Repository • @Component (不推荐使用) • @Scope • @SessionAttributes • @InitBinder
模型数据类型 ...– @SessionAttributes: 将模型中的某个属性暂存到HttpSession 中,以便多个请求之间可以共享这个属性 – @ModelAttribute: 方法入参标注该注解后, 入参的对象就会放到数据模型中。
访问数据模型:@ModelAttribute 访问数据模型:Map及Model 访问数据模型:@SessionAttributes 一场由@SessionAttributes引发的血案... 如何避免@SessionAttributes引发的血案 目录 Spring MVC如何解析视图 视图解析...
访问数据模型:@ModelAttribute 访问数据模型:Map及Model 访问数据模型:@SessionAttributes 一场由@SessionAttributes引发的血案... 如何避免@SessionAttributes引发的血案 目录 Spring MVC如何解析视图 ...
样本SpringMVC Spring MVC表单-验证,数据绑定,验证Spring注释-@ RequestParam,@ PathVariable,@ ModelAttribute,@ SessionAttributes Spring安全Spring国际化Spring异常处理Spring基本REST服务用户-hsetpaing...
注解)、ModelAttribute注解如修饰POJO类型的入参、SessionAttributes注解引发的异常处理、JstlView、mvc_view-controller标签转换成注解、自定义视图、重定向、Employee管理RESTRUL_CRUD_显示所有 员工信息、...
本次实践内容包括RequestMapping关键字修饰类和方法(请求方式、请求参数&请求头、Ant风格路径)、PathVariable注解、HiddenHttpMethodFilter 过滤器(将Get请求转换成PUT、DELETE请求)、RequestParam 注解、...
SSM笔记-处理模型数据 处理模型数据之ModelAndView + 处理模型数据之ModelAttribute + 处理模型数据之 SessionAttributes
使用@ModelAttribute提供一个从模型到数据的链接 13.12.6. 使用@SessionAttributes指定存储在会话中的属性 13.12.7. 自定义WebDataBinder初始化 13.13. 更多资源 14. 集成视图技术 14.1. 简介 14.2. JSP和...
使用@ModelAttribute提供一个从模型到数据的链接 13.12.6. 使用@SessionAttributes指定存储在会话中的属性 13.12.7. 自定义WebDataBinder初始化 13.13. 更多资源 14. 集成视图技术 14.1. 简介 14.2. JSP和...
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
org.springframework.web.bind.annotation.SessionAttributes.class org.springframework.web.bind.support.ConfigurableWebBindingInitializer.class org.springframework.web.bind.support....