国际化(Internationalization)有时候被简称为i18n,因为有18个字母在国际化的英文单词的字母i和n之间。Spring对国际化的支持示例如下所示。

需要将spring.tld放到工程的lib目录下,这样才能JSP才能正常使用spring:message标签。

web.xml中设置加启动时就要载配置文件applicationContext.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

在applicationContext.xml中注册资源文件处理类。这里可以注册一个或者多个资源文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

<!-- <property name="basename" value="messages"/> --> 
   
    <property name = "basenames">
       <list>
        <value>messages</value>
        <value>messages2</value>
       </list>
    </property>
</bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/> </beans>

  messageSource指明了资源文件的路径,这里的路径是根目录下的名为messages的资源文件件。

  CookieLocaleResolver是资源文件处理类,资源文件处理类有以下三种:

  1. 基于session的

  <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

  2. 基于请求的

  <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/>
   3.基于cookie的

  <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

  以上三种处理器理论上配置任意一个就可以,使用第一种和第三种比较常见。

第一与第三个用法相同,只不过前者使用session,session过期就需要重新设置,而后者使用cookie,可以根据项目的具体情况进行选择。

创建资源文件messages_zh.properties和messages_en.properties:

messages_zh.properties内容如下:
main.title=你好 messages_en.properties内容如下:
main.title=Hello World!

JSP测试页面test.jsp如下:

<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Spring国际化</title>
</head>
<body>
<spring:message code="main.title" /><br>
<input type="button" value='<spring:message code="main.title" />'/><br>
</body>
</html>

在Java代码中使用的例子如下:

//FileSystemXmlApplicationContext与ClassPathXmlApplicationContext的区别,前者需要写路径
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(ctx.getMessage("main.title", null, Locale.getDefault())); System.out.println(ctx.getMessage("main.title", null, Locale.US));

Spring国际化的更多相关文章

  1. 关于Spring 国际化 No message found under code 的解决方案

    用spring做国际化时经常会报: org.springframework.context.NoSuchMessageException: No message found under code 'u ...

  2. 深入理解spring国际化

    深入理解spring国际化 转自http://blog.csdn.net/ethan_fu/article/details/45621337

  3. spring 国际化i18n配置

    i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件等)无需做大的改 ...

  4. spring 国际化-i18n

    i18n(其 来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版 物,软件,硬件等)无需做大 ...

  5. 【spring 国际化】springMVC、springboot国际化处理详解

    在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...

  6. Spring国际化模块

    1.Spring3.1.0实现原理分析(二).国际化(i18n) https://blog.csdn.net/roberts939299/article/details/69666291

  7. Spring 国际化

    http://www.xdemo.org/spring-i18n/ xxxx_zh_CN.properties中不允许保存中文,用jdk/bin目录下的native2ascii.exe将中文转化为Un ...

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

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

  9. Spring的国际化(转载)

    1:在MyEclipse下面创建一个test的Web  Project,然后添加Spring相关的文件,在src根目录下创建applicationContext.xml文件. applicationC ...

随机推荐

  1. c++(排序二叉树线索化)

    前面我们谈到了排序二叉树,还没有熟悉的同学可以看一下这个,二叉树基本操作.二叉树插入.二叉树删除1.删除2.删除3.但是排序二叉树也不是没有缺点,比如说,如果我们想在排序二叉树中删除一段数据的节点怎么 ...

  2. oracle 11g体系结构

    1.数据库体系结构 1.1 数据库和实例的关系database server  = instance 实例 + database 数据库instance 实例  =内存 memory + 进程  pr ...

  3. 试用最强Spark IDE--IDEA

    1.安装IntelliJ IDEA IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示 ...

  4. 二维数组模拟实现酒店管理系统-java

    业务分析 1.需要一个房间类,包含房间的属性,比如房间编号.房间类型.是否占用. 2.需要一个旅馆类,旅馆有房间,提供的方法需要有 预订房间.打印房间信息.初始化房间.退房. 3.测试类,测试预订房间 ...

  5. node学习笔记1——require参数查找策略

    require参数类型 http.fs.path等,原生模块 ./mod或../mod,相对路径的文件模块 /pathtomodule/mod,绝对路径的文件模块 mod,非原生模块的文件模块 mo ...

  6. 给织梦DEDECMS添加栏目图片与英文名显示

    开始做微网站了,不同于传统手机网站,因为微信上的微网站是支持CSS3与HTML5的,好吧,各种要学习的还有很多很多阿~这么多新代码,叹! 本来想转战帝国CMS了,奈何这名字太不对味了,PHPCMS也懒 ...

  7. PHP 获得当前页面所有变量常量的值

    get_defined_vars() - 返回由所有已定义变量所组成的数组,这个函数在二次开发的时候用起来非常给力: get_defined_constants();可以返回当前的所有常量 zend的 ...

  8. CCF系列之图像旋转(201503-1)

    试题编号: 201503-1时间限制: 5.0s 内存限制: 256.0MB 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表 ...

  9. Java数据持久层框架 MyBatis之API学习六(Mapper XML 文件详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  10. 记录linux tty的一次软锁排查

    本过程参照了某大侠的https://github.com/w-simon/debug/blob/master/tty_lock_cause_sytemd_hung , 当第二次出现的时候,还是排查了一 ...