Spring mvc下国际化的实现

之前一直用的是struts的国际化方法。现在尝试一下spring下面的国际化。具体步骤如下:

1.web.xml中增加   

 <jsp-config>  
    <taglib>  
      <taglib-uri>/spring</taglib-uri>  
      <taglib-location>/WEB-INF/resources/spring-form.tld</taglib-location>  
    </taglib>  
    </jsp-config> 

这样jsp中就能识别spring:message标签了

2. spring-config.xml 中增加

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

如果需要有多个资源文件,比如 异常是一个,页面文字是一个的话,这样写

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

       <property name="basenames">

            <list>

                 <value>error</value>

                 <value>message</value>

            </list>

       </property>

   </bean>

3. 具体的jsp中增加

标签库引用 <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

具体标签   <spring:message code="system.name" />

4. 在src 下建立资源文件: messages.properties  

里面记得写unicode的值,如

username.illegal=u7528u6237u540du4e0du5408u6cd5uff0cu5fc5u987bu4e3au5b57u6bcdu6570u5b57u7ec4u6210uff0cu957fu5ea6u4e3a5-10

如果有多国语言的话,增加资源文件 messages_zh_CN.properties messages_en_US.properties

打包后的系统,资源文件请放在 WEB-INF下

如果出现一下问题:

org.springframework.context.NoSuchMessageException: No message found under code 'XXXXXX' for locale 'en_US'.
如果你使用eclipse的话,应该把属性文件放在src文件夹下,而不是工程下,或者是因为文件名不对messages.properties   messages_zh_CN.properties messages_en_US.properties

原文地址:https://www.cnblogs.com/superch0054/p/4010067.html