SpringMVC存取Session的两种方法

网友投稿 209 2023-06-19


SpringMVC存取Session的两种方法

WEB 应用通常会引入 Session,用来在服务端和客户端之间保存一系列动作/消息的状态,比如网上购物维护 user 登录信息直到 user 退出。SpringMVC存取Session的两种方法,具体如下:

方法一:使用servlet-api

@Controller

public class ManagerController {

@Resource

private ManagerService managerServiceImpl;

@RequestMapping(value = "manager/login.do",method = RequestMethod.GET)

public ModelAndView login(ManagerModel managerModel,HttpSession httpSession){

ManagerModel manager = managerServiceImpl.getManager(managerModel);

if(manager!=null){

manager.setPassword("");

httpSession.setAttribute("manager", manager);

return new ModelAndView(new RedirectView("../admin/main.jsp"));

}else{

return new ModelAndView(new RedirectView("../admin/login.jsp"));

}

}

@RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)

public String logout(HttpSession httpSession){

httpSession.getAttribute("manager");

return "success";

}

}

方法二:使用SessionAttributes

@Controller

@SessionAttributes("manager")

public class ManagerController {

@Resource

private ManagerService managerServiceImpl;

@RequestMapping(value = "manager/login.do",method = RequestMethod.GET)

public ModelAndView login(ManagerUtilyAoUiiModel managerModel,ModelMap model){

ManagerModel manager = managerServiceImpl.getManager(managerModel);

if(manager!=null){

manager.setPassword("");

model.addAttribute("manager", manager);

return new ModelAndView(new RedirectView("../admin/main.jsp"));

}else{

return new ModelAndView(new RedirectView("../admin/login.jsp"));

}

}

@RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)

public String logout(@ModelAttribute("manager")ManagerModel managerModel){

return "success";

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:深入理解Spring AOP
下一篇:java设计模式之外观模式(Facade)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~