2015-10-05 53 views
0

我有一個頁面添加用戶「/ user/userAdd」。在GET中,我填充了一個國家列表。在POST中,我從formsubmit驗證用戶對象。如果它有錯誤,我會返回到msg錯誤的同一頁面。我的問題是我只是做一個簡單的返回「/ user/userAdd」;國家列表未填充。如果我做了返回「重定向:/ user/userAdd」;我失去了以前的用戶輸入。我該如何處理?驗證後的春季退貨

@RequestMapping(value = "/user/userAdd", method = RequestMethod.GET) 
public void getUserAdd(Model aaModel) { 
    aaModel.addAttribute("user", new User()); 

    List<Country> llistCountry = this.caService.findCountryAll(); 

    aaModel.addAttribute("countrys", llistCountry); 
} 

@RequestMapping(value = "/user/userAdd", method = RequestMethod.POST) 
public String postUserAdd(@ModelAttribute("user") @Valid User user, 
     BindingResult aaResult, SessionStatus aaStatus) { 
    if (aaResult.hasErrors()) { 

     return "/user/userAdd"; 
    } else { 
     user = this.caService.saveUser(user); 

     aaStatus.setComplete(); 
     return "redirect:/login"; 
    } 
} 

回答

2

我在我的春季項目中也遇到類似的問題。我會建議改變你的POST方法如下

@RequestMapping(value = "/user/userAdd", method = RequestMethod.POST) 
public String postUserAdd(@ModelAttribute("user") @Valid User user, 
     BindingResult aaResult, Model aaModel, SessionStatus aaStatus) { 
    if (aaResult.hasErrors()) { 
     List<Country> llistCountry = this.caService.findCountryAll(); 
     aaModel.addAttribute("countrys", llistCountry); 

     return "/user/userAdd"; 
    } else { 
     user = this.caService.saveUser(user); 

     aaStatus.setComplete(); 
     return "redirect:/login"; 
    } 
} 

這裏,名單再次添加到模型中,它也將在UI保持以前選擇的值(如果有的話)。

希望這會有所幫助