简体   繁体   中英

Spring MVC <form:errors /> not showing error message from properties file

I was having some problem when trying to validate form in Spring MVC. Below as my controller class:

@RequestMapping(value = "/search.do", params ="doImageExtract", method = { RequestMethod.POST })
    public String doImageExtract(Model model, @ModelAttribute("attendanceTO") @Valid AttendanceTO attendanceSearchForm,
            BindingResult bindingResult) {
        
        log.debug("doImageExtractSearchList(): bindingResult has error " + bindingResult.hasErrors());
        if (bindingResult.hasErrors())
            return "search";
}

In my JSP:

<form:form action="search.do" method="POST" modelAttribute="attendanceTO">
<table>
<td align="left"><form:input path="dteEnlist" maxlength="10" /></td>
<td align="left"><form:errors path="dteEnlist" class="errorClass" /></td>
</table>
</form:form>

In my TO class:

@NotNull
@DateTimeFormat(pattern = "dd/MM/yyyy")
private String dteEnlist;

And in my properties file:

NotNull.attendanceTO.dteEnlist = Enlistment Date is required.
DateTimeFormat.attendanceTO.dteEnlist = Enlistment Date must be in dd/MM/yyy format.

I tried to check bindingResult.hasErrors() in the controller and it did returned true. However, the error message is not displaying in the JSP.

I have imported validation-api-2.0.1.Final.jar as library into my project.

Any ideas? Thanks!

Edit

<bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/properties/messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>

By default if you keep the validation messages in a ValidationMessages.properties it should be taken up automatically without any configurations by spring. Keep your properties file under src/main/resources .If you are keeping the messages in a different file like messages.properties and using different keys for your validation messages then you could do like:

@NotNull(message="{validation.date.notEmpty}")
@DateTimeFormat(pattern = "dd/MM/yyyy")
private String dteEnlist;

and also a configuration like when you place the messages in a messages.properties file:

messages.properties

validation.date.notEmpty =  Enlistment Date is required.

then provide the configuration to override default beans like:

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setValidationMessageSource(messageSource());
        return bean;
    }

    // other methods...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM