我在Spring MVC项目中实现了与Httpsession有关的问题。
首先,在用户成功登录后,我将在loginAuthentication控制器中使用Httpsession对象,并使用所需的名称和值设置属性。 (如下图所示。)>
A.java控制器文件,
@RequestMapping(value="login-authentication", method = RequestMethod.POST) public String authentication(@Valid @ModelAttribute("systemAccount") SystemAccount systemAccount, BindingResult bindingResult, Model model, HttpServletRequest request){ if (bindingResult.hasErrors()) { model.addAttribute(GenericConstant.MessageAttributeName.ERROR_MSG_NAME.toValue(), SystemMessage.SystemException.LOGIN_INCORRECT_USERNAME_PASSWORD.toValue()); model.addAttribute("systemAccount", new SystemAccount()); return "index"; }else { if (systemAccountService.authenticate(systemAccount.getUsername(), systemAccount.getPassword()) != null && !"".equals(systemAccountService.authenticate(systemAccount.getUsername(), systemAccount.getPassword()))) { SystemAccount dbSystemAccount = systemAccountService.authenticate(systemAccount.getUsername(), systemAccount.getPassword()); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ID.toValue(),dbSystemAccount.getAccountID()); //check account role if(dbSystemAccount.getCounterStaff()!= null && !"".equals(dbSystemAccount.getCounterStaff())){ CounterStaff counterStaff = dbSystemAccount.getCounterStaff(); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), counterStaff.getStaffName()); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), GenericConstant.SystemRole.COUNTER_STAFF.toValue()); }else if(dbSystemAccount.getCustomer()!= null && !"".equals(dbSystemAccount.getCustomer())){ Customer customer = dbSystemAccount.getCustomer(); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), customer.getCustomerName()); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), GenericConstant.SystemRole.CUSTOMER.toValue()); }else if(dbSystemAccount.getManager()!= null && !"".equals(dbSystemAccount.getManager())){ Manager manager = dbSystemAccount.getManager(); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), manager.getManagerName()); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), GenericConstant.SystemRole.MANAGER.toValue()); }else if(dbSystemAccount.getDoctor()!= null && !"".equals(dbSystemAccount.getCounterStaff())){ Doctor doctor = dbSystemAccount.getDoctor(); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), doctor.getDoctorName()); request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), GenericConstant.SystemRole.DOCTOR.toValue()); } request.setAttribute(SessionAttribute.AttributeName.LOGIN_DATE.toValue(), DateTimeUtil.getCurrentDate()); return "mainPage"; }else { model.addAttribute(GenericConstant.MessageAttributeName.ERROR_MSG_NAME.toValue(), SystemMessage.SystemException.LOGIN_INCORRECT_USERNAME_PASSWORD); model.addAttribute("systemAccount", new SystemAccount()); return "index"; } } }
一切准备就绪后,控制器会将用户导航到主页,并且主页能够无问题地访问所有定义的变量。 (下图显示了与mainPage映射的控制器)。
A.java控制器文件,
@RequestMapping(value = "/mainPage", method = RequestMethod.GET) public String renderMainPageView(Model model, HttpServletRequest request) { if(request.getAttribute(SessionAttribute.AttributeName.LOGIN_CHECK.toValue()) != null) { model.addAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ID.toValue(), request.getAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ID.toValue())); model.addAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), request.getAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue())); model.addAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), request.getAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue())); model.addAttribute(SessionAttribute.AttributeName.LOGIN_DATE.toValue(), request.getAttribute(SessionAttribute.AttributeName.LOGIN_DATE.toValue())); return "mainPage"; }else { model.addAttribute("systemAccount", new SystemAccount()); return "index"; } }
在主页的导航菜单中,我单击选择以指示我添加经理网页。 (以下显示了链接)。
方法访问时,此控制器(renderAddManagerView)无法识别我先前定义的HTTP会话。它一直显示空值(如下图所示。<a href="addManager" target="ifrm" >Add New Account</a>
与链接(GET)映射的控制器能够检测到。但是,当我尝试在if条件下使用getAttribute
B.java控制器文件,
] >>@RequestMapping(value = "/addManager", method = RequestMethod.GET) public String renderAddManagerView(Model model, HttpSession httpSession) { if(httpSession.getAttribute(SessionAttribute.AttributeName.LOGIN_CHECK.toValue()) != null) { model.addAttribute("manager", new Manager()); model.addAttribute(FormSelectionValue.FormSelectionAttributeName.COUNTRY_SELECTION.toValue(), FormSelectionValue.COUNTRY_SELECTION_LIST); model.addAttribute(FormSelectionValue.FormSelectionAttributeName.GENDER_SELECTION.toValue(), FormSelectionValue.GENDER_SELECTION_LIST); return "addManager"; }else { model.addAttribute("systemAccount", new SystemAccount()); return "index"; } }
所以我不确定我的代码有什么问题,并且不会显示错误消息。
我在Spring MVC项目中实现了有关Httpsession的问题。首先,在用户成功登录后,我将在...