0

私のjsf 2.2プロジェクトのプロパティファイルから読みたいです。私は日食ケプラーを使用しています。

パッケージde.exanpleを使用して、フォルダーsrcのjava-beanでこれを使用しようとしています。Bean のファイルは PageServiceBean.java と呼ばれます。

プロパティファイルはWEB-INF/resources/propフォルダにあります。プロパティ ファイルは config.properties と呼ばれます。

web.xmlファイルのjsf 2.2のリソースフォルダーを、javax.faces.WEBAPP_RESOUCRES_DIRECTORYparam名とparam値を次のように変更できることを読みました/WEB_INF/resoucres

しかし、設定ファイルへのパスがわかりません。パス名を取得できる場所を教えてください。相対パス名を使用する必要があると思います。手伝ってくれませんか?

アップデート

あなたの好きな 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か?

4

2 に答える 2

0

あなたのニーズに対する私のアプローチを紹介しますが、あなたの質問には答えようとしません。

JEE アプリケーションでプロパティ ファイルを使用するために、プロパティのゲッターとセッターを使用してアプリケーションの残りの部分を処理するステートレス Bean を作成します。この 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 サーバー構成、ドメイン構成、アプリケーション展開ファイルなどによって異なります。

于 2014-01-23T12:05:40.787 に答える