gpt4 book ai didi

Spring 4 Hibernate 验证器本地化消息

转载 作者:行者123 更新时间:2023-12-02 02:15:46 26 4
gpt4 key购买 nike

我尝试使用自定义休眠消息进行本地化,但无法使其工作。我的经过验证的类如下所示:

@Entity
@Table(name = "USERS")
public class UserEntity extends AbstractBaseEntity implements UserDetails {

@NotNull
@Column(unique = true, nullable = false)
@Size(min = 5, max = 30)
private String username;

这是我的 spring 配置的一部分:

<!-- Localization of hibernate messages during validation!-->
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validation" />
</bean>

<bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="validationMessageSource"/>
</property>
</bean>

在资源中,我有两个验证文件:validation_en.properties 和validation_pl.properties 这是示例条目:

NotNull.UserEntity.username=Username can't be empty!

当我显示验证错误时,我看到标准消息“可能不为空”,而不是我的自定义和本地化消息。我做错了什么?预先感谢您的帮助,最诚挚的问候

最佳答案

您需要做的是@NotNull(message = "{error.username.required}")

@Entity
@Table(name = "USERS")
public class UserEntity extends AbstractBaseEntity implements UserDetails {

@NotNull(message = "{error.username.required}")
@Column(unique = true, nullable = false)
@Size(min = 5, max = 30)
private String username;

在资源文件夹中的 validation_en.properties 属性文件中

error.username.required=Username can't be empty!

在 Spring 配置中:

<mvc:annotation-driven validator="validator">
</mvc:annotation-driven>

<!-- Localization of hibernate messages during validation!-->
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validation" />
</bean>

<bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="validationMessageSource"/>
</property>
</bean>

如果这对您不起作用,也请发表评论。这样我可以进一步帮助您。

关于Spring 4 Hibernate 验证器本地化消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com