zoukankan      html  css  js  c++  java
  • ApplicationContext更强的功能-学习笔记

    ---恢复内容开始---

    一、国际化支持

    二、资源访问

    三、事件传递

    国际化支持

    ApplicationContext继承了org.springframework.context.MessageResource接口,使用getMessage()的各个方法来取得信息资源

    从而实现国际化信息的目的。getMessage()有三个方法:

    1)String getMessage(String code,Object[] args,String default,Locale loc)

    2)String getMessage(String code,Object[] args,Locale loc)

    3)String getMessage(MessageSourceResolvable resolvable,Locale locale),通过MessageSourceResolvable来传入需要获取信息的代号

    当ApplicationContext被加载的时候,它会自动查找在XML中定义的messageSource,Spring约定这个Bean必须被定义为messageSource.

    开发人员可以通过org.springframework.context.support.ResourceBundleMessageSource来取得国际化信息。

    实例如下:

    1)定义Spring的配置文档config.xml

    2)定义存放信息资源的文档

    3)编写测试程序,查看输出

    1.config.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">

              <!--国际化支持的定义在文件名为message的文件夹中-->

          <value>messages</value>

        </property>

      </bean>

      <bean id="HelloWorld" class="com.gc.action.HelloWorld"></bean>

      <bean id="date" class="java.util.Date"/> 

    </beans>

    存放信息资源的文档的名称为messages.properties或messages.class

    2.用记事本编写存放信息资源的文档messages.properties.

    HelloWorld=问候语:{0}  问候时间:{1}

    {0} {1} 用来标识当从外部传入参数时,传入值存放的位置

    把messages.properties存放在ClassPath下,即myAppWEB-INFsrc下

    3.测试程序TestHelloWorld.java

    //**********TestHelloWorld.java*********

    package com.gc.test;

    import java.util.Calendar;

    import java.util.Locale;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.FileSystemXmlApplicationContext;

    import com.gc.action.HelloWorld;

    public class TestHelloWorld{

      public static void main(String[] args) throws InstantiationException,IllegalAccessException,

    ClassNotFoundException{

      //通过ApplicationContext获取配置文档

      ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

      //设定当前时间

      Object[] objs=new Object[] {"HelloWorld",Calendar.getInstance().getTime()};

      //国际化支持

      String msg=actx.getMessage("HelloWorld",objs.Local.CHINA)

      System.out.println(msg);

    }

    }

    说明:

    1.objs是一个数组,用来存放要传入的内容,数组中的内容分别对应与messages.properties中的{0}和{1}

    2.输出的结果,中文为乱码:因为JAVA本身在转码过程中出现了问题

    解决方法:

    cmd中输入cd\回到C盘根目录,然后输入native2ascii messages.properties messages.txt

    查看C盘生成的messages.txt文件,复制内容到messages.properties中,覆盖原来的就可以

    在myApp/WEB-INF/src下用同样的方法新建一个message_en_US.properties文件输入:HelloWorld=Language{0} Time{1}

    修改测试程序TestHelloWorld,将Locale.CHINA改为Locale.US

    总结:只需要修改Locale就可以很容易地实现对国际化的支持

    资源访问

    ApplicationContext继承了ReourceLoader接口,开发人员可以使用getResource()方法并指定资源文件的URL来获取

    ApplicationContext对资源文件的存储在设定资源文件的路径上有如下3种方式:

    1.虚拟路径来存取;

    2.绝对路径来存取;

    3.相对路径来存取。

    1.资源文件位于CLASSPATH下:classpath: 是Spring约定的URL虚拟路径

    ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

    Resource resource=actx.getResource("classpath:messages.properties");

    2.实际路径:file:或http:

    ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

    Resource source = actx.getResource("file:d:/eclipse/workspace/myApp/WEB-INF/src/messages.properties");

    3.相对路径

    ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

    Resource source = actx.getResource("WEB-INF/src/messages.properties");

    当取得一个Resource后,开发人员可以使用:

    getFile() 来存取资源文件内容

    exists()来检查资源文件是否存在

    isOpen()检查资源文件是否被打开

    getURL()返回资源文件的URL

    事件传递:  待续...

    ---恢复内容结束---

  • 相关阅读:
    Balance的数学思想构造辅助函数
    1663. Smallest String With A Given Numeric Value (M)
    1680. Concatenation of Consecutive Binary Numbers (M)
    1631. Path With Minimum Effort (M)
    1437. Check If All 1's Are at Least Length K Places Away (E)
    1329. Sort the Matrix Diagonally (M)
    1657. Determine if Two Strings Are Close (M)
    1673. Find the Most Competitive Subsequence (M)
    1641. Count Sorted Vowel Strings (M)
    1679. Max Number of K-Sum Pairs (M)
  • 原文地址:https://www.cnblogs.com/victoria-c/p/5642900.html
Copyright © 2011-2022 走看看