首页 > 其他 > 详细

@SessionAttributes 注解

时间:2015-06-30 01:30:19      阅读:293      评论:0      收藏:0      [点我收藏+]

    1. SessionAttributes 的作用 

   
一般而言ModelAndView中的属性作用域都是request级别,即本次请求结束,属性也随之销毁。
如果想要实现多个请求共享其某个属性,需将其保存至session

@SessionAttributes能自动捕获到当前controller中ModelAndView的指定属性,并转存为session


    2. 通过使用 @SessionAttributes("user") 快速得到user

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/info")
@SessionAttributes("user")  //要在方法中直接注解user必须要这一步
public class InfoController {
    private Logger logger = LoggerFactory.getLogger(InfoController.class);
    
    @RequestMapping("")
    public ModelAndView view(@ModelAttribute User user,) {
        ModelAndView modelView = new ModelAndView("/info/view");
        logger.info(user.toString());
        return modelView;
    } 
}



@SessionAttributes 注解

原文:http://my.oschina.net/u/239075/blog/472283

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!