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
- <span style="FONT-SIZE:14PX;" ><bean id="Messagesource" class=" Org.springframework.context.support.ReloadableResourceBundleMessageSource ">
- <property name="defaultencoding" value="iso-8859-1"/>
- <property name="usecodeasdefaultmessage" value="true"/>
- <property name="cacheseconds" value="ten" ></property>
- <property name="Basenames" >
- <list>
- <value>classpath:messages</value>
- </list>
- </property>
- </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
- <span style= "FONT-SIZE:14PX;" >mobile.is.null= mobile phone number cannot be empty
- Mobile.is.invalid= mobile phone number is invalid
- Username.is.null= user name cannot be empty
- 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
- <c:if test="${msgs!=null && msgs.size () >0}">
- <fmt:message key="${msgs}" />
- </c:if>
Messagesource in spring 2--reloadableresourcebundlemessagesource "Go" using the configuration method