在Tomcat的GWT中加载属性文件 [英] Loading properties file in GWT on Tomcat

查看:161
本文介绍了在Tomcat的GWT中加载属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道那里有很多类似的问题,但没有哪一个帮助。
我们有一个在tomcat服务器上运行的GWT应用程序,它正在加载数据库连接的属性文件。



加载属性文件的代码如下所示:

  private static final String DB_CONF =com / x / monitorui / server / configuration.properties; 
属性properties = new Properties();
properties.load(DatabaseConnection.class.getClass()。getResourceAsStream(DB_CONF));

在Jetty上进行本地开发时,可以正常工作,但不能在Tomcat上部署。它似乎无法找到资源。
文件本身位于 war / WEB-INF / classes / com / x / monitorui / server 中,尝试加载它的类位于相同的位置如果您的属性文件与您用于调用getResourceAsStream(..)的类位于相同的目录中,那么您可以使用getResourceAsStream(..) ,那么你只需要文件的名称,而不需要绝对路径。
例如,假设你有:

  //注意你的版本应该将它从源目录复制到你的/ WEB-INF / classes / ....目录,因此你可以做一个干净的生成
com / x / monitorui / server / configuration.properties

和位于相同目录的类,即。 in:

  com.x.monitorui.server.DatabaseConnection 

然后您可以简单地调用

  Properties properties = new Properties( ); 
properties.load(DatabaseConnection.class.getResourceAsStream(configuration.properties));


I know there are lots of similar questions out there, but none of which that helped. We have a GWT application running on a tomcat server, which is loading a properties file for database connections.

The code to load the properties file looks like following:

private static final String DB_CONF = "com/x/monitorui/server/configuration.properties";
Properties properties = new Properties();
properties.load(DatabaseConnection.class.getClass().getResourceAsStream(DB_CONF));

This works fine whilst developing locally on the Jetty, but not when deployed on the Tomcat. It can't seem to find the resource. The file itself is located in war/WEB-INF/classes/com/x/monitorui/server, the class trying to load it is located in the same package.

解决方案

If you properties file is in the same directory as the class you use to call "getResourceAsStream(..)", then you just need the name of the file, without the absolute path to it. For example, supposing you have:

// notice that your build should copy this from the source directory to your /WEB-INF/classes/.... directory automatically so you can do a clean build
com/x/monitorui/server/configuration.properties

and a class located at the same directory, ie. in package:

com.x.monitorui.server.DatabaseConnection

then you can simply call

Properties properties = new Properties();
properties.load(DatabaseConnection.class.getResourceAsStream("configuration.properties"));

这篇关于在Tomcat的GWT中加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆