简体   繁体   English

春季表单验证行动 5

[英]form validation in spring in action 5

I am following the source code of spring in action 5 on github, and I code my own version.我在 github 上关注spring in action 5的源代码,我编写了自己的版本。 When I leave the name blank and commit, I get a wrong page like that当我将名称留空并提交时,我得到了这样的错误页面

在此处输入图片说明

and the console output is: Field error in object 'taco' on field 'name': rejected value [];并且控制台输出是: Field error in object 'taco' on field 'name': rejected value [];

But the correct page is like that:但正确的页面是这样的: taco 设计页面,名称文本框周围带有警告消息

here is my code:这是我的代码:

design.html设计.html

 <label>give it a name:</label>

 <input type="text" th:field="*{name}">

  <span class="validationError"
      th:if="${#fields.hasErrors('name')}"
      th:errors="*{name}">Name Error</span>

designController.java设计控制器.java

    @PostMapping
    public String postTaco(@Valid Taco taco, Order order, Errors errors) {
        if (errors.hasErrors()){
            return "design";
        }

        Taco taco1 = tacoRepo.save(taco);
        order.addDesign(taco1);
        return "order";
    }


Taco.java玉米卷

@Size(min=5, message = "at least 5 characters")
private String name;

Where is the problem问题出在哪儿

designContrller.java设计控制器.java

    @PostMapping
    public String postTaco(@Valid Taco taco, Order order, Errors errors) {
        if (errors.hasErrors()){
            return "design";
        }

        Taco taco1 = tacoRepo.save(taco);
        order.addDesign(taco1);
        return "order";
    }

solution:解决方案:

    @PostMapping
    public String postTaco(@Valid Taco taco, Errors errors, Order order) {
        if (errors.hasErrors()){
            return "design";
        }

        Taco taco1 = tacoRepo.save(taco);
        order.addDesign(taco1);
        return "order";
    }

conclusiong:结论g:

The parameter Erroes errors must behide the valid object parameter, here is Taco taco参数Erroes errors必须隐藏有效的对象参数,这里是Taco taco

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

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