spring mvc 3 国际化(上)——基础使用

spring mvc 国际化的基本使用

基本使用步骤

 

1.在spring的配置文件里加入

    <!-- Register the welcome.properties -->
    <bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <!-- welcome为文件的前缀 ,message为文件夹 -->
        <property name="basenames" value="message/welcome"/>
        <property name="defaultEncoding" value="utf8" />
    </bean>

 

2.放入资源文件

message/welcome_en.properties

welcome.springmvc = english title

message/ welcome_zh.properties

welcome.springmvc = chongwen title

 

3.在jsp里使用

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<h3>
    welcome.springmvc : <spring:message code="welcome.springmvc" text="default text" />
</h3>

 

4.结束

上面3步就实现了spring的项目的国际化,默认使用的是头信息里的

Accept-Language:zh-CN,zh;q=0.8

 来判断语言,(决定权在浏览器)

 

使用cookie或session来判断用户的语言

 

1.使用cookie

在spring配置文件中加入:

   <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>

 这是决定使用cookie来存储用户的语言,里面还可以配置用户的默认语言(也可以不配置)。

 

2.使用session

    <bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en"/>
    </bean>

 

 3.使用用户浏览器的语言(默认的采用的方法)

 <!--这个就是使用的http头信息里面的接收语言来判断用户的语言   -->
    <bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
    </bean>

 

修改用户语言

1.使用

spring mvc的配置文件中插入

  <mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="changeparam"/>
    </bean>
    </mvc:interceptors>

 

这样就访问 http://localhost:8080?changeparam=en 就可以修改了

 

2.注意事项

使用浏览器来判断用户语言的方法不能修改,只有在使用Cookie和session的时候才可以修改用户语言。

 

代码中获取用户语言

 

request.getLocale()

 

 结束

spring mvc 3 国际化(下)——简单自定义操作

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值