1. SessionAttributes 的做用 java
通常而言ModelAndView中的屬性做用域都是request級別,即本次請求結束,屬性也隨之銷燬。 若是想要實現多個請求共享其某個屬性,需將其保存至session @SessionAttributes能自動捕獲到當前controller中ModelAndView的指定屬性,並轉存爲session @SessionAttributes({"a", "b"}) 保存多個session
2. 經過使用 @SessionAttributes("user") 快速獲得userweb
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; } }