Announcement Announcement Module
Collapse
No announcement yet.
messageSource definition when migrating from 1.1.4 to 1.1.5 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • messageSource definition when migrating from 1.1.4 to 1.1.5

    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:

    Code:
    ValidationUtils.rejectIfEmpty(errors, "password", PASSWORD_REQUIRED", "Password is required.");
    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:

    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)
    ...
    After looking at the last method in DelegatingMessageSource:

    Code:
    	public String getMessage&#40;MessageSourceResolvable resolvable, Locale locale&#41; throws NoSuchMessageException &#123;
    		if &#40;this.parentMessageSource != null&#41; &#123;
    			return this.parentMessageSource.getMessage&#40;resolvable, locale&#41;;
    		&#125;
    		else &#123;
    			String&#91;&#93; codes = resolvable.getCodes&#40;&#41;;
    			String code = &#40;codes != null && codes.length > 0 ? codes&#91;0&#93; &#58; null&#41;;
    			throw new NoSuchMessageException&#40;code, locale&#41;;
    		&#125;
    	&#125;
    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&#40;MessageSourceResolvable resolvable, Locale locale&#41; throws NoSuchMessageException &#123;
    		if &#40;this.parentMessageSource != null&#41; &#123;
    			return this.parentMessageSource.getMessage&#40;resolvable, locale&#41;;
    		&#125;
    		else &#123;
             String msg = resolvable.getDefaultMessage&#40;&#41;;
             if &#40;StringUtils.hasText&#40;msg&#41;&#41; &#123;
                return msg;
             &#125; else &#123;
                String&#91;&#93; codes = resolvable.getCodes&#40;&#41;;
                String code = &#40;codes != null && codes.length > 0 ? codes&#91;0&#93; &#58; null&#41;;
                throw new NoSuchMessageException&#40;code, locale&#41;;
             &#125;
    		&#125;
    	&#125;
    so that if a default messageSource is not specified, you can still at least get the default error message, if one exists.

    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.


  • #2
    in case you haven't seen this thread yet....

    I just did a one liner fix in my xml, as mentioned by one of the posters in this thread:

    Code:
    <bean id="messageSource" class="org.springframework.context.support.StaticMessageSource" />

    Comment

    Working...
    X