java 如何将参数传递给 errors.rejectValue(),它是 Spring MVC 3 中 ResourceBundle 的另一个属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11105627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:49:09  来源:igfitidea点击:

How to pass an argument to errors.rejectValue() which is another property of a ResourceBundle in Spring MVC 3?

javaspring-mvc

提问by Alfredo Osorio

I want to pass the ResourceBundle's (globalmessage.properties) property name "atm.number" instead of the hard-coded text "Número de cajero" as an argument of the "errors.required" property. How would I do that in a Validator?

我想传递 ResourceBundle 的 (globalmessage.properties) 属性名称“ atm.number”而不是硬编码文本“Número de cajero”作为“ errors.required”属性的参数。我将如何在验证器中做到这一点?

ReporteIncidenciaValidator (Validator):

ReporteIncidenciaValidator(验证器):

public class ReporteIncidenciaValidator implements Validator {

    public boolean supports(Class<?> clazz) {
        return ReporteIncidencia.class.isAssignableFrom(clazz);
    }

    public void validate(Object target, Errors errors) {
        ReporteIncidencia reporteIncidencia = (ReporteIncidencia) target;
        if (StringUtils.isBlank(reporteIncidencia.getNumeroCajero())) {
            errors.rejectValue("numeroCajero", "errors.required",
                new Object[]{"Número de cajero"}, "");
        }

    }
}

globalmessage.properties:

globalmessage.properties:

errors.required={0} es requerido.
atm.number=Número de cajero

servlet-context.xml:

servlet-context.xml:

...

...

<beans:bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename" value="classpath:globalmessages" />
</beans:bean>

...

...

回答by Rodrigo

An easy way is using the ApplicationContext class

一个简单的方法是使用 ApplicationContext 类

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

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

</beans>

public class ReporteIncidenciaValidator implements Validator, ApplicationContextAware  {
    private ApplicationContext ctx;


    public boolean supports(Class<?> clazz) {
        return ReporteIncidencia.class.isAssignableFrom(clazz);
    }

    public void validate(Object target, Errors errors) {
        String message = context.getMessage("yourKey", 
                new Object[] { }, Locale.US);
        // your code

    }
}