0

我正在使用 Tiles 3.0 和 Spring Validator。如何使用 .../edit/{userID} 之类的 ulr 编辑用户并出现一些验证错误,然后使用相同的 url 返回到该视图。我的问题是错误没有显示。我怎样才能做到这一点?对不起,我的英语不好!

这是我的代码:

@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();
}
4

1 回答 1

0

我发现了我的愚蠢错误。

当有验证错误时,只需返回到从用户接收输入的 Tile(视图),并在该视图中修改操作表单以匹配 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();
}
于 2014-11-25T09:31:17.180 回答