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=\u7528\u6237\u540d\u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u4e3a\u5b57\u6bcd\u6570\u5b57\u7ec4\u6210\uff0c\u957f\u5ea6\u4e3a5-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

posted @ 2013-07-28 19:51  竹君子  阅读(154)  评论(0编辑  收藏  举报