I have a web application making use of springs validation package. I noticed that if I don't specify a messageSource in spring 1.1.5 I get an error, even though I am setting a default error message using the argument of the ValidationUtils.rejectIfEmtpy method and example follows:
with 1.1.4 I didn't any the exceptions and everything worked as expected even without the bean specified, but now I get this:Code:ValidationUtils.rejectIfEmpty(errors, "password", PASSWORD_REQUIRED", "Password is required.");
After looking at the last method in DelegatingMessageSource:org.springframework.context.NoSuchMessageException : No message found under code 'PASSWORD_REQUIRED.userForm.userAccount.password' for locale 'en_US'.
at org.springframework.context.support.DelegatingMess ageSource.getMessage(DelegatingMessageSource.java: 73)
at org.springframework.context.support.DelegatingMess ageSource.getMessage(DelegatingMessageSource.java: 68)
at org.springframework.context.support.AbstractApplic ationContext.getMessage(AbstractApplicationContext .java:594)
at org.springframework.web.servlet.support.RequestCon text.getMessage(RequestContext.java:492)
at org.springframework.web.servlet.support.BindStatus .getErrorMessages(BindStatus.java:171)
at org.springframework.web.servlet.support.BindStatus .<init>(BindStatus.java:128)
at org.springframework.web.servlet.tags.BindStatus.<i nit>(BindStatus.java:38)
at org.springframework.web.servlet.tags.BindTag.doSta rtTagInternal(BindTag.java:105)
at org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag.java:7 0)
...
I noticed that it never attempts to get the defaultMessage from the resolvable object that is passed in if its parentMessageSource is null which it apparently is my case (I'm running in tomcat 5.0.28 with jdk.1.4.2 and it looks like the getInternalParentMessageSource() method in AbstractApplicationContext must be returning null on init). So I figure it might make sense to change the code to something like:Code:public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { if (this.parentMessageSource != null) { return this.parentMessageSource.getMessage(resolvable, locale); } else { String[] codes = resolvable.getCodes(); String code = (codes != null && codes.length > 0 ? codes[0] : null); throw new NoSuchMessageException(code, locale); } }
so that if a default messageSource is not specified, you can still at least get the default error message, if one exists.Code:public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { if (this.parentMessageSource != null) { return this.parentMessageSource.getMessage(resolvable, locale); } else { String msg = resolvable.getDefaultMessage(); if (StringUtils.hasText(msg)) { return msg; } else { String[] codes = resolvable.getCodes(); String code = (codes != null && codes.length > 0 ? codes[0] : null); throw new NoSuchMessageException(code, locale); } } }
I didn't see anything in release notes about messageSource now being required (It may always have been, and I've just been relying on an undocumented "feature"), but I thought I'd mention it in case anybody else has noticed the same or its something that is agreed makes sense to do.
Thanks.
![]()


Reply With Quote