java - Spring : message. 属性文件不起作用

标签 java xml spring validation

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

org.springframework.web.servlet.tags.RequestContextAwareTag doStartTag: No message found under code 'notmatch.password' for locale 'en_US'. org.springframework.context.NoSuchMessageException: No message found under code 'notmatch.password' for locale '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

最佳答案

确保您在 message_en_US.properties 文件中输入 notmatch.password key ,例如

notmatch.password = Inccorect password

并将基本名称更改为

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

有关更多信息,请查看此 link .

关于java - Spring : message. 属性文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12256945/

相关文章:

java - 扩展 JPanel 在添加到另一个 JPanel 时显示很小

java - 代码中的 SwitchPreference 和 CheckBoxPreference

html - R - 如何从 XML 节点集中提取项目?

java - 如何将经过身份验证的用户名保存到数据库中

java - Spring Controller 中的 JDBCTemplate

java - 表达式的类型必须是数组类型,但解析为Object

java - eclipse java 如何插入构建时间戳

java - 从 HttpServletRequest 获取 AsyncContext

html - 如何在不知道编码的情况下读取编码头?

java - 获取在 Spring 中实现通用接口(interface)的所有 bean