【问题标题】:Spring : message.properties file not workingSpring:message.properties 文件不起作用
【发布时间】:2012-08-28 17:27:07
【问题描述】:

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

org.springframework.web.servlet.tags.RequestContextAwareTag doStartTag:在区域设置的代码“notmatch.password”下找不到消息 'en_US'。 org.springframework.context.NoSuchMessageException:否 在语言环境“en_US”的代码“notmatch.password”下找到消息。

我的验证码是:

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>

【问题讨论】:

    标签: java xml spring validation


    【解决方案1】:

    确保您在message_en_US.properties 文件中拥有notmatch.password 密钥,例如

    notmatch.password = Inccorect password
    

    并将基本名称更改为

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

    更多信息请查看link

    【讨论】:

      【解决方案2】:

      在您的上下文中配置 PropertyPlaceholder:

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

      更多详情,请查看这篇文章。

      How to read values from properties file?

      【讨论】:

        猜你喜欢
        • 2018-04-24
        • 2019-02-02
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多