EclipseのJSF Webアプリケーションプロジェクトで、すべてのサーバーとデータベースの接続を含むプロパティファイルを読み書きしようとしています。コンソールに書き込むためにlog4jを使用していますか?私のconfig.propertiesファイルは次のとおりです。Web Appプロジェクトでプロパティファイルが更新されないのはなぜですか?
dbserver=localhost
dbname=mydatabase;instance=myinstance
dbuser=myuser
dbpassword=mypassword
私は(これは右のクラスパスですか?)/ WebアプリケーションにWEB-INF/classesフォルダを私のconfig.propertiesファイルを置きました。ファイルを削除するとファイルが破損するため、この特定の場所でファイルを正しく読み取っていることを確認しました。
私のマネージドBeanでは、config.propertiesファイルを読み書きする機能があります。
public void getSettings() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("config.properties");
Properties properties = new Properties();
try {
properties.load(input);
this.server = properties.getProperty("dbserver");
this.db = properties.getProperty("dbname");
this.user = properties.getProperty("dbuser");
this.pass = properties.getProperty("dbpassword");
logger.info("Config file successfully loaded!");
} catch (IOException e) {
logger.error("Loading Database Settings Error with " + e);
} finally {
if (input != null) {
try {
input.close();
logger.info("Closing config file...");
} catch (IOException e) {
logger.error("Error closing config file with " + e);
}
}
}
}
public void saveSettings() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
OutputStream out = null;
try {
props.setProperty("dbserver", this.server);
props.setProperty("dbname", this.db);
props.setProperty("dbuser", this.user);
props.setProperty("dbpassword", this.pass);
URL url = classLoader.getResource("config.properties");
File file = null;
try {
file = new File(url.toURI().getPath());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// File f = new File("config.properties");
out = new FileOutputStream(file);
props.store(out, "This is an optional header comment string");
logger.info("Config file successfully saved!");
} catch (IOException io) {
logger.error("Saving configuration properties failed error with : " + io.getMessage());
} finally {
if (out != null) {
try {
logger.info("Closing config file...");
out.close();
} catch (IOException e) {
logger.error("Failed closing configuration properties file error with : " + e);
}
}
}
}
プロパティファイルからの読み込みに問題はありませんでしたが、ファイルに書き込むのに苦労しました。この問題は、私が「192.168.1.1」には「localhost」からサーバー名を変更した場合、私は新しい情報が、私はページを更新するかにもかかわらず持続することが確認でき、
今URL url = classLoader.getResource("config.properties");
を指定することで解決されているように見えましたサーバーを再始動してください。私はconfig.propertiesファイルを開いたとき、私は情報がどこかにファイルが残っていても存続しているようだ
dbserver=192.168.1.1
を見て期待していたときに...しかし、私はまだ
dbserver=localhost
を見ます同じ?私の所有物の内容にどのようにアクセスして変更を見ることができますか? WARファイルの変更
は
JSFはこのようにプロパティファイルを(再)ロードすることについて何も知りません。だからあなたの質問はjsf関連ではありません。 _ "私は自分のconfig.propertiesファイルをwebapp/WEB-INF/classesフォルダに配置しました(これはクラスパスの権利ですか?)。 srcフォルダ(src/main/resources、Mavenを使用している場合など)にある可能性が最も高い場所 – Kukeltje
ありがとうございました。まだあなたが言ったことを消化しようとしていますが、それは私の目を開けました。 –