Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: access messages.properties

  1. #1
    Join Date
    May 2007
    Posts
    10

    Default access messages.properties

    Hello Friends,
    For internationalization sake, I have defined messages in message.properties file. I have maintained different copies for different languages. It’s all fine now. Now, for generic error handling, I need access messages.properties values in DAO layer. How can access messages.properties in Spring Java Code?

    ~San

  2. #2
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    You can implement the MessageSourceAware interface.

  3. #3
    Join Date
    May 2007
    Posts
    10

    Default

    Thanks ... But I am a newbee to spring !!!
    Could you elobrate more on this Please

    Thanks ~san

  4. #4
    Join Date
    Nov 2006
    Location
    Germany
    Posts
    452

    Default

    Hi,

    if you implement MessageSourceAware, then the Spring Applicationcontext will inject the MessageSource into your Spring configured bean.

    Here is an example

    Code:
    class Sample implements MessageSourceAware {
    
     private MessageSource messageSource;
    
     public void setMessageSource(MessageSource messageSourcePar) {
    
            messageSource = messageSourcePar;
     }
    
    public void foo(){
          messageSource.getMessage(.. ,  .. ,  ...);
    }
    }

    Note. The bean must be instanciated through teh Spring IOC Container, otherwise your MessageSource is null

    regards
    agim

  5. #5
    Join Date
    May 2007
    Posts
    10

    Default

    Ok.I did as you suggested. Now I get some other error.

    Piece of code “messageSource.getMessage("header.searchHotels", null,
    Locale.ENGLISH)” gives me this following error is

    "org.springframework.context.NoSuchMessageExceptio n: No message found under code 'header.searchHotels' for locale 'en'."




    My Message Soorce entried in IBE-Servlet-config.xml are as follow

    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basename" value="messages"/>
    </bean>

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor">
    <property name="paramName" value="siteLanguage"/>
    </bean>

  6. #6
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    Check the content of your properties file. Is there a "header.searchHotels" property in the messages_en.properties on your classpath?

  7. #7
    Join Date
    May 2007
    Posts
    10

    Default

    Got it , I moved the messageSoorce bean definition to Application Context XML.

    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basename" value="messages"/>
    </bean>


    Its working now .Thanks

  8. #8
    Join Date
    Oct 2008
    Posts
    18

    Default

    Hi at all,

    I also want to use the message resource in my controller and implemented the interface. Now my question: how do I instanciate the resource to my controller? Is the following the right way?

    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basenames">
    <list>
    <value>systemmsg</value>
    <value>international</value>
    <value>documentcreator</value>
    </list>
    </property>
    </bean>

    <bean id="fileUploadController" class="de.saxsys.hshec.controller.FileUploadContro ller">
    <property name="commandClass" value="de.saxsys.hshec.domain.FileUploadBean" />
    <property name="formView" value="ecupload" />
    <property name="successView" value="ecupload" />
    <property name="msgSource" ref="messageSource" />
    </bean>

    Regards
    Hardie

  9. #9
    Join Date
    Jul 2008
    Location
    Medellín, Colombia
    Posts
    135

    Default

    If Your using Web FLow:

    Declaration of the message sopurce:
    Code:
    	<bean id="messageSource"
              class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basenames">
                <value>messages</value>
            </property>
        </bean>
    From within a flow just send the EL variable messageContext to your method ...

    and the one I like:

    Code:
    	protected MessageContext getMessageContext() {
    		MessageContext messageContext = (MessageContext) RequestContextHolder
    		.getRequestContext().getMessageContext();
    		return messageContext;
    	}
    with the last one you can get you're message properties anywhere on you're code, but seems to me that is not a god idea to attach messages from within a Dao, you better throw spring exceptions, like DataAccessException, ObjectRetrievalNotFound... etc.

  10. #10
    Join Date
    May 2007
    Location
    London
    Posts
    7

    Default

    Just implement MessageSourceAware... I mean, come on, how cool is that??

    Spring is just super-awesome.
    And all these tireless answers on the forum... you're great.
    Many thanks.

Posting Permissions

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