`

springframework【4】

阅读更多

这里说的国际化不是简单的web层面的,也有纯后台层面的。因为spring不只是处理web的,也可以处理纯java的后台程序。这里也就是说aop、ioc、DI的使用是在整个java层面的而不是只局限于web项目中。

 

1、资源取得

资源的取得主要应用于国际化处理。在写程序的时候可以以不同的方式获取资源

例如:

       //直接从classpath中将文件信息取出,具体化称为一个资源文件对象

       Resource resource = ctx.getResource("classpath:admin.properties");

       //从指定的硬盘路径中取得文件,具体实例化称为一个资源文件。

       Resource resource1 = ctx.getResource("file:c:/admin.properties");

 上边的“classpath:”是spring自制的URL虚拟协定,这会取得一个org.springframework.core.io.ClasspathResource 实例,代表一个具体的资源文件。上边程序中,该资源文件是位于Classpath的根目录中,文件名称为admin.properties,你也可以指定标准的URL,像“file:”或“http:”

 

2、获取国际化资源的信息的方法:

 A、在classpath中定义国际化文件 messages_zh_CN.properties,messages_en_US.properties

  例如:

hello=用户{0}于{1}登陆系统

hello=user {0} login in system when {1}

 B、在配置文件list.xml中声明实现一个ResourceBundleMessageSource来取得国际化消息

 例如:

 

  <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

  <property name="basename" value="messages"></property>

  </bean>

 

 此处bean的id必须是messageSource

 C、编写测试程序

 

import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
//资源的取得和国际化
public class TestRes {
	public static void main(String[] args){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("list.xml");
		Object[] arguments = new Object[]{"管理员",java.util.Calendar.getInstance().getTime()};
		System.out.println(ctx.getMessage("hello",arguments ,Locale.US));
		System.out.println(ctx.getMessage("hello",arguments ,Locale.CHINA));
	}
}

 执行结果:

user 管理员 login in system when 3/15/09 10:10 AM
用户管理员于09-3-15 上午10:10登陆系统

 

总结下:

 1、获取资源的方式有多样的

 2、我们可以直接通过在spring的配置文件中声明国际化文件的名字,这里声明为messages,但是也要同时建立相对应的国际化文件messages_en_US.properties、messages_zh_US.properties

 3、只要传送指定参数,指明要处理的语言,spring自动为我们将国际化文件中的内容取得,并且你还可以给国际化文件中的“占位符”传送参数

 

 

 

分享到:
评论

相关推荐

    Spring Framework 4.x Reference Documentation 中文翻译

    Spring Framework 4.x Reference Documentation 中文翻译 Chinese translation of the Spring Framework 4.x Reference Documentation ...

    Spring Framework 4 参考文档中文版.docx

    纯手工制作,spring4 中文文档 2. Spring Framework介绍 2.1 依赖注入和控制反转 2.2 模块 ... 2.3 使用场景

    spring framework4

    spring4 开发jar包 Introduction The Spring Framework provides a comprehensive programming and configuration model for modern Java-based ...JDK 6+ for Spring Framework 4.x JDK 5+ for Spring Framework 3.x

    SpringFramework4

    详细描述Spring框架,自带书签,便于阅读,更好地使用Spring框架

    spring framewok 4参考文档

    《Spring Framework 4.x参考文档》中文翻译

    spring framework 中文参考手册

    spring framework 中文参考手册

    Getting started with Spring Framework: covers Spring 5(epub)

    Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...

    org.springframework.flex-1.0.3.RELEASE.jar.zip

    org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包,org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3....

    Spring Framework API文档

    Spring Framework API文档。Spring是什么呢?首先它是一个开源的项目,而且非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据...

    org.spring-framework-3.0.4. 所有jar

    org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....

    Spring Framework Reference Documentation 4.25

    spring 4.25文档

    spring-framework-2.0.2

    org\springframework\aop org\springframework\beans org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\spring...

    Spring Framework 5.1.0源码

    Spring Framework 5.1.0源码 The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key ...

    Spring_Framework_ API_5.0.5 (CHM格式)

    Spring5 是一个重要的版本,距离SpringFramework4差不多四年。在此期间,大多数增强都是在 SpringBoot 项目中完成的。在本文中,我们将很快了解到Spring5发行版中的一些令人兴奋的特性。 1. 基准升级 要构建和运行...

    SpringFramework_v5.0.7.zip

    Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...

    Spring Framework v5.0.11

    Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...

    Spring Framework 4.0.2.RELEASE API

    Spring Framework 4.0.2.RELEASE API,该文档来自官网经本人制作而成方便使用的chm格式。

    org.springframework.core.jar

    org.springframework.core.jar

    Apress.Introducing.Spring.Framework.A.Primer.

    This book is an introduction to the well-known Spring Framework that offers an inversion of control container for the Java platform. The Spring Framework is an open source application framework that ...

Global site tag (gtag.js) - Google Analytics