4

I did some googling around and found my problem have appeared many times but i have tried all suggested solutions but it is still not working for me

I am keep getting this exception:

org.springframework.context.NoSuchMessageException: No message found under code 'error.null.firstname' for locale 'en'.

here is my setup in my dispatcher-servlet.xml file i have:

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value = "error"/>
</bean>

i have a properties file (error_en.properties) under src/main/resources/ directly and it does contain the following line:

error.null.firstname=Firstname cannot be null

in my code i am trying:

    @Autowired
    private MessageSource messageSource;
    ...
    System.out.println(messageSource.getMessage("error.null.firstname", null, Locale.ENGLISH));

but above is not working and is giving me exception mentioned above

I've tried prefixing "classpath:" to 'value' in value attribute but that didn't work for me. I've ensured that the filename 'error' matches with the value attribute because i know '_en' would be dealt with by Spring Framework.

I can't see what am i missing?

5
  • Which IDE are you using? Also, please make sure the name of the property file is correct. Commented Aug 8, 2015 at 11:00
  • Eclipse. my property file is "error_en.properties" where i have value attribute to "error" and in code i'm using Locale.ENGLISH in the getMessage method of MessageSource Commented Aug 8, 2015 at 11:06
  • Actually I tried same in my eclipse and it's working fine. Can you also make sure src/main/resources/ is in build path? Commented Aug 8, 2015 at 11:16
  • yep it is on the build path Commented Aug 8, 2015 at 11:28
  • hmm. Not sure what else can be done. Would it be possible to share your eclipse screenshot. Might able to spot something otherwise I tried your code and it's working for me. Commented Aug 8, 2015 at 11:34

1 Answer 1

1

after days of experimenting/debugging - finally figured out what the problem is.

The class where i'm autowiring the MessageSource was actually belong to a different context.

My messageSource bean definition was declared in my dispatcher-Servlet.xml context file because i'm developing a web application.

Whereas my business class was declared in application-context.xml so it couldn't find the MessageSource.

After i've moved everything into the dispatcher-Servlet.xml file it work. Probably not an elegant solution but that solved my problem.

Thanks for all help previously

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.