Spring:message.properties文件无效

时间:2012-09-04 04:47:08

标签: java xml spring validation

在我的应用验证过程中,我使用message.properties文件来显示自定义消息。但它无法正常工作,并在App Engine服务器日志中显示以下错误

  

org.springframework.web.servlet.tags.RequestContextAwareTag doStartTag:在代码'notmatch.password'下找不到有关区域设置的消息   'EN_US'。 org.springframework.context.NoSuchMessageException:没有   在代码'notmatch.password'下找到的有关区域设置'en_US'的消息。

我的验证代码是:

public void validate(Object target, Errors errors) {

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
            "required.password", "Field name is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
            "required.confirmPassword", "Field name is required.");

    NewUser cust = (NewUser) target;

    if (!(cust.getPassword().equals(cust.getConfirmPassword()))) {
        errors.rejectValue("password", "notmatch.password");
    }

}

我的配置xml是:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <beans:bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <beans:property name="basename"
            value="/WEB-INF/resources/message_en_US.properties" />
    </beans:bean>



        <beans:bean name="/register" class="com.my.registration.NewUserRegistration">
        <beans:property name="validator">
            <beans:bean class="com.my.validation.UserValidator" />
        </beans:property>
        <beans:property name="formView" value="newuser"></beans:property>
        <beans:property name="successView" value="home"></beans:property>
    </beans:bean>

My project file structure is given in the image

2 个答案:

答案 0 :(得分:4)

确保notmatch.password文件中有message_en_US.properties个密钥,如

notmatch.password = Inccorect password

并将基本名称更改为

<beans:property name="basename"
            value="/WEB-INF/resources/message" />  

有关详细信息,请查看此link

答案 1 :(得分:1)

在您的上下文中配置PropertyPlaceholder:

<context:property-placeholder locations="classpath*:my.properties"/>

有关详细信息,请查看此文章。

How to read values from properties file?