复习Spring第四课---Spring对国际化的支持
其实国际化这东西很少使用,之前也就是粗略的学了下,趁今天有空,拿出来稍微写写。以前学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对国际化的支持的更多相关文章
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...
- Spring(四)Spring与数据库编程
Spring最重要的功能毫无疑问就是操作数据.数据库的百年城是互联网编程的基础,Spring为开发者提供了JDBC模板模式,那就是它自身的JdbcTemplate.Spring还提供了Transact ...
- 攻城狮在路上(贰) Spring(四)--- Spring BeanFactory简介
BeanFactory时Spring框架最核心的接口,它提供了高级IoC的配置机制,使管理不同类型的Java对象成为了可能.我们一般称BeanFactory为IoC容器.BeanFactory是Spr ...
- 深入浅出Spring(四) Spring实例分析
上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Sprin ...
- 玩转spring mvc(四)---在spring MVC中整合JPA
关于在Spring MVC中整合JPA是在我的上一篇关于spring mvc基本配置基础上进行的,所以大家先参考一下我的上一篇文章:http://blog.csdn.net/u012116457/ar ...
- Spring点滴四:Spring Bean生命周期
Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...
- [置顶] 深入浅出Spring(四) Spring实例分析
上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Sprin ...
- Spring ( 四 )Spring的AOP动态代理、切面编程
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.AOP切面编程 1.什么是AOP AOP是面向切面编程.全称:Aspect Oriented Pro ...
- spring学习四:Spring中的后置处理器BeanPostProcessor
BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...
随机推荐
- font 和 text ,cursor
font:14px/30px/"宋体"这种写法等于 font-size="14px" line-heigiht="30px" font-fa ...
- 都 2021 年了,竟然有人搞大数据时忽略 JSON 而去研究用 C# 把 XML 转换为 XML 的技术
在大数据项目开发过程中,ETL(Extract-Transform-Load)是必不可少.即便目前 JSON 非常流行,开发人员也有必定会有对远古系统的挑战,而 XML 格式的数据源作为经典存在浑身上 ...
- [bug] ERROR: Can't get master address from ZooKeeper; znode data == null
排错 访问bigdata111:50070没显示 jps发现hdfs的namenode没启动 查看namenode日志发现9000端口被占用 查找占用端口的进程 杀死进程,或在配置文件中更改端口号 参 ...
- [web] 系统运维--单机
处理过程 浏览器发送请求经过网络到达web服务器 web服务器处理请求并响应数据 响应数据从web服务器发送到用户端 用户浏览器接收数据,本地计算渲染 指标 响应时间 吞吐量 响应时间 响应时间 = ...
- [Web] 网络安全(SSH SSL HTTPS)
概念 SSH(Secure Shell) 一种安全通信协议 为shell提供加密通信服务 使用了非对称加密和对称加密 对称加密(Symmetric-Key Encryption):只用一个密钥来进行加 ...
- .jnlp 文件打开方式
.jnlp 文件打开方式 jnlp文件打开需要安装jre ,java环境,通过java环境运行即可,下面介绍详细步骤 1.下载.安装最新版jre环境,直接下一步即可 2 java配置 打开控制面板,查 ...
- Docker——Registry搭建私有镜像仓库
前言 在 Docker 中,当我们执行 docker pull xxx 的时候,它实际上是从 registry.hub.docker.com 这个地址去查找,这就是Docker公司为我们提供的公共仓库 ...
- 云计算OpenStack---虚拟机获取不到ip(12)
一.现象描述 openstack平台中创建虚拟机后,虚拟机在web页面中显示获取到了ip,但是打开虚拟机控制台后查看网络状态,虚拟机没有ip地址,下图为故障截图: 二.分析 1.查看neutron服务 ...
- IDEA 安装 zookeeper 可视化管理插件
1. 安装 zookeeper 插件 打开 IDEA->Settings->Plugins,然后在 Marketplace 输入 "zookeeper" 如下: 插件安 ...
- mysql8 安装配置教程
第一步 下载安装包 MySQL 是甲骨文(Oracle)公司产品,可以到官网上下载 MySQL: 官网下载地址:https://dev.mysql.com/downloads/mysql/ 如果嫌弃官 ...