Java 如何将 .properties 文件加载到 jsp 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1140653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load a .properties file into a jsp
提问by morgancodes
I've gotten as far as this:
我已经到了这个程度:
private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream("channelLogos.properties"));
where channelLogos.properties is in the same directory as my JSP. I get a FileNotFound exception. Where does my app actually think I mean by "channelLogos.properties", if not the same directory as the JSP? How can I determine the correct path to load the properties file?
其中 channelLogos.properties 与我的 JSP 位于同一目录中。我收到 FileNotFound 异常。如果不是与 JSP 相同的目录,我的应用程序实际上认为我的意思是“channelLogos.properties”在哪里?如何确定加载属性文件的正确路径?
回答by pkaeding
Take a look at ServletContext.getRealPath(). That should give you the full path to the properties file.
看看ServletContext.getRealPath()。这应该为您提供属性文件的完整路径。
回答by Don Branson
I'd like to highlyrecommend reading about Model 2 Servlets. I recommend it to everyone who's still doing Model 1 Servlets, that is, doing "real work" in a JSP.
我强烈推荐阅读有关Model 2 Servlets 的文章。我向所有仍在使用 Model 1 Servlets 的人推荐它,即在 JSP 中做“真正的工作”。
As to your question: First, throw the properties file in your classpath, then read the file using getResourceAsSttream:
至于你的问题:首先,在你的类路径中抛出属性文件,然后使用 getResourceAsStream 读取文件:
Thread.currentThread().getContextClassLoader().getResourceAsStream("channelLogos.properties");
There are many options, of course, and everyone will have their favorite.
当然,有很多选择,每个人都会有自己喜欢的。
回答by victor hugo
This will do the job:
这将完成这项工作:
<%@page import="java.io.InputStream" %>
<%@page import="java.util.Properties" %>
<%
InputStream stream = application.getResourceAsStream("/some.properties");
Properties props = new Properties();
props.load(stream);
%>
Anyway I really think you should have the properties file in the classpathand use a servlet
无论如何,我真的认为您应该在类路径中拥有属性文件并使用 servlet
回答by ChssPly76
When you say "same directory as JSP", what exactly do you mean by that? That your JSP sits somewhere in, say, /mywebapp/somefolder/my.jsp
with mywebapp
being your application root and your properties file is /mywebapp/somefolder/channelLogos.properties
?
当您说“与 JSP 相同的目录”时,您到底是什么意思?那你的JSP坐在某处,说,/mywebapp/somefolder/my.jsp
有mywebapp
是你的应用程序的根,你的属性文件是/mywebapp/somefolder/channelLogos.properties
?
If so, then most likely they're NOT in the same directory. JSP has been compiled and where it is actually located may vary depending on servlet container. Your best bet is to use ServletContext.getRealPath()
as suggested by pkaedingwith properties file path relative to webapp context as an argument. Using the above example:
如果是这样,那么它们很可能不在同一目录中。JSP 已被编译,它的实际位置可能因 servlet 容器而异。您最好的选择是ServletContext.getRealPath()
按照pkaeding 的建议使用相对于 webapp 上下文的属性文件路径作为参数。使用上面的例子:
private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream(servletContext.getRealPath("/somefolder/channelLogos.properties")));
That said, keep in mind that if you insist on putting your properties in the same folder as JSP you should take care to restrict it from being publicly accessible (unless that's the intention).
也就是说,请记住,如果您坚持将您的属性放在与 JSP 相同的文件夹中,您应该注意限制它被公开访问(除非这是故意的)。
回答by VAYU
JSP runs in servlet container, so its current working directory is defined by the container. Typically it is the directory where container is installed or its bin directory. In any case it is not the place where you want to store your custom properties file.
JSP 运行在 servlet 容器中,因此它的当前工作目录由容器定义。通常它是安装容器的目录或其 bin 目录。在任何情况下,它都不是您想要存储自定义属性文件的地方。
There are 2 typical approaches to do what you need.
有两种典型的方法可以满足您的需求。
The first approach is good if your file is a part of your application and you never change it on deployment. In this case read it from resources:
如果您的文件是应用程序的一部分并且您在部署时从不更改它,则第一种方法是好的。在这种情况下,从资源中读取它:
props.load(getClass().getResourceAsStream())
props.load(getClass().getResourceAsStream())
or even better
甚至更好
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream())
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream())
*
*
The second approach is good if you want to change your properties file on deployment environment
如果您想在部署环境中更改属性文件,则第二种方法很好
*. In this case put it somewhere in the file system outside your container. For example /opt/mycompany/conf/myproperties.properties on linux or at any other location you like. Now you should use absolute path when creating FileInputStream.
*. 在这种情况下,将它放在容器外的文件系统中的某个位置。例如,Linux 上的 /opt/mycompany/conf/myproperties.properties 或您喜欢的任何其他位置。现在您应该在创建 FileInputStream 时使用绝对路径。
To make system better configurable you should not write the path to configuration file inside the code. Better approach is to pass it to application using system properties, e.g. add parameter like -Dmycompany.conf=/opt/mycompany/myprops.properties when you are running your application server. When you want to read the file do the following:
为了使系统更好地可配置,您不应在代码中写入配置文件的路径。更好的方法是使用系统属性将其传递给应用程序,例如在运行应用程序服务器时添加类似 -Dmycompany.conf=/opt/mycompany/myprops.properties 的参数。当您要读取文件时,请执行以下操作:
new FileInputStream(System.getProperties("mycompany.conf"))
新的 FileInputStream(System.getProperties("mycompany.conf"))
Now configuration of your system can controlled independently by deployer.
现在您的系统配置可以由部署者独立控制。
回答by Nancy
In properties file, properties are listed as below:
在属性文件中,属性列出如下:
label.esme.interface.dependenciesDelete = "Dependencies present. Cannot delete."
In case you are using Struts framework, it can be present in struts-config.xml like:
如果您使用的是 Struts 框架,它可以出现在 struts-config.xml 中,例如:
<message-resources parameter="pathToApplcnRsrc"/>
or struts.properties like:
或 struts.properties 像:
struts.custom.i18n.resources = ApplicationResource
Use a property from properties file like below in JSP:
在 JSP 中使用属性文件中的属性,如下所示:
'<s:text name="label.esme.interface.dependenciesDelete" />'