Announcement Announcement Module
No announcement yet.
Access MessageSource from a service fails Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access MessageSource from a service fails

    Hi all!

    I need access message sources (due to current locale) in a service (invoked via Ajax), but when trying to retrieve the message linked to the given key, an error is thrown saying that the key doesn't exists for the given locale.

    As I'm inside a service (and not in a controller), and it is invoked via Ajax, I need to "retrieve" the locale from the JSP. The solution I'm using is leaving two hidden fields with the country and the language of the current locale, and in the service method instance a Locale with this information to access the message source.

    I'm suspecting that the error appears because I'm instantiating a Locale (and not using the instance that Spring manages along all services...), but I have not found any other way to do this.

    To access MessageSources I'm using the approach of implementing MessageSourceAware in the service class (same approach is used in a controller and it works perfectly). Can anyone help me with this problem?

    Edit: ┐Could be the reason the fact of trying to access MessageSource from a service? Strange, but possible...
    Last edited by bLaDe00; Oct 1st, 2008, 09:55 AM.

  • #2
    Testing this problem, I'm absolutely becoming mad.

    Same message key tested in a controller works perfect, but in this service it doesn't appears, but also any of the other messages defined doesn't appear!

    org.springframework.context.NoSuchMessageException: No message found under code '' for locale 'es_ES'.
    Which is the reason of this behaviour? As I've read about MessageSourceAware, it allow to access messages from any java class which implements this interface...

    The code:

    public class AjaxServiceImpl implements MessageSourceAware, AjaxService 
    	@Autowired private ResearcherService researcherService;
    	@Autowired private PublicationService publicationService;
    	/* Load via XML inyection */
    	private MessageSource messageSource; 
    	public void setMessageSource(MessageSource messageSource)
    		this.messageSource = messageSource;
    	public List<LabelValue> getAllPublicationTypes(HttpServletRequest request)
    		List<LabelValue> publicationTypeList = new ArrayList<LabelValue>();
    		Locale locale = request.getLocale();
    		publicationTypeList.add(new LabelValue(messageSource.getMessage("", null, locale),"1") );
    		publicationTypeList.add(new LabelValue(messageSource.getMessage("PublicationType.bookChapter", null, locale),"2") );
    		publicationTypeList.add(new LabelValue(messageSource.getMessage("PublicationType.journal", null, locale),"3") );
    		publicationTypeList.add(new LabelValue(messageSource.getMessage("PublicationType.conference", null, locale),"4") );
    		return publicationTypeList;

    Note: the MessageSource is filled in the application-Context.xml when defining the service for DWR:

    In application-Context.xml:
    <bean id="ajaxService" name="ajaxService" class="es.udc.lbd.portal.http.controller.ajax.AjaxServiceImpl">
    		<dwr:remote javascript="AjaxFindService">
    			<dwr:include method="getAllPublicationTypes" />
    			<dwr:convert type="bean" class="es.udc.lbd.portal.http.view.formplainobjects.LabelValue"></dwr:convert> 
    		<property name="messageSource" ref="messageSource"/>
    	<dwr:controller id="dwrController" debug="true" />

    In X_servlet.xml
        <bean id="messageSource" 
            <property name="basename" value="messages"/>


    • #3
      It is because the application context doesn't have visibility on beans defined in the servlet context files (but the servlet context inherits all beans from the application context). Move your message source into the application context and it should work.

      BTW - I don't think you need to add the messageSource property to the ajaxService bean in application context as you implement MessageSourceAware so as I understand, it should be automagically injected.


      • #4
        Yeah, it works

        Thanks a lot! I forgot putting message source into application-Context to allow access it from the services (little mistake... but hard to find )