【问题标题】:Show validation errors on webpage with spring-mvc?使用 spring-mvc 在网页上显示验证错误?
【发布时间】:2018-01-18 20:12:09
【问题描述】:

以下控制器提供一个简单的 html 页面,显示存储库中的所有人员。

问题:我在 get-query 上使用了验证约束。如果查询无效(在我的示例中:lastname 参数丢失),那么 spring 会自动抛出异常作为对浏览器的响应。

但我仍然想渲染 persons.html 页面,只显示存储库内容之外的错误。

问题:我怎样才能做到这一点?因为如果验证失败,下面的方法甚至都无法访问。

@Controller
@RequestMapping("/persons")
public class PersonController {
    @GetMapping //note: GET, not POST
    public String persons(Model model, @Valid PersonForm form) {
        //on the persons.html page I want to show validation errors

        model.addAttribute("persons", dao.findAll());
        return "persons";
    }
}

public class PersonForm {
    private String firstname;

    @NotBlank
    private String lastname;
}

旁注:我使用thymeleaf 作为模板引擎。但同样的问题也适用于jspjsf 引擎。

【问题讨论】:

    标签: java spring jsp spring-mvc thymeleaf


    【解决方案1】:

    添加BindingResult 应该可以解决@obecker 指出的这个问题。我看到你的评论,它也适用于GetMapping@PostMapping

    请查看:

    @SpringBootApplication
    public class So45616063Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So45616063Application.class, args);
        }
    
        public static class PersonForm {
            private String firstname;
            @NotBlank
            private String lastname;
    
            public void setFirstname(String firstname) {
                this.firstname = firstname;
            }
    
            public void setLastname(String lastname) {
                this.lastname = lastname;
            }
    
            @Override
            public String toString() {
                return firstname + " " + lastname;
            }
        }
    
        @RestController
        @RequestMapping("/")
        public static class Home {
    
            @GetMapping
            public void get(@Valid PersonForm form, BindingResult bindingResult) {
                System.out.println(form);
                System.out.println(bindingResult);
            }
        }
    }
    

    呼叫:

    curl -XGET 'localhost:8080?firstname=f&lastname=l'
    

    将产生输出:

    f l
    org.springframework.validation.BeanPropertyBindingResult: 0 errors
    

    呼叫:

    curl -XGET 'localhost:8080?firstname=f'
    

    将产生:

    f null
    org.springframework.validation.BeanPropertyBindingResult: 1 errors
    

    【讨论】:

    • 感谢您添加示例。因此,我在尝试使用 BindingResult 时也会发现我的错误:必须在应该应用它的 @Valid 参数之后立即添加它。我不知道这一点(可能这在文档中也不够清楚)。
    【解决方案2】:

    您需要在persons 方法中添加一个额外的BindingResult bindingResult 参数。你可以使用这个bindingResult来查看是否有验证错误。

    Spring 有一个很好的指南,展示了如何做到这一点。 见https://spring.io/guides/gs/validating-form-input/

    【讨论】:

    • 这就是为什么我明确写了它是关于GET 请求的原因。您与 'BindingResult` 的链接示例仅适用于 @PostMapping!
    • 你能举个例子吗?如果我在上面的/persons 方法中添加BindingResult bindingResult 参数,则无法再访问它并返回404。
    • @membersound 我在回答中为您提供了一个示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2014-09-23
    • 2011-11-25
    • 2011-07-21
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    相关资源
    最近更新 更多