SPRING MVC:一种方法中定义的 HttpSession 对另一种方法不可用

SPRING MVC: Defined HttpSession in One Method is Not Available to Another Method

我在 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的controller)

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

在主页面的导航菜单中,我点击选择引导我添加经理网页。 (下图为link)。

<a href="addManager" target="ifrm" >Add New Account</a>

映射到link(GET)能够检测到的控制器。但是,当我尝试在 if 条件下使用 getAttribute 方法访问时,此控制器 (renderAddManagerView) 无法识别我之前定义的 HTTP 会话。一直显示空值(如下图所示。

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

所以我不确定我的代码有什么问题,也没有显示任何错误消息。

我已经通过使用 HttpServletRequest 而不是 HttpSession 解决了这个问题。 现在我的会话不会丢失甚至重定向或导航到 JSP.

中的任何页面

像这样:

@RequestMapping("/renderview", method = RequestMethod.GET)
@Controller
public class TestController {
    @RequestMapping(method = RequestMethod.GET)
    public String myMethod(HttpServletRequest request)
    {
        request.getSession().setAttribute("mySession", "XXX");
        return "jspview";
    }
}

参考:Set session variable spring mvc 3