【问题标题】:Where to put a properties file in Struts 2?将属性文件放在 Struts 2 的什么位置?
【发布时间】:2013-01-29 10:05:11
【问题描述】:

我有一个属性文件放在 Java 中的 Web 项目的根目录中。我正在使用 Struts 2。 我的代码无法读取属性文件。我应该将我的属性文件保存在哪里?

我已经检查了默认路径,它是我的 Eclipse 的安装位置。但我希望该系统应该从项目文件夹本身读取文件。

【问题讨论】:

    标签: properties struts2 localization


    【解决方案1】:

    通常您应该将属性文件放到src 文件夹中,以便您的应用程序能够在您运行应用程序时读取属性文件,属性文件从src 文件夹复制到classes 文件夹。据您所知,classes 文件夹应该是项目输出文件夹,因此它将用作classpath 文件夹,如果它位于classpath 上,应用程序可以加载属性文件。

    从类路径获取属性的示例:

    Properties prop = new Properties();
    
    try {
      //load properties from the class path
      prop.load(this.getClass().getClassLoader().getResourceAsStream("myproperties.properties"));
    
      //get the property 
      System.out.println(prop.getProperty("mykey"));
    
    } catch (IOException ex) {
      ex.printStackTrace();
      throw ex;
    }
    

    但是,如果您知道文件系统上文件的路径,则可以加载属性,在这种情况下使用

    prop.load(new FileInputStream("/path/to/myproperties.properties"));
    

    如果你说的是struts.properties

    该框架使用了许多可以更改以适应的属性 您的需求。要更改这些属性中的任何一个,请指定属性 struts.properties 文件中的键和值。属性文件可以是 定位在类路径上的任何位置,但它通常位于 /WEB-INF/类。

    如果您正在寻找消息资源属性,则可以在 struts.propertiesstruts.xml 中进行配置,后者提供。

    <constant name="struts.custom.i18n.resources" value="path/to/resources/MessageResources"/>
    

    值是文件路径src/path/to/resources/MessageResources.properties

    如果您正在寻找配置应用程序的正确方法,请考虑选择使用EasyConf

    【讨论】:

    • 谢谢,现在不用每次都硬编码属性文件的绝对路径了。 :)
    【解决方案2】:

    属性文件通常会:

    1. 在类路径上,例如,作为资源打开,或
    2. 在客户端无法访问的位置,例如,在/WEB-INF

    哪个更合适取决于您的需求。基于类路径的文件允许捆绑默认属性文件而无需配置。例如,Log4J 将在类路径的根目录中查找 log4j.properties 作为其默认配置文件。

    然而,这有时会导致问题,具体取决于类加载顺序:有时系统会拾取“杂散”配置文件。自己配置它可能仍然是可取的;我仍然倾向于类路径,但配置文件也常见于WEB-INF。任何一种方法都可以,并且两种样式都可以使用 JNDI、init 参数、环境变量或系统变量(例如,-D)进行配置。

    【讨论】:

      【解决方案3】:

      将 myPropertyFile.properties 文件保存在 src 文件夹中(构建项目后,您将在 WEB-INF/classes 中找到它)并使用以下代码访问它们:

        Properties prop = new Properties();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        prop.load(classLoader.getResourceAsStream("/myPropertyFile.properties"));
      

      【讨论】:

        【解决方案4】:

        理想情况下,您可以将属性文件保存在: /src/com/cft/web/bundle/. 喜欢, /src/com/cft/web/bundle/LabelResources.properties 或者 /src/com/cft/web/bundle/Applicationresources.properties。

        实际上,您可以根据自己的喜好提供任何路径。

        只要记住在 web.xml/struts-config.xml 中添加正确的完整路径

        对于 ex=g。 1. 在 web.xml 中:

        		<description>Application Resources</description>
        		<env-entry-name>ApplicationResources</env-entry-name>
        		<env-entry-value>com.cft.web.bundle.ApplicationResources</env-entry-value>
        		<env-entry-type>java.lang.String</env-entry-type>
        1. 在 Struts-config.xml 中

        &lt;message-resources parameter="com.cft.web.bundle.LabelResources" key="yourPropertiesFileName"/&gt;

        【讨论】:

        • 仔细阅读问题,OP正在使用Struts2
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-18
        • 2014-08-13
        • 1970-01-01
        • 2012-11-25
        • 2012-07-24
        • 1970-01-01
        相关资源
        最近更新 更多