基于session的springmvc国际化

来源:互联网 发布:up to u网络天什么意思 编辑:程序博客网 时间:2024/06/06 20:48

转自:https://www.cnblogs.com/FlyHeLanMan/p/6282077.html

项目中采用springMvc的框架,需要动态切换语言,找了一些资料,最后决定采用基于session的动态切换,实现动态切换中文,英文,韩文,其实就是把中文翻译成其他语言显示

springMvc国际化包括两个方面,一个是前台页面的国际化,spring有自己的标签可以去实现,非常方便,另一个是后台java代码种涉及中文的国际化。

1、项目总体结构

 

有关bean的主要配置在spring-mvc.xml里配置,messages文件夹里放的是需要翻译的内容格式如下:key  =  value 的格式

英文:  

中文: 

韩文 :

需要注意的是 配置文件里面涉及到的中文需要转成unicode编码,否则翻译后会出现乱码的情况。

2、在spring-mvc加入以下配置:

复制代码
 1 <!-- 国际化资源配置,资源文件绑定器--> 2     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 3         <!-- 国际化资源文件配置,指定properties文件存放位置 --> 4         <property name="basename" value="classpath:messages/message" /> 5         <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称  -->                6         <property name="useCodeAsDefaultMessage" value="true" /> 7     </bean> 8     <!-- 动态切换国际化 ,国际化放在session中 --> 9     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>10     <mvc:interceptors>11         <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->12         <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">13             <!-- 通过这个参数来决定获取那个配置文件 -->14             <property name="paramName" value="language" />15         </bean>16     </mvc:interceptors>
复制代码

需要注意的是 basename的值需要配置到messages/message 这一级别也就是国际化文件存放的位置,一直到第一个下划线前面。

3、前台页面实现

 

复制代码
 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 4 <%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title>10 </head>11 <body>12 选择语言:<a href="?language=zh_CN">中文</a> | <a href="?language=en_US">英文</a> | <a href="?language=ko_KR">韩文</a>13 <br></br>14 这里展示选择对应语言后的“你好”的翻译(前台标签翻译):15 <spring:message code="你好" />16 <br></br>17 这里展示选择对应语言后的“欢迎你”的翻译(后台代码翻译):${welcome}18 19 </body>20 </html>
复制代码

需要注意的是 页面引入spring的标签,  code我这里写的是中文,对应那三个翻译文件里的key 如果code在翻译文件里没有找到对应的key则使用code里面的内容 这个配置在spring'-mvc 里指定了。

 

4、后台代码的实现

复制代码
 1 package com.ccg.controller; 2  3  4  5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7  8 import org.springframework.stereotype.Controller; 9 import org.springframework.web.bind.annotation.RequestMapping;10 import org.springframework.web.servlet.ModelAndView;11 import org.springframework.web.servlet.support.RequestContext;12 13 14 @Controller15 @RequestMapping("test")16 public class TestController {17     18     @RequestMapping("view")19     public ModelAndView view(HttpServletRequest request, HttpServletResponse response){20         //spring翻译使用req.getmessage()方法21         RequestContext req = new RequestContext(request);22         ModelAndView model = new ModelAndView("test");23         model.addObject("welcome", req.getMessage("欢迎你"));24         return model;25     }26 }
复制代码

5、效果展示

总结一下springMvc的国际化的关键点:

1)指定spring国际化需要翻译的文件位置,需要注意的是路径一定要写完整。

2)指定spring国际化时的参数,上文中我用到是language,配置好之后,spring会根据请求中该参数的值找到对应的配置文件。

3)前台页面使用spring标签,code对应配置文件中的key,推荐使用中文,万一对应不上的时候默认显示code里面的内容(需要在spring-mvc.xml里指定配置)

4)后台代码国际化使用RequestContext 对象的getMessage 方法,返回String。

 

以上就是我对springMvc国际化的实现,有问题可以留言。

原创粉丝点击