在Spring中没有从消息属性文件中获取验证消息

Jul*_*ius 6 spring message bundle bean-validation

我昨天工作了,然后我做了一些事情,现在我一直在努力修复它几个小时,我再也无法让它工作了.

我有一个Spring MVC应用程序包含一个<form:form>我想<form:errors>在用户键入错误信息时从.properties文件显示自定义错误消息().在JSR-303注释中定义了什么'错误'.

从表格中摘录:

<form:form method="post" action="adduserprofile" modelAttribute="bindableUserProfile">
<table>
    <tr>
        <td><form:label path="firstName">Voornaam</form:label></td>
        <td>
            <form:input path="firstName"/>
            <form:errors path="firstName" />
        </td>
    </tr>
    <tr>
        <td><form:label path="lastName">Achternaam</form:label></td>
        <td>
            <form:input path="lastName"/>
            <form:errors path="lastName" />
        </td>
    </tr>
Run Code Online (Sandbox Code Playgroud)

摘自BindableUserProfile:

   @NotNull
@Size(min = 3, max = 40, message="{errors.requiredfield}")
public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@NotNull
@Size(min = 3, max = 40,  message="errors.requiredfield")
public String getLastName() {
    return lastName;
}
Run Code Online (Sandbox Code Playgroud)

摘录自控制器:

    @RequestMapping(value = "/edit/{userProfileId}", method = RequestMethod.GET)
public String createOrUpdate(@PathVariable Long userProfileId, Model model) {
    if (model.containsAttribute("bindableUserProfile")) {
        model.addAttribute("userProfile", model.asMap().get("bindableUserProfile"));
    } else {
        UserProfile profile = userProfileService.findById(userProfileId);
        if (profile != null) {
            model.addAttribute(new BindableUserProfile(profile));
        } else {
            model.addAttribute(new BindableUserProfile());
        }
    }

    model.addAttribute("includeFile", "forms/userprofileform.jsp");
    return "main";
}

@RequestMapping(value = "/adduserprofile", method = RequestMethod.POST)
public String addUserProfile(@Valid BindableUserProfile userProfile, BindingResult result, Model model) {
    if (result.hasErrors()) {
        return createOrUpdate(null, model);
    }

    UserProfile profile = userProfile.asUserProfile();
    userProfileService.addUserProfile(profile);
    return "redirect:/userprofile";
}
Run Code Online (Sandbox Code Playgroud)

摘自application-context.xml

   <bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages/messages"/>
</bean>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource">
        <ref bean="messageSource"/>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

在资源/消息中,我有两个文件,messages_en.properties和messages_nl.properties.两者都有相同,简单的内容:

errors.requiredfield=This field is required!!!
Run Code Online (Sandbox Code Playgroud)
  • 当我提交带有空名字的表单时,我可以在控制器方法'addUserProfile()'中看到确实找到了错误.
  • 当我使用空名字提交表单时,字段旁边会显示消息标识符,即姓氏旁边的文字文本"errors.requiredfield"或"{errors.requiredfield}".
  • 当我将消息属性值更改为"Foo"时,"Foo"显示为错误消息.所以错误机制本身似乎工作正常.
  • application-context.xml中的messageSource bean必须正确,因为它说我在更改basename时找不到属性文件.
  • NotNull注释未捕获空输入.Spring将空输入视为空字符串而不是null.

因此,似乎找到属性文件并正确处理验证注释,但Spring不明白它必须用属性文件中的消息替换消息密钥.

Jul*_*ius 3

是的,我认为这从一开始就不应该起作用。

我认为可以将 JSR-303 注释的“message”属性解释为键,以便从 message.properties 文件获取关联的错误消息,但我认为我错了。

@Size(min = 3, max = 40, message="errors.requiredfield")

我的同事在工作中编写了一个层来为我们创建这种行为,但默认情况下它不起作用。好像我曾经工作过一次,因为我正在使用

@Size(min = 3, max = 40, message="{errors.requiredfield}")

大括号导致 Spring 启动使用 .properties 文件作为源的查找和替换过程。不过,第二个选项仍然有效。