Announcement Announcement Module
Collapse
No announcement yet.
access messages.properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    You can implement the MessageSourceAware interface.

    Comment


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

      Thanks ~san

      Comment


      • #4
        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

        Comment


        • #5
          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>

          Comment


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

            Comment


            • #7
              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

              Comment


              • #8
                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

                Comment


                • #9
                  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.

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      Full example

                      hi, here is a example

                      1.- If you using Netbeans. Create a new message.properties on the folder other sources.

                      Attachment

                      2.- open the file and set the key

                      title=MyTitleTest


                      3.- In your disparcher-servlet.xml (Spring IOC Container)
                      Code:
                           <bean id="localeChangeInterceptor"
                              class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                              <property name="paramName" value="language" />
                          </bean>
                      
                          <bean id="messageSource"
                              class="org.springframework.context.support.ResourceBundleMessageSource">
                              <property name="basename" value="messages" />
                          </bean>
                      
                      // this is you controller where you make the reference to the messageSource  
                      
                        <bean name="userProfileFirstLoginController" class="gaa.vaa.web.user.profile.UserProfileFirstLoginController">
                             <property name="messageSource" ref="messageSource" />
                         </bean>
                      2.- In this case in your controller implement MessageSourceAware

                      Code:
                      public class UserProfileFirstLoginController extends AbstractController implements MessageSourceAware{
                          
                          private MessageSource messageSource_;
                          
                          @Override
                          public void setMessageSource(MessageSource ms) {
                              messageSource_=ms;
                          }
                      
                      
                      protected ModelAndView handleRequestInternal(
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
                      
                      
                           Map<String,Object> model = new HashMap<String,Object>();
                      
                           // here is how to get the message 
                           String messageS = messageSource_.getMessage("title",null,Locale.ENGLISH);
                      
                           model.put("messageS",messageS);
                           return new ModelAndView(modelAndView,model);        
                              
                          }
                      Done, you get the message
                      Attached Files

                      Comment

                      Working...
                      X