1.建立资源文件

在webapp下建立文件夹language,在其中再添加file,命名分别为language.properties,language_en.properties,language_zh_CN.properties。其中language.properties为默认资源文件。

在其中添加内容,格式如下:

language.properties

welcome=Welcome

language_en.properties

welcome=Welcome

language_zh_CN.properties

welcome=\u6b22\u8fce

其中welcome为key,在jsp中调用使用的就是这个,汉语的需要使用unicode编码(在eclipse的properties文件中输入汉语会自动转换成unicode。\u6b22\u8fce-欢迎)

2.配置springMVC

在demo-servlet.xml中添加如下

<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- Saves a locale change using a session-->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<!-- 国际化文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/language/language" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>

其中<property name="basename" value="/language/language" />value即为资源文件所在,不需要后缀名.properties。

3.调用国际化

3.1 在web中调用国际化

在jsp文件头添加

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

在jsp HTML显示内容处将内容更改,如

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true">
Welcome
</a>

更改为

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true">
<spring:message code="welcome"/>
</a>

<input type="text" value="welcome" />

更改为

<input type="text" value='<spring:message code="welcome"/>' />

3.2 在javascript中调用国际化

在jsp文件头中添加

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

$.messager.alert('info');

更改为

$.messager.alert('<spring:message code="message.info"/>');

注意:1.<spring>标签不可以这样写"<spring:message code=\"message.info\"/>"这样会出错。

2.在_en文件中key的值不可以使用\或'符号。

3.3 在java中调用国际化

在controller中使用

if(!model.containsAttribute("contentModel")){

    RequestContext requestContext = new RequestContext(request);

    String welcome = requestContext.getMessage("welcome");

}

其中request为HttpServletRequest,model为Model。

4 解决乱码

4.1 中文输入乱码

完成了上面的步骤之后,会发现XCenter界面中输入汉语,点击确定,往数据库中写入汉语时,写入的是乱码。

解决办法如下:

在applicationContext.xml中的

<property name="url" value="${jdbc.url}"/>

更改为

<property name="url" value="${jdbc.url}?useUnicode=true&amp;characterEncoding=UTF-8"/>

注意:不要在jdbc.properties中的jdbc.url=jdbc:mysql://localhost:3306/demo之后添加?useUnicode=true&amp;characterEncoding=UTF-8,这样是不正确的。

4.2 部分界面显示中文乱码

接下来发现在大部分界面因为添加了这句

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

可以正常显示中文了,但是XCenter部分界面中文显示为???,经过检查发现是因为在controller中返回值为String,而且标记为@ResponseBody,查资料发现是因为Spring MVC中@ResponseBody默认编码是ISO-8859-1(其他的都是UTF-8,谁知道为什么同一个框架中这里要用另外一种编码)。

解决办法:

@RequestMapping(value = "welcome", method = RequestMethod.POST)

中添加produces = "application/json; charset=utf-8"

变成

@RequestMapping(value = "welcome", method = RequestMethod.POST, produces = "application/json; charset=utf-8")

然后会发现在部分的tree中中文仍然显示为乱码,发现是因为返回值转换成JSON格式的原因。

解决办法:

tree的返回值格式化将

JSON.toJSONStringWithDateFormat(rs.getDefaultModel(), "yyyy-MM-dd HH:mm:ss");

  更改为

JSON.toJSONString(rs.getDefaultModel());

5 中英文切换

5.1 资源文件的中英文切换

这个比较简单,使用如下代码就行

<a href="?locale=en" onclick="changeLanguage('en')">English</a>
<a href="?locale=zh" onclick="changeLanguage('zh_CN')">简体中文</a>

5.2 easyui框架的中英文切换

easyui中有些框架(如datagrid)有些内容是框架自带的,需要在<head>中加入

<script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>

那如何切换中英文呢

首先不要在<head>中添加上面这句,然后在主界面<head>中加入如下js语句

<script type="text/javascript">

    var language=window.navigator.language;
var userLanguage="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE']}"; if(null != userLanguage&&userLanguage!=""){//not login
language = userLanguage;
} $(function(){
var src = 'js/locale' + '/easyui-lang-'+language.replace("-","_")+'.js';// when login in China the language=zh-CN
$.getScript(src);
}); </script>

这是使用jquery加载js文件。

接下来将项目中需要替换的内容全部用步骤3中的方法替换就行了,这样国际化就完成了。

Spring MVC与easyui国际化的更多相关文章

  1. Spring MVC + Velocity实现国际化配置

    国际化介绍 web开发中,国际化是需要考虑的一个问题,而且这个问题一般是越早敲定越好(不然等到系统大了,翻译是个问题).下面是结合实际项目(Spring MVC+Velocity)对实现国际化的一些总 ...

  2. spring MVC +freemarker + easyui 实现sql查询和执行小工具总结

    项目中,有时候线下不能方便的连接项目中的数据源时刻,大部分的问题定位和处理都会存在难度,有时候,一个小工具就能实时的查询和执行当前对应的数据源的库.下面,就本人在项目中实际开发使用的小工具,实时的介绍 ...

  3. spring mvc综合easyui点击上面菜单栏中的菜单项问题

    采用easyui的tree报错发生的背景后,会弹出一个窗口,有一个问题是,当你点击顶部   解决方案,如下面(运用easyui1.36): /home/cyz/workspace/hb_manager ...

  4. EasyUI + Spring MVC + hibernate实现增删改查导入导出

    (这是一个故事--) 前言 作为一个JAVA开发工程师,我觉得最基本是需要懂前端.后台以及数据库. 练习的内容很基础,包括:基本增删改查.模糊查询.分页查询.树菜单.上传下载.tab页 主管发我一个已 ...

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

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

  6. [Spring MVC] - 从数据库读取MessageSource

    Spring MVC中使用MessageSource默认是写在properties文件当中,以支持国际化. 但很多时候我们需要把数据写到数据库当中,而不是在properties文件当中,以方便日常维护 ...

  7. Spring MVC学习纲要

    感慨一下 之前用过Spring MVC, MyBatis,但是很久不用之后发现很多知识点都荒废了,毕竟工作就是重复,重复再重复.没有啥新东西.所以还是找个时间把忘了的东西捡起来.万一搞了个大bug,然 ...

  8. 使用Spring MVC 实现 国际化

    使用Spring MVC 实现 国际化     博客分类: Spring   1. 版本 Spring 3.1   2. 配置 LocaleResolver     LocaleResolver 是指 ...

  9. spring mvc 国际化

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

随机推荐

  1. PHP版根据经纬度和半径计算出经纬度的范围

    百度地图提供了范围搜索的功能,但是它使用的是百度自己的数据,但是有时候我们需要使用自己的数据,显示在地图上.比如给定两个参数:指定位置(某一处的经纬度lnglat)和搜索半径(r),搜索指定范围内的数 ...

  2. ios uitableviewcell动态计算高度

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...

  3. [xPlugins] 开发中常用富文本编辑器介绍

    富文本编辑器学习,常见富文本编辑器有: CKeditor(FCkeditor).UEditor(百度推出的).NicEdit.KindEditor CKEditor 即 FCKEditor FCKed ...

  4. 自定义EditText实现一键删除数据

    转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 自定义EditText带删除小图标, 实现的功能: 点击删除小图标,删除当 ...

  5. python 基础——实现一个带缓存功能的函数

    from functools import wraps def cache(func): data = {} @wraps(func) def wrapper(*args): if args in d ...

  6. 正则转nfa:bug消除

    正则到nfabug的解决方法 前面提到了这个bug,为了解决这个bug,我们必须在每次引用到一个假名的时候,都构建一个拷贝.现在假设我们遇到了一个假名,并得到了他的开始节点和结束节点,当前的难题就是构 ...

  7. WPF全球化与本地化 (二)

    Visual Baml Visual Locbaml is a free and open-source software to simplify the task of WPF applicatio ...

  8. Freebsd 编译内核

    # cd /usr/src/sys/i386/conf # cp GENERIC GENERIC.20060812# ee GENERIC 如果要加入ipf防火墙的话则加入options        ...

  9. poj 1821 动态规划

    思路:每次枚举每个工人的右边界j,维护最优的左边界k.那么dp[j]=max(dp[j],dp[k]+(j-k)*w[i].p): 对于每个工人的初值k=w[i].s-1; 令x=j-w[i].l,如 ...

  10. Person

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace PersonD ...