spring mvc 国际化的几种方案

首先配置我们项目的service-servlet.xml文件添加的内容如下:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的文件名,根据ResourceBundleMessageSource类加载资源文件.\src\main\resources\messages\messages_en_US.properties -->
<property name="basename" value="messages/messages" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
1. 一.基于浏览器请求的国际化实现:

使用Controller测试,

@RequestMapping(value="/test",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public Result test(HttpServletRequest request){
public Result test(HttpServletRequest request,Model model){
//从后台代码获取国际化信息
RequestContext requestContext = new RequestContext(request);
String msg = requestContext.getMessage("msg");
return new Result(true, msg, "返回数据");
}

  

通过设置浏览器请求测试:http://localhost:8080/xxx/nation/test

  • 注意: 上述基于浏览器设置,根据浏览器的本地来确定message
  1. 基于session的国际化
    在项目中的源文件夹resources/messages中添加messages.properties、messages_zh_CN.properties、messages_en_US.properties三个文件,其中messages.properties、messages_zh_CN.properties里面添加msg="\u662F\u4E0D\u662F"为中文,messages_en_US.properties里面的为msg="ok"。
    在项目的service-servlet.xml文件添加的内容如下,(之前ResourceBundleMessageSource的配置任然保留)
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

使用controller测试

@RequestMapping(value="/test",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public Result test(HttpServletRequest request, @RequestParam(value="langType", defaultValue="zh") String langType){ if(langType.equals("zh")){
Locale locale = new Locale("zh", "CN");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
else if(langType.equals("en")){
Locale locale = new Locale("en", "US");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}else{
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
}
//从后台代码获取国际化信息
RequestContext requestContext = new RequestContext(request);
String msg = requestContext.getMessage("msg");
return new Result(true, msg, "返回数据");
}

通过设置浏览器请求测试:http://localhost:8080/xxx/nation/test?langType=zh 或者 http://localhost:8080/xxx/nation/test?langType=en

  1. 基于cookie,与session类似
    移除session国际化的设置
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> 

添加cookie设置

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<!-- 设置cookieName名称,可以根据名称通过js来修改设置,也可以像上面演示的那样修改设置,默认的名称为 类名+LOCALE(即:org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE-->
<property name="cookieName" value="lang"/>
<!-- 设置最大有效时间,如果是-1,则不存储,浏览器关闭后即失效,默认为Integer.MAX_INT-->
<property name="cookieMaxAge" value="100000" />
<!-- 设置cookie可见的地址,默认是“/”即对网站所有地址都是可见的,如果设为其它地址,则只有该地址或其后的地址才可见-->
<property name="cookiePath" value="/" />
</bean>

使用Controller测试

@RequestMapping(value="/test",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public Result test(HttpServletRequest request, HttpServletResponse response,@RequestParam(value="langType", defaultValue="zh") String langType){ if(langType.equals("zh")){
Locale locale = new Locale("zh", "CN");
(new CookieLocaleResolver()).setLocale (request, response, locale);
}else if(langType.equals("en")){
Locale locale = new Locale("en", "US");
(new CookieLocaleResolver()).setLocale (request, response, locale);
}else
(new CookieLocaleResolver()).setLocale (request, response, LocaleContextHolder.getLocale());
//从后台代码获取国际化信息
RequestContext requestContext = new RequestContext(request);
String msg = requestContext.getMessage("msg");
return new Result(true, msg, "返回数据");
}
  1. 基于url国际化方式
    配置如下,移除上述localeResolver的bean改为下面的:
<bean id="localeResolver" class="xx.xxx.xxx.UrlAcceptHeaderLocaleResolver"/>

UrlAcceptHeaderLocaleResolver为自定义实现,具体代码如下:

public class UrlAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver {

    private Locale urlLocal;

    public Locale resolveLocale(HttpServletRequest request) {

        return urlLocal!=null?urlLocal:request.getLocale();
} public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
urlLocal = locale;
} }
  • 之后就可以在请求的URL后附上 locale=zh_CN 或 locale=en_US 如 http://xxxxxxxx?locale=zh_CN 来改变语言了
    使用Controller测试,
@RequestMapping(value="/test",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public Result test(HttpServletRequest request){
public Result test(HttpServletRequest request,Model model){
//从后台代码获取国际化信息
RequestContext requestContext = new RequestContext(request);
String msg = requestContext.getMessage("msg");
return new Result(true, msg, "返回数据");
}

5.总结下,以上几种其实都是基于

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的文件名 -->
<property name="basename" value="messages/messages" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>

这里无非是读取messages目录下以messages开头的几种配置文件,借助MessageSource根据local读取相应的配置文件中的信息

Locale locale = new Locale("en", "US");
String message = msr.getMessage("msg",
new Object [] {"userDao"}, "Required", locale);
System.out.println(message);

所以其实控制local即选择相应的处理方式,而以上几种均是通过拦截器注入不同的local来实现,这里我们可以自己实现符合自己业务场景的实现方式

 

作者:勃列日涅夫
链接:https://www.jianshu.com/p/680fc53dc3cc
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

spring mvc 国际化的几种方案的更多相关文章

  1. Spring MVC(十六)--Spring MVC国际化实例

    上一篇文章总结了一下Spring MVC中实现国际化所需的配置,本文继上一文举一个完整的例子,我选择用XML的方式.我的场景是这样的: 访问一个页面时,这个页面有个表格,对表头中的列名实现国际化. 第 ...

  2. spring mvc 国际化

    spring mvc 国际化 使用CookieLocaleResolver实现国际化的步骤:1.注册 messageSource,localeResolver 两个bean2.调用localeReso ...

  3. Spring入门(十四):Spring MVC控制器的2种测试方法

    作为一名研发人员,不管你愿不愿意对自己的代码进行测试,都得承认测试对于研发质量保证的重要性,这也就是为什么每个公司的技术部都需要质量控制部的原因,因为越早的发现代码的bug,成本越低,比如说,Dev环 ...

  4. Spring MVC国际化配置

    Spring MVC国际化配置 前言 项目开发中要考虑支持国际化,框架选用的是Spring MVC框架,那么问题来了Spring MVC如何配置并实现国际化. 实现过程(Maven项目) 对于Spri ...

  5. spring MVC 拦截有几种实现方式

    spring MVC 拦截有几种实现方式 实现HandelInterceptor接口方式        继承HandelInterceptor 的方式.一般有这两种方式 spring 如何走单元测式 ...

  6. Spring MVC国际化

    本文基于Spring MVC 注解-让Spring跑起来.本文提到的国际化是Spring实现国际化的方案之一. (1) 在applicationContext.xml中添加以下配置信息: <!- ...

  7. spring mvc 参数传递的三种方式

    springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  8. spring mvc和web-flow的整合方案

    发现了一份整合spring MVC 和webflow的很好的资料,日后翻译出来发布.先记着. http://docs.spring.io/spring-webflow/docs/2.3.x/refer ...

  9. SpringBoot + Spring MVC国际化使用示例

    项目中需要显示中英文两种语言,所以需要对显示的内容进行国际化,如下是一个示例程序. 程序文件结构,如下图,后面详细列出各文件的代码. 1. 编写maven的pom.xml文件,如下: <proj ...

随机推荐

  1. Python无限循环

    Python 无限循环:在 while 循环语句中,可以通过让判断条件一直达不到 False ,实现无限循环. 条件表达式: # var = 1 # while var == 1: # 表达式永远为 ...

  2. PHP image2wbmp - 输出WBMP图片

    image2wbmp — 以 WBMP 格式将图像输出到浏览器或文件.高佣联盟 www.cgewang.com 语法 int image2wbmp ( resource $image [, strin ...

  3. [草稿]Skill 中的map

    https://www.cnblogs.com/yeungchie/ Skill 中的map map mapc mapcan mapcar mapcon mapinto maplist

  4. C/C++编程笔记:C++入门知识丨运算符重载

    本篇要学习的内容和知识结构概览 运算符重载使用场景 常规赋值操作 我们现在有一个类 想要实现这种赋值操作 具体实现如下: 所以说呢,我们在使用运算符进行运算的时候, 实际上也是通过函数来实现运算的. ...

  5. EC R 87 div2 D. Multiset 线段树 树状数组 二分

    LINK:Multiset 主要点一下 二分和树状数组找第k大的做法. 线段树的做法是平凡的 开一个数组实现就能卡过. 考虑如树状数组何找第k大 二分+查询来判定是不优秀的. 考虑树状数组上倍增来做. ...

  6. Android JNI之静态注册

    这篇说静态注册,所谓静态注册,就是native的方法是直接通过方法名的规定格式和Java端的声明处代码对应起来的,其对应规则如下: JNIEXPORT <返回值> JNICALL Java ...

  7. 阿里居然推出了开源的JDK,你造么?

    简介 Alibaba Dragonwell 是一款免费的, 生产就绪型Open JDK 发行版,提供长期支持,包括性能增强和安全修复.阿里巴巴拥有最丰富的Java应用场景,覆盖电商,金融,物流等众多领 ...

  8. jmeter分布式踩得坑汇总

    一.普通的配置文件基本都能网上搜索资料,这里就简单记录: a.jmeter.properties几处修改:1.remote_hosts=master压力机Ip;2.server_port,开启服务器端 ...

  9. Ant Design Pro V5 从服务器请求菜单(typescript版)

    [前言] 找了很多Admin模板,最后还是看中了AntDesignPro(下文简写antd pro)这个阿里巴巴开源的Admin框架,长这样(还行吧,目前挺主流的): 官网地址:https://pro ...

  10. 7、Prototype 原型模式 通过复制创造实例 创造型模式

    2020-07-19 发哥讲 发哥讲 其实上一节的末尾讲到如何去生成对象,其中有一个关于clone的,这其实就是Prototype原型模式. 通过克隆(拷贝)的方式生成对象 1.了解Prototype ...