springmvc國際化 基於瀏覽器語言的國際化配置


項目結構圖如下:

說明:lib下存放的是Spring相關包,項目應用包為Spring3.2,message_*.properties中存放的是國際化的資源文件

資源文件

英語的資源文件message_en.properties

main.title=Hello World
main.target=I love you

韓語的資源文件messages_ko.properties

main.title=\uC548\uB155\uD558\uC2ED\uB2C8\uAE4C
main.target=\uC0AC\uB791\uD574

對應的韓語為:

main.title=안녕하십니까
main.target=사랑해


Springspring mvc的相關配置


web.xml
文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/context/spring-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/context/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 spring-mvc的配置文件servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.pudp" />
</beans:beans>

spring配置文件spring-context.xml中聲明國際化

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
   
   <context:component-scan base-package="com.pudp" />    
    
   <!-- 定義國際化消息-->   
   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">   
     
     <!-- 其中basename用來指定properties文件的通用名 如實例中的messages_en.properties,messages_ja.properties通用名都是messages -->
     <property name="basename" value="messages"/>
     <property name="useCodeAsDefaultMessage" value="true" />
     
   </bean>   
    
   <!-- 獲取本地 -->  
   <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>  
    
</beans>

說明:servlet-context.xml 只會在servlet做出響應,所以加載信息應該放置在spring配置文件中.

視圖層應用

視圖層應用前,需要先聲明spring標簽

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

 國際化標簽引用

  <body>
      <!-- 使用message 標簽配置需要顯示的國際化文本, code 對應國際化文件中對應的鍵的名稱 -->
    <span style="color: #2D2D2D;">
        <spring:message code="main.title" />
    </span>
    <br>
    <input type="text" value="<spring:message code="main.target"/>">
  </body>

程序運行結果:

因為我瀏覽器默認語言是韓語,所以運行結果會展示韓語相關.

更改瀏覽器默認使用語言.實例中更改為日語,如下圖



這時運行程序,系統根據瀏覽器默認的語言選擇日語展示



轉載請注明出處:[http://www.cnblogs.com/dennisit/p/3359008.html]

在線交談


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM