我有一些属性文件/WEB-INF.我想在JSF托管bean中加载它.有没有办法做到这一点?
我有一个Java Spring MVC Web应用程序.我使用application.properties文件来设置一些属性值.现在我试图让属性独立于war文件.我在tomcat下创建了一个新的文件夹shared/classes,如下所示.
在catalina.properties我添加的文件中shared.loader=${catalina.base}/shared/classes.
在我的root-context.xml文件中,我补充道
<context:property-placeholder location="file:${catalina.base}/shared/classes/application.properties"/>
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: E:\projects\smartwcm\workspace-20163101-eclipse-wp\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\shared\classes\application.properties (The system cannot find the path specified)
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
假设我有一个简单的"Hello world"类型servlet,配置了注释@WebServlet("/hello").
我想为构建/部署禁用它,因此无法"调用"servlet.我该怎么办?
然后,通过配置文件,我希望能够在运行时启用servlet ,以便客户端可以使用它.我该怎么办?
这些都可能吗?
我需要在JSF应用程序中获取资源文件.
InputStream input = new FileInputStream("filename.xml");
Run Code Online (Sandbox Code Playgroud)
但是,系统找不到filename.xml与sample.xhtml网页位于同一文件夹中的文件.我怎么得到InputStream它?
要properties使用Glassfishv3 webserver(位于root directory我的Web应用程序中)读取JSF2.0中的文件,我使用下面的代码 -
ServletContext ctx = (ServletContext) FacesContext
.getCurrentInstance().getExternalContext().getContext();
String deploymentDirectoryPath = ctx.getRealPath("/");
Properties prop = new Properties();
prop.load(new FileInputStream(deploymentDirectoryPath
+ File.separator + "portal-config.properties"));
Run Code Online (Sandbox Code Playgroud)
以下是门户网站的截图 -

运行门户网站时,我收到FileNotFound错误,因为该文件不存在于glassfish域中.
有没有办法读取属性文件,它既适用于情境,也适用于开发阶段和战争文件?
我正在尝试获取InputStream托管豆中的PNG文件,如下所示:
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
InputStream input = externalContext.getResourceAsStream("/myFile.png");
// input is null.
Run Code Online (Sandbox Code Playgroud)
但是,InputStream始终为null。这是怎么引起的,我该如何解决?
我试图在我的java Web应用程序中读取属性文件.我试过这些解决方案:
在基于servlet的应用程序中放置位置以及如何读取配置资源文件?
但它们都不适合我.
这是我的应用程序的结构:

读取属性文件的代码放在A类中,即使我放置绝对路径它也不起作用.A是普通的Java类.但是如果读取属性代码放在servlet类中(ProcessRequest.java),一切都像魅力一样
这是我用过的代码:
public class A {
public A() {
try {
Properties p = new Properties();
p.load(this.getClass().getClassLoader().getResourceAsStream("/a.properties"));
String n = p.getProperty("name");
System.out.println("name: " + n);
} catch (Exception ex) {
Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
java ×4
jsf ×3
servlets ×3
properties ×2
resources ×2
file ×1
glassfish-3 ×1
inputstream ×1
java-io ×1
jsf-2 ×1
managed-bean ×1
primefaces ×1
spring-mvc ×1
tomcat ×1
web-inf ×1
wildfly-9 ×1