@SessionAttributes 注解

    1. SessionAttributes 的作用 

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

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

@SessionAttributes({"a", "b"}) 保存多个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;
    } 
}



转载于:https://my.oschina.net/u/239075/blog/472283

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值