简体   繁体   English

Hibernate @Valid在SpringMVC中的奇怪行为

[英]Hibernate @Valid strange behaivor in SpringMVC

First, I'm very new to this, sorry if this question is dumb. 首先,我对此很陌生,如果这个问题很愚蠢,对不起。 I have two controllers with class request mappings and form to validate: 我有两个具有类请求映射和表单的控制器来验证:

@Controller
@RequestMapping("/creditcard")
public class CreditCardController {
    @Autowired
    //repositories

    @RequestMapping(value = "/addnewcard", method = RequestMethod.POST)
    public String addNew(
             @ModelAttribute("newCard") @Valid CreditCard creditCard,
            BindingResult bindingResult, Principal principal, Model model) {
        if (bindingResult.hasErrors()) {
            //render error view
        }
        creditCardService.registerNew(creditCard, principal.getName());
        return "redirect:/account";
    }

Another 另一个

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    //repo

    @RequestMapping("")
    public String showUserProfile(Principal principal, Model model) {
        String username = principal.getName();
        User user = userService.findByUsername(username);
        Account account = user.getAccount();
        List<Payment> payers = paymentService
                .getAllPaymentsForPayerAccount(account.getId());
        model.addAttribute("user", user);
        model.addAttribute("account", account);
        model.addAttribute("payers", payers);
        return "userprofile";
    }
}

Form on userprofile.jsp 在userprofile.jsp上形成表格

<form:form cssClass="form-horizontal" modelAttribute="newCard" action="creditcard/addnewcard">
........
</form:form>

And all this works without @Valid . 而所有这些都不需要@Valid When I add @Valid it works fine, when validation fails ( shows error view with messages), but when it succeeds I get 404 error due to incorrect URI - http://localhost:8080/PaymentSystem/creditcard/creditcard/addnewcard . 当我添加@Valid它可以正常工作,当验证失败时(显示带有消息的错误视图),但是当它成功时,由于URI不正确,我会收到404错误http://localhost:8080/PaymentSystem/creditcard/creditcard/addnewcard Here one /creditcard is extra, @Valid somehow adds this to my URI. 这里一个/creditcard是多余的,@ @Valid以某种方式将其添加到我的URI中。 I found to ways for me to solve this: 1) I moved addNew card method to 'AccountController' 我找到了解决此问题的方法:1)我将addNew卡方法移至“ AccountController”

2) I just removed @RequestMapping("/creditcard") 2)我刚刚删除了@RequestMapping("/creditcard")

But still I do not found any explanation of such behaviour. 但是我仍然没有找到任何关于这种行为的解释。 Any idea? 任何想法?

I found 3rd way to solve this issue, most suitable in this situation. 我找到了解决此问题的第三种方法,最适合这种情况。 I just added ${pageContext.request.contextPath} to my action param like this: 我只是将${pageContext.request.contextPath}添加到我的操作参数中,如下所示:

<form:form cssClass="form-horizontal" modelAttribute="newCard" action="${pageContext.request.contextPath}/creditcard/addnewcard">
........
</form:form>

But still it don't know the reason of that kind of @Valid behaviour. 但它仍然不知道这种@Valid行为的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM