2

I'm trying to implement form validation with Spring Validator, following these tutorials from dzone and mkyong. It seems the Validator method validate is called upon form submission, errors are thrown, but the message codes aren't the same that I specified.
I have a bean named Song with an attribute name title In my Validator, I have ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "title.required");

yet, I got this error message
org.springframework.context.NoSuchMessageException: No message found under code 'title.required.song.title' for locale 'en_US'.

it searches the message under title.required.song.title instead of title.required, and I guess under a file name messages_en_US.properties

How can I change this behaviour?

My messages.properties placed under /src/ folder has
title.required=Ce champ est obligatoire

@Entity
public class Song {

@Id
@GeneratedValue
private Long id;

private String title;
//and other stuff
Gers
  • 572
  • 1
  • 4
  • 15

1 Answers1

1

add this to your configuration xml :

<bean id="messageSource"
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
     p:basename="classpath:messages" />

or using config java add this line

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

best regards

hicham abdedaime
  • 346
  • 2
  • 15
  • Actually, in my configuration xml, I had **ResourceBundleMessageSource** as in the tutorials instead of **ReloadableResourceBundleMessageSource**. the class change solved the issue. thanks (by the way, I still struggle to understand the difference in practice) – Gers Mar 14 '17 at 20:03
  • check this url explain what is the difference http://stackoverflow.com/questions/39685399/reloadableresourcebundlemessagesource-vs-resourcebundlemessagesource-cache-con – hicham abdedaime Mar 15 '17 at 08:34