gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:43:47 25 4
gpt4 key购买 nike

我想传递 ResourceBundle 的 (globalmessage.properties) 属性名称“atm.number”而不是硬编码文本“Número de cajero”作为“errors”的参数.required”属性。我将如何在 validator 中执行此操作?

ReporteIncidenciaValidator( validator ):

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:

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

servlet-context.xml:

...

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

...

最佳答案

一个简单的方法是使用 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

}
}

关于java - 如何将参数传递给 errors.rejectValue() 这是 Spring MVC 3 中 ResourceBundle 的另一个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com