2014-11-25 39 views
0

我正在使用Tiles 3.0和Spring Validator。 如何編輯ulr這樣的用戶,比如.../edit/{userID},並且有一些驗證錯誤,然後用相同的url返回到該視圖。我的問題是錯誤沒有顯示。 我該怎麼做? 對不起,我的英語不好!彈出式窗口+ validtion有錯誤時驗證

這裏是我的代碼:

@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) 
public String getEditUser(@PathVariable int id, Model model) { 

    User user = userService.getUserById(id); 
    model.addAttribute("user", user); 
    return "user_edit"; 
} 

@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST) 
public String editUser(@PathVariable int id, 
     @Validated(GroupEdit.class) @ModelAttribute User user, 
     BindingResult result, Model model) { 

    if (result.hasErrors()){ 
     return "redirect:edit/" + user.getUserId(); //how can i return here? 
    } 
    userService.updateUser(user); 
    return "redirect:edit/" + user.getUserId(); 
} 

回答

0

我發現我的愚蠢的錯誤。

只要返回平鋪(視圖),在用戶收到輸入時驗證錯誤,並在該視圖中修改操作表單以匹配POST方法。瀏覽器的URL將是../user/{userId}。

筆者認爲:

<form:form action="../edit/${user.userId}" modelAttribute="user" method="post" 

我的控制器:

@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) 
public String getEditUser(@PathVariable int id, Model model) { 

    User user = userService.getUserById(id); 
    model.addAttribute("user", user); 
    return "user_edit"; //this is my Tiles definition in tiles.xml where user input data 
} 

@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST) 
public String editUser(@PathVariable int id, 
     @Validated(GroupEdit.class) @ModelAttribute User user, 
     BindingResult result, Model model) { 

    if (result.hasErrors()) { 
     return "user_edit"; //return to the Tiles in tiles.xml, it will show error. 
    } 
    userService.updateUser(user); 
    return "redirect:../edit/" + user.getUserId(); 
}