自定义弹簧验证错误 [英] Customize spring validation error

查看:72
本文介绍了自定义弹簧验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要自定义弹簧验证错误

I want customize the spring validation error for

    @NotNull
    @Length(max = 80)
    private String email; 

但是我做不到. 要采取的步骤是什么?

but I'm unable to do it. What are the step to follow?

推荐答案

The JSR 303 default message interpolation algorithm allows you to customize messages by supplying a resource bundle named ValidationMessages. Create a ValidationMessages.properties file in the classpath containing:

javax.validation.constraints.NotNull.message=CUSTOM NOT NULL MESSAGE
javax.validation.constraints.Size.message=CUSTOM SIZE MESSAGE

这会更改@Size约束的默认消息,因此您应该使用@Size约束,而不是特定于Hibernate的@Length约束.

This changes the default message for the @Size constraint, so you should use the @Size constraint instead of the Hibernate-specific @Length constraint.

您可以更改特定约束实例的消息,而不是更改所有约束的默认消息.在约束上设置message属性:

Instead of changing the default message for all constraints, you can change the message for a specific constraint instance. Set the message attribute on the constraint:

@NotNull(message = "{email.notnull}")
private String email;

并将消息添加到ValidationMessages.properties文件:

email.notnull=E-mail address is required

这篇关于自定义弹簧验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆