2016-01-08 27 views
0

@RequestMapping(值=「/概述/ addPage」,方法= RequestMethod.GET)驗證後模型Bean的map屬性會丟失嗎?

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選擇選項的empForm中的map字段丟失了。 我該怎麼做才能保留empForm中的地圖字段?

回答

0

我用:

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

在類級別來解決問題。