在哪里放置资源? [英] Where to put resources?
本文介绍了在哪里放置资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要向我的应用程序添加一些属性文件.我已将此文件添加到controller
目录,但无法加载它们(在classpath中为否)-InputStream
为null.可以在哪里放置此文件?
I need to add some properties file to my application. I have added this file to controller
directory, but can't load them (no in classpath ?) - InputStream
is null. Where to put this file to can be accessed ?
public class Application extends Controller {
static {
try {
Properties p = new Properties();
InputStream in = Application.class.getClassLoader().getResourceAsStream("accounts.properties");
if(in != null) {
p.load(in);
in.close();
} else {
error("null inputstream");
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Actions below
// ...
}
推荐答案
您必须将其放入Play应用的conf
文件夹中.
You have to put it in the conf
folder of your Play app.
您还可以使用conf
目录中的子文件夹.
You can also use subfolder in the conf
directory.
例如:
conf/foo/bar.txt
可以使用以下方式访问
:
can be accessed using:
InputStream in = MyClass.class. getResourceAsStream("/foo/bar.txt")
您还可以通过更新project/Build.scala
文件并添加以下内容来在应用中添加自定义资源目录:
You can also add a custom resources directory in your app, by updating your project/Build.scala
file and adding:
val main = play.Project(appName, appVersion, appDependencies).settings(
...
resourceDirectory in Compile <<= baseDirectory / "myresources"
...
)
这篇关于在哪里放置资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文