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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值