存储session方法
方法一:可以直接在方法的参数中引入,其中session也可以通过request获得
方法二:通过@SessionAttributes注解进行设置
@Controller
@RequestMapping(value="/spittles")
@SessionAttributes("name")
public class SpittlesController {
@RequestMapping(value="/model",method=RequestMethod.GET)
public String personInfo1(Model model,HttpSession httpSession,HttpServletRequest request){
String personList = "返回字符串";
httpSession.setAttribute("nickname","Frank");
HttpSession session = request.getSession();
session.setAttribute("age","14");
model.addAttribute("name",personList);
return "personList";
}
}
获取session方法
方法一:可以直接在方法的参数中引入,其中session也可以通过request获得
JAVA代码
@RequestMapping(value="/session",method={RequestMethod.GET,RequestMethod.POST})
public String getSession(HttpSession httpSession){
System.out.println(httpSession.getAttribute("name"));
return"registerForm";
}
方法二:使用 @Autowired 注入HttpSession,或HttpServletRequest来获取session
JAVA代码
@Autowired
HttpServletRequest request;
@RequestMapping(value="/session",method={RequestMethod.GET,RequestMethod.POST})
public String getSession(){
HttpSession httpSession = request.getSession();
System.out.println(httpSession.getAttribute("name"));
return"registerForm";
}