验证后模型的Bean的map属性会丢失?

时间:2016-01-08 11:26:56

标签: spring-mvc

@RequestMapping(value =“/ Summary / addPage”,method = RequestMethod.GET)

public String getEmployeeAddPage(HttpServletRequest request, Model model) {

    logger.info("INSIDE getEmployeeAddPage");

    EmployeeForm empForm = empService.getEmployeeAddPageFormData();

    model.addAttribute("empForm", empForm);
    model.addAttribute("actionUrl", request.getRequestURL());

    return "/Employee/AddEmployee";

}


@RequestMapping(value = "/Summary/addPage", method = RequestMethod.POST)
public String employeeFormSubmit(HttpServletRequest request, Model model, @RequestParam("action") String action, @Valid @ModelAttribute("empForm") EmployeeForm empForm,
        BindingResult result) {

    try {

        empFormValidator.validate(empForm, result);

        model.addAttribute("empForm", empForm);
        model.addAttribute("actionUrl", request.getRequestURL());

        if (result.hasErrors()) {

            logger.info("has Errors");
            return "/Employee/AddEmployee";
        } else {
            String actionStatus = empService.saveEmployeeDetails(empForm);
            empForm.setActionStatus(actionStatus);
        }

    } catch (Exception e) {
        logger.error("Error", e);
        empForm.setActionStatus("error");
    }

    return "/Employee/AddEmployee";
}

当我提交页面时,empForm属性被提交给第二个方法,之后如果验证失败,它将返回到同一页面。但是这次用于JSP上的select选项的empForm中的map字段丢失了。 我该怎么办才能保留empForm中的地图字段?

1 个答案:

答案 0 :(得分:0)

我用过:

@SessionAttributes(value = {"empForm"}, types = {EmployeeForm.class})

在课程级别解决问题。