Messagesource in spring 2--reloadableresourcebundlemessagesource "Go" using the configuration method

Source: Internet
Author: User

This article reproduced only for their own study included, do not do any commercial use, if necessary to access the original address: http://blog.csdn.net/qyf_5445/article/details/8124362

How do I implement Messagesource in the Spring MVC framework to manage International resource files?

As follows:

1. Configure the following within the Applicationcontext.xml file

[Java]View PlainCopy
  1. <span style="FONT-SIZE:14PX;" ><bean id="Messagesource" class=" Org.springframework.context.support.ReloadableResourceBundleMessageSource ">
  2. <property name="defaultencoding" value="iso-8859-1"/>
  3. <property name="usecodeasdefaultmessage" value="true"/>
  4. <property name="cacheseconds" value="ten" ></property>
  5. <property name="Basenames" >
  6. <list>
  7. <value>classpath:messages</value>
  8. </list>
  9. </property>
  10. </bean></span>

Set "Usecodeasdefaultmessage", which defaults to false so that if spring cannot find Messagekey in ResourceBundle, it throws Nosuchmessageexception, If you set it to true, you cannot find an exception to be thrown, but instead use Messagekey as the return value.

2. Create a new messages.properties file under your src file path and write some information

[Plain]View PlainCopy
    1. <span style= "FONT-SIZE:14PX;" >mobile.is.null= mobile phone number cannot be empty
    2. Mobile.is.invalid= mobile phone number is invalid
    3. Username.is.null= user name cannot be empty
    4. username.already.exists= user name already exists </span>


3. In the required controller can be as follows:

Model.addattribute ("Msgs", "username.already.exists");

4. Use FMT label to display normally

<%@ taglib prefix= "FMT" uri= "Http://Java.sun.com/jsp/jstl/fmt"%>

[HTML]View PlainCopy
      1. <c:if test="${msgs!=null && msgs.size () >0}">
      2. <fmt:message key="${msgs}" />
      3. </c:if>

Messagesource in spring 2--reloadableresourcebundlemessagesource "Go" using the configuration method

Related Article

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.