zoukankan      html  css  js  c++  java
  • struts2对properties资源的处理

    struts2对properties资源的处理

    做了一些功能增强

    包括:

    可以读取项的描述

    可以读取项所在的行号,文件路径等

    实现方式

    继承了java的java.util.Properties实现了一个类LocatableProperties完成此事。

    LocatableProperties对外公布了load api完成properties文件的读取,但内部逻辑还是靠其自定义的PropertiesReader完成的。

    PropertiesReader继承自java的java.io.LineNumberReader,主要利用其原有的构造方法readLine等方法。

     

    LocatableProperties在构造时或者构造完成后,需要向其传递Location对象(包括文件描述,文件路径等信息),否则LocatableProperties是没法知道文件文件位置的。

     

    测试类 

    LocatablePropertiesTest

     

    z

     1 @Test
     2     public void testLocatableProperties001()
     3     {
     4         try
     5         {
     6             String propertiesPath = "/cn/chenxiaguang/demo/sss/xwork2/util/location/testConfigData.properties";
     7             Location loc = new LocationImpl("测试配置文件", this.getClass().getResource(propertiesPath).toString());
     8             LocatableProperties locatableProperties = new LocatableProperties(loc);
     9             locatableProperties.load(this.getClass().getResourceAsStream(propertiesPath));
    10             System.out.println(locatableProperties.getPropertyLocation("a"));
    11             System.out.println(locatableProperties.getPropertyLocation("b"));
    12         }
    13         catch (IOException e)
    14         {
    15             e.printStackTrace();
    16         }
    17     }

    testConfigData.properties

    #test a
      a=1
    
    #test b
    b=2

    但是在打印 时并不会打印出properties文件的描述信息。

    打印结果信息如下:

     

    #test a

     - file:/Users/simon/600.self/05.code/04.java/10.struts2-src-study/struts2-src-study/WebContent/WEB-INF/classes/cn/chenxiaguang/demo/sss/xwork2/util/location/testConfigData.properties:3:0

     

    #test b

     - file:/Users/simon/600.self/05.code/04.java/10.struts2-src-study/struts2-src-study/WebContent/WEB-INF/classes/cn/chenxiaguang/demo/sss/xwork2/util/location/testConfigData.properties:6:0

     

    当然 struts2还对xml文件等做了信息定位,主要使用sax api的特性完成。

     

  • 相关阅读:
    .net 下webservice 的WebMethod的属性
    做一个项目,平时都用到哪些工具提高效率(James Li)
    Android之解析Android Map地图返回的Json数据
    歌词文件LRC的解析,可用于音乐播放器实现歌词同步操作
    Android之创建程序快捷方式
    Android之Bitmap使用心得(持续更新)
    Socket编程之旅(服务器与客户端沟通)
    Android之应用自定义相机拍照并且对拍照文字(英文)进行识别
    android之App widget实际应用Demo
    Android之创建实时文件夹
  • 原文地址:https://www.cnblogs.com/simoncook/p/6920741.html
Copyright © 2011-2022 走看看