Spring mvc Spring MVC-无法@Autowire messageSource
我有一个自定义的异常类,需要从属性文件中读取一些错误消息。但它似乎一直在变为空。我甚至尝试添加@Component,但仍然不起作用Spring mvc Spring MVC-无法@Autowire messageSource,spring-mvc,autowired,Spring Mvc,Autowired,我有一个自定义的异常类,需要从属性文件中读取一些错误消息。但它似乎一直在变为空。我甚至尝试添加@Component,但仍然不起作用 @Component public class FormValidationFailExceptionHolder { @Autowired private MessageSource messageSource; ... public void someMethod(){ .... String
@Component
public class FormValidationFailExceptionHolder {
@Autowired
private MessageSource messageSource;
...
public void someMethod(){
....
String message = messageSource.getMessage(errorid, msgargs, Locale.ENGLISH);
....
}
}
这是我的spring配置
....
<context:component-scan base-package="simptex.my" />
....
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="useCodeAsDefaultMessage" value="false" />
<property name="basenames">
<list>
<value>classpath:mgmtlabels</value>
<value>classpath:error</value>
<value>classpath:PWMResources</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
据我所知,您真正想要实现的是自定义表单验证器
@Component
public class CustomValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return clazz == FormBean.class;
}
@Override
public void validate(Object target, Errors errors) {
FormBean bean = (FormBean) target;
if ( StringUtils.isEmpty( bean.getField() ) ) {
errors.rejectValue("field", "field.empty");
}
}
}
现在,您需要在控制器中添加:
@Autowired
private CustomValidator customValidator;
@InitBinder
protected void initBinder(WebDataBinder binder) {
if (binder.getTarget() instanceof FormBean) {
binder.setValidator(customValidator);
}
}
@RequestMapping(value = "/xxxx", method = RequestMethod.POST)
public String fooMethodxxx(@ModelAttribute("bean") @Validated FormBean bean, BindingResult result) {
if ( result.hasErrors() ) {
prepareModel(model);
return "form.view";
}
return "other.view"
}
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<f:errors cssClass="error" path="field"></f:errors>
<f:input path="field" type="text"/>
当您发布表单时,它将被验证。要在jsp中向用户显示验证消息,您需要添加:
@Autowired
private CustomValidator customValidator;
@InitBinder
protected void initBinder(WebDataBinder binder) {
if (binder.getTarget() instanceof FormBean) {
binder.setValidator(customValidator);
}
}
@RequestMapping(value = "/xxxx", method = RequestMethod.POST)
public String fooMethodxxx(@ModelAttribute("bean") @Validated FormBean bean, BindingResult result) {
if ( result.hasErrors() ) {
prepareModel(model);
return "form.view";
}
return "other.view"
}
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<f:errors cssClass="error" path="field"></f:errors>
<f:input path="field" type="text"/>
我认为这是处理自定义验证器的正确方法。谢谢。。我最终使用了“经典”的方式获得标签。拥有一个使用
ResourceBundleMessageSource
vincent的应用程序-您能否分享您是如何解决此问题的。我无法在自定义AuthenticationEntryPoint中自动连接MessageSource(ResourceBundleMessageSource)
@Component
public class CustomValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return clazz == FormBean.class;
}
@Override
public void validate(Object target, Errors errors) {
FormBean bean = (FormBean) target;
if ( StringUtils.isEmpty( bean.getField() ) ) {
errors.rejectValue("field", "field.empty");
}
}
}