【问题标题】:jsf 2.2 get the path for a propertie filejsf 2.2 获取属性文件的路径
【发布时间】:2014-02-13 22:09:54
【问题描述】:

我想从我的 jsf 2.2 项目中的属性文件中读出。我使用 Eclipse 开普勒。

我尝试在包含 de.exanple 包的文件夹 src 中的 java-bean 中使用它。 bean 的文件名为 PageServiceBean.java。

属性文件位于WEB-INF/resources/prop 文件夹中。属性文件称为 config.properties。

我已经读到我可以使用javax.faces.WEBAPP_RESOUCRES_DIRECTORY 参数名称和/WEB_INF/resoucres 之类的参数值更改web.xml 文件中jsf 2.2 中的资源文件夹

但我没有得到配置文件的路径。 你能告诉我在哪里可以得到路径名。我想我必须使用相对路径名。 你能帮帮我吗?

更新

我执行你喜欢的第二个代码片段:

private Properties getProperties() {
        Properties prop = new Properties();
        try {
            //load a properties file
            prop.load(new FileInputStream("config2.properties"));
        } catch(Exception e) {

        }
        return prop;
    }

    public void setProperty1(Integer value) {
        Properties prop = getProperties();
        prop.setProperty("ort", value.toString());
        try {
            prop.store(new FileOutputStream("config2.properties"), null);
            Properties prop2 = getProperties();                 
        } catch (IOException ex) {
            Logger.getLogger(PageServiceBean.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

有效!我使用Properties prop3 = getProperties(); 来读取属性文件config2.properties。该文件存储在 Eclipse 主路径 ECLIPSE_HOME = C:\elipse_jee\eclipse 中。我可以将路径更改为特定路径,例如WEB_INF/resources吗?

【问题讨论】:

    标签: jsf jsf-2 path properties-file webcontent


    【解决方案1】:

    ServletContext#getResourceAsStream() 及其 JSF 委托人ExternalContext#getResourceAsStream() 提供 Web 内容资源。所以,应该这样做:

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    prop.load(ec.getResourceAsStream("/WEB-INF/resources/prop/config2.properties"));
    

    另见:

    【讨论】:

    • 谢谢。但是在我阅读下面的链接之后。我认为使用prop.store() 函数最好将属性文件放在类路径中。你也相信吗?
    • 在固定磁盘文件系统路径上。
    • 它将部署在服务器上。
    • 我不确定你在暗示什么。当然,您会在 Web 服务器上部署 Web 应用程序。
    • 我将项目构建为 maven 项目。我读到的属性文件存储在 src/main/resources 文件夹中。我可以使用代码读取文件 config2.properties:ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); prop.load(ec.getResourceAsStream("config2.properties")); 或者我需要路径吗?
    【解决方案2】:

    我会向您展示我的方法来满足您的需求,但我不会尝试回答您的问题。

    为了在 JEE 应用程序中使用属性文件,我创建了一个无状态 bean,它为应用程序的其余部分提供属性的 getter 和 setter。只有这个 EJB 会访问服务器中的属性文件,我使用 java.util.Properties。

    private Properties getProperties() {
        Properties prop = new Properties();
        try {
            //load a properties file
            prop.load(new FileInputStream("config.properties"));
        } catch(Exception e) {
    
        }
        return prop;
    }
    

    获得特定属性的访问方法后:

    public Integer getProperty1() {
        Properties prop = getProperties();
        String value = prop.getProperty("myProperty1Name");
        if(value != null) {
            return Integer.parseInt(value );
        }
        return 0;
    }
    
    public void setProperty1(Integer value) {
        Properties prop = getProperties();
        prop.setProperty("myProperty1Name", value.toString());
        try {
            prop.store(new FileOutputStream("config.properties"), null);
        } catch (IOException ex) {
            Logger.getLogger(PropertiesManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    在这种方法中,如果文件不存在,它将被创建。不过,属性的默认值将被硬编码。对于这种方法,您的文件放置在哪里并不重要。实际位置将取决于您的 JEE 服务器配置、域配置、应用程序部署文件等。

    【讨论】:

    • 感谢您的回答。但是当我在 PageServiceBean 中执行此操作时,我在您的第一个代码片段中找不到 config.properties 文件。我的问题是路径,我写在prop.load(new FileInputStream("config.properties"));
    • 它将在您的磁盘中的某个位置创建,关键是它放置在哪里并不重要。如果您需要进行备份,请在磁盘中运行搜索以找到它。在 glassfish 服务器中,此文件将在 docroot 参数中指定的路径中创建。
    • 谢谢。但我使用的是 tomcat 7 服务器。
    • 如果您实现了我的代码,只需调用 setProperty1 方法并在磁盘中的 config.properties 文件中运行搜索/定位/查找。我将查看 tomcat 7 服务器文档...
    • 看这个关于tomcat 7配置文件的问题。它可能允许你指定一个路径:stackoverflow.com/questions/7276989/…
    猜你喜欢
    • 2012-03-11
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多