• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create a Validator using .properties file

 
Anupam Dee
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a Validator class e.g PersonValidator that implements Validator interface. I want to get the error messages stored in the .properties file (for Internalization).
I am trying to autowire MessageResource to do the same.



this is the xml content


and this is main

and the output is
null
[Field error in object 'person' on field 'name': rejected value [null]; codes [name.required.person.name,name.required.name,name.required.java.lang.String,name.required]; arguments []; default message [null]]

the first null is messageSource getting printed and it is null !
I think i am not able to autowire the messagesource to the .properties file. How should i do it?
Any help would be appreciated.

Thanks in advance.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MessageSource is null because it is not a Spring managed bean you are creating it using the new operator. Get it from the context instead.


Of course you will first have to make sure that the "personValidator" bean is defined in your context and you are wiring the messageSource into it. You could do this by adding an @Component to the PersonValidator class and adding a component scan or you could just wire it up manually.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic