1. 程式人生 > >springMVC 中獲取session方法

springMVC 中獲取session方法

儲存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"; }