其实国际化这东西很少使用,之前也就是粗略的学了下,趁今天有空,拿出来稍微写写。以前学android开发的时候,类似于多语言的版本。差别就是一个是手机打开,一个是浏览器打开,本质是一样的。

在Spring配置文件中的配置如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
4 xmlns:tx="http://www.springframework.org/schema/tx"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
9 <!--
10 Spring 通过ApplicationContext 容器的MessageSource 接口实现Sping 对国际化的支持。
11 这里的Bean id 必须是“messageSource”,因为Spring 在装配系统Bean 时会根据这个名字进行查找。
12 这样,我们便可以在程序中通过ApplicationContext 对象调用getMessage()方法获取资源文件的信息。
13 -->
14 <bean id="messageSource"
15 class="org.springframework.context.support.ResourceBundleMessageSource"
16 abstract="false" lazy-init="default"
17 autowire="default" dependency-check="default">
18 <property name="basenames">
19 <list>
20 <value>config/properties</value>
21 </list>
22 </property>
23 </bean>
24 </beans>

两个资源文件

properties_en_US.properties

message1=Hello {0},GoodBye{1}

properties_zh_CN.properties

message1=\u4f60\u597d{0},\u518d\u89c1{1}

至于\u4f60\u597d和\u518d\u89c1其实就是‘你好’和‘再见’的ASCII码值,在国际化资源文件中,中文汉字必须要配置成ASCII形式的,具体怎么得到这些值的呢

win+r-->运行窗口 输入native2ascii (前提是装了JDK了)

测试代码:

package com.lsd.test;  

import java.util.Date;
import java.util.Locale; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class CommonTest { @Test
public void test1() {
String path = "WebRoot/WEB-INF/applicationContext.xml";
ApplicationContext context = new FileSystemXmlApplicationContext(path);
Object[] objs = new Object[] { "lsd",
new Date().toLocaleString() };
// message1为资源文件的key值,objs为资源文件value值所需要的参数,Local.CHINA为国际化为什么语言
String str = context.getMessage("message1", objs, Locale.CHINA);
System.out.println(str);
}
}
</span>

在WEB页面上怎么实现呢,首先添加个sping.tld到WEB-INF/lib下,还需要spring-webmvc.jar

在这之后需在Spring配置文件中添上

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

在jsp页面上:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%>
3 <html>
4 <head>
5 <title>Spring国际化</title>
6 </head>
7 <body>
8 <spring:message code="message1" /><br>
9 <input type="button" value="<spring:message code="message1" />"/><br>
10 </body>
11 </html>

浏览器默认都是中文的,所以显示的结果肯定是中文的,显示英文只要更换下浏览器语言即可

复习Spring第四课---Spring对国际化的支持的更多相关文章

  1. Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)

    参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...

  2. Spring(四)Spring与数据库编程

    Spring最重要的功能毫无疑问就是操作数据.数据库的百年城是互联网编程的基础,Spring为开发者提供了JDBC模板模式,那就是它自身的JdbcTemplate.Spring还提供了Transact ...

  3. 攻城狮在路上(贰) Spring(四)--- Spring BeanFactory简介

    BeanFactory时Spring框架最核心的接口,它提供了高级IoC的配置机制,使管理不同类型的Java对象成为了可能.我们一般称BeanFactory为IoC容器.BeanFactory是Spr ...

  4. 深入浅出Spring(四) Spring实例分析

    上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Sprin ...

  5. 玩转spring mvc(四)---在spring MVC中整合JPA

    关于在Spring MVC中整合JPA是在我的上一篇关于spring mvc基本配置基础上进行的,所以大家先参考一下我的上一篇文章:http://blog.csdn.net/u012116457/ar ...

  6. Spring点滴四:Spring Bean生命周期

    Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...

  7. [置顶] 深入浅出Spring(四) Spring实例分析

    上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Sprin ...

  8. Spring ( 四 )Spring的AOP动态代理、切面编程

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.AOP切面编程 1.什么是AOP AOP是面向切面编程.全称:Aspect Oriented Pro ...

  9. spring学习四:Spring中的后置处理器BeanPostProcessor

    BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...

随机推荐

  1. Asp.NetCore Web开发之初始文件解析

    在写代码之前,有必要了解一下.net帮我们生成的文件都是干什么用的,在开发过程中他们都负责那些地方(下面以MVC模板举例). 先简单介绍一下什么是MVC,MVC(model-view-controll ...

  2. MySQL中几种常见的日志

    前言: 在 MySQL 系统中,有着诸多不同类型的日志.各种日志都有着自己的用途,通过分析日志,我们可以优化数据库性能,排除故障,甚至能够还原数据.这些不同类型的日志有助于我们更清晰的了解数据库,在日 ...

  3. 【2020BUAA软件工程】个人博客作业

    个人作业博客 项目 内容 北航2020软工 班级博客 作业要求 具体要求 我的课程目标 学习软件工程,掌握团队合作,锻炼自我 作业在哪个方面帮助我实现目标 通读<构建之法>,尝试理解软件工 ...

  4. 普里姆算法(Prim)

    概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图(带权图)里搜索最小生成树.即此算法搜索到的边(Edge)子集所构成的树中,不但包括了连通图里的所有顶点(Vertex)且其所有边的权 ...

  5. Duplicate entry '' for key 'PRIMARY'

    今天在在mysql中插入数据 因为直接插入查询出来的表格,insert into 表(student_id,class_id) 直接插入了这两个字段对应的查询出来的表 没有留意到该表的主键没有设置自增 ...

  6. Cookie&Session-授课

    1 会话技术 1.1 会话管理概述 1.1.1 什么是会话 会话:浏览器和服务器之间的多次请求和响应 为了实现一些功能,浏览器和服务器之间可能会产生多次的请求和响应,从浏览器访问服务器开始,到访问服务 ...

  7. Date类常用方法总结(构造|格式化输出|String转换|Long转换|计算间隔|比较)

    java.util.Date类 它重写了toString方法,new一个Date类直接输出是按照这样的格式 // "EEE MMM dd HH:mm:ss zzz yyyy"Fri ...

  8. curl: (35) SSL connect error

    curl: (35) SSL connect error weixin_34212762 2018-02-23 20:16:23  230  收藏 文章标签: 运维 版权 阿里云的机器,昨晚githu ...

  9. 单独跑ltp-20200508 ./runltp

    # cat r3.sh#!/bin/bash # cat r3.sh#!/bin/bashi=1for ((; i<=1000; i++))do/opt/ltp/runltp -s fmtmsg ...

  10. 001.Ansible简介与基本安装

    一 自动化运维 1.1 运维的自动化发展历程 IAAS:infratructure as a Service PAAS:platform as a Service 平台及服务 SAAS:softwar ...