In spring MVC, how to get internationalized content in Java code

Source: Internet
Author: User
Tags getmessage object object

First, define in spring's application.xml

 <BeanID= "Messagesource"class= "Org.springframework.context.support.ResourceBundleMessageSource">        <!--the file name where internationalization information resides -        < Propertyname= "BaseName"value= "Messages/messages"/>        <!--if the corresponding code is not found in the Internationalized resource file, use this code as the name -        < Propertyname= "Usecodeasdefaultmessage"value= "true"/></Bean>

Where the value of the ID must be "messagesource" or an error will be

First of all, I think that since it is a well-declared bean, you should be able to use @Autowired tag to bind it. So I wrote the following code:

 Public class Const {      @Autowired      privatestatic  resourcebundlemessagesource RMS             publicstatic  string Gettextvalue (String key) {          return  NULLnull);      }  }  

The RMS value is always null, which means that the injection failed.

Later on the internet, "ApplicationContext" This interface inherits the "Messagesource" interface, then we just get the project ApplicationContext implementation class, you can pass the GetMessage () method to get the Internationalized file content.

So how easy is it to get the ApplicationContext implementation class? This time requires another interface, that is, "Applicationcontextaware", any class implementation of this interface, will be injected into the applicationcontext.

 Public classSpringutilImplementsApplicationcontextaware {Private StaticApplicationContext ApplicationContext;  Public StaticApplicationContext Getapplicationcontext () {returnApplicationContext; } @Override Public voidSetapplicationcontext (ApplicationContext arg0)throwsbeansexception {ApplicationContext=arg0; }         Public StaticObject Getbean (String ID) {Object object=NULL; Object=Applicationcontext.getbean (ID); returnobject; }  } 

Of course, the above Springutil class must be configured in the Application.xml file to allow it to be read by the spring framework and then inject applicationcontext into it. The configuration is simple:

<id= "Springutil"  class= "Util". Springutil "/>  

That's it, okay?

public class Const {public            static string Gettextvalue (String key) {          return Springutil.getapplicationcontext (). GetMessage (key, NULL, NULL);      }  }  

In spring MVC, how to get internationalized content in Java code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.