Results 1 to 2 of 2

Thread: messageSource definition when migrating from 1.1.4 to 1.1.5

  1. #1
    Join Date
    Mar 2005
    Posts
    1

    Default 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. #2
    Join Date
    Feb 2005
    Location
    Lahaina, HI
    Posts
    8

    Default 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" />
    \"In the past, I was the future.\"
    - Tes

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. UnknownAdviceTypeException
    By dbendlin in forum Security
    Replies: 5
    Last Post: Sep 19th, 2005, 07:57 AM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. Replies: 8
    Last Post: May 10th, 2005, 05:46 AM
  5. Replies: 2
    Last Post: Aug 30th, 2004, 04:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •