java JSF 面临 WEB-INF 之外的配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1365512/
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
JSF faces config file outside WEB-INF?
提问by PlagueHammer
When creating multiple faces config files, it is correct to have the faces-config.xml outside of WEB-INF? The JSF spec does not seem to be very clear about this (Section 10.1.3)
创建多个人脸配置文件时,将faces-config.xml 放在WEB-INF 之外是否正确?JSF 规范对此似乎不是很清楚(第 10.1.3 节)
If yes, how should this faces-config.xml be declared in web.xml? the paths generated by IDE's (like Eclipse/JDev) generally use something like:
如果是,这个faces-config.xml 应该如何在web.xml 中声明?IDE 生成的路径(如 Eclipse/JDev)通常使用以下内容:
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config1.xml</param-value>
</context-param>
Now, if my faces-config.xml is outside WEB-INF -- is it correct to declare the param-value as something like "/WebContent/WEB-INF/faces-config2.xml"?
现在,如果我的 faces-config.xml 在 WEB-INF 之外——将 param-value 声明为“/WebContent/WEB-INF/faces-config2.xml”这样的东西是否正确?
回答by David Rabinowitz
It is possible, but not recommended. The reason you put configuration files under WEB-INF is that the server knows not to serve these files - you have your code, database configuration and other sensitive stuff there.
这是可能的,但不推荐。您将配置文件放在 WEB-INF 下的原因是服务器知道不提供这些文件 - 您有代码、数据库配置和其他敏感内容。
You cannot use "/WebContent/WEB-INF/faces-config2.xml" - AFAIK "WebContent" is eclipse specific. have the config files names to be absolute inside the WAR. It means that they will always begin with "/WEB-INF/".
您不能使用“/WebContent/WEB-INF/faces-config2.xml” - AFAIK“WebContent”是eclipse特定的。让配置文件名在 WAR 中是绝对的。这意味着它们将始终以“/WEB-INF/”开头。
回答by harto
As mentioned in David's answer, it's not recommended to put the config files outside of the WEB-INF directory. However, if you're looking for ways to organise your project, you might try creating a new subdirectory underWEB-INF.
正如大卫的回答中提到的,不建议将配置文件放在 WEB-INF 目录之外。但是,如果您正在寻找组织项目的方法,您可以尝试在WEB-INF下创建一个新的子目录。
For example, when I'm dealing with lots of faces-config files, I'll create a directory /WEB-INF/configand put the faces-config files in there.
例如,当我处理大量faces-config 文件时,我将创建一个目录/WEB-INF/config并将faces-config 文件放在那里。
回答by Billy Bob Bain
If you are referencing them from the web.xml, then the file needs to be in the war at runtime. There are other ways that the container can discover faces-config.xml resources.
如果您从 web.xml 引用它们,则该文件需要在运行时处于War中。容器可以通过其他方式发现 faces-config.xml 资源。
10.4.2At application startup time, before any requests are processed, the JSF implementation must process zero or more application configuration resources, located according to the following algorithm:
10.4.2在应用程序启动时,在处理任何请求之前,JSF 实现必须根据以下算法处理零个或多个应用程序配置资源:
Search for all resources named “META-INF/faces-config.xml” in the ServletContext resource paths for this web application, and load each as a JSF configuration resource (in reverse order of the order in which they are returned by getResources() on the current Thread's ContextClassLoader).
Check for the existence of a context initialization parameter named javax.faces.CONFIG_FILES. If it exists, treat it as a comma-delimited list of context relative resource paths (starting with a “/”), and load each of the specfied resources.
Check for the existence of a web application configuration resource named “/WEBINF/ faces-config.xml”, and load it if the resource exists.
在此 Web 应用程序的 ServletContext 资源路径中搜索名为“META-INF/faces-config.xml”的所有资源,并将每个资源作为 JSF 配置资源加载(与 getResources() 返回的顺序相反)在当前线程的 ContextClassLoader 上)。
检查是否存在名为 javax.faces.CONFIG_FILES 的上下文初始化参数。如果存在,将其视为上下文相关资源路径的逗号分隔列表(以“/”开头),并加载每个指定的资源。
检查是否存在名为“/WEBINF/faces-config.xml”的 Web 应用程序配置资源,如果该资源存在,则加载它。

