我模板中的绝对路径名以获取资源 [英] Absolute path name in my template to get resources
问题描述
在我的JSF 2.0 Facelets应用程序中,我有一个光荣的模板,我希望所有页面都可以使用.它位于名称为template.xhtml的Web应用程序的根目录中.因此,正如您所期望的那样被引用:
In my JSF 2.0 Facelets application I have one glorious template that I want all the pages to use. It is in the root directory of the web-app disguised with the name template.xhtml. So it is referenced as you would expect:
<ui:composition template="./template.xhtml">
但是,我确实会立即导航到子目录中的客户端文件.由于特权级别不同,以这种方式组织它们很有用.这些子目录中的facelets将引用相同的模板,如下所示:
However I do navigate now and then to client files in sub-directories. It is useful to organize them this way because of different privilege levels. The facelets in those subdirectories would have a reference to the same template like this:
<ui:composition template="../template.xhtml">
到目前为止,一切都很好.但是,在模板的标头中,我像这样拉入CSS:
So far so good. However in the header of the template I pull in the css like this:
<link href="./resources/css/default.css" rel="stylesheet" type="text/css" />
<link href="./resources/css/tableLayout.css" rel="stylesheet" type="text/css" />
<link href="../resources/css/default.css" rel="stylesheet" type="text/css" />
<link href="../resources/css/tableLayout.css" rel="stylesheet" type="text/css" />
冗余引用的原因是,我还没有找到其他方法来使模板从根目录或子目录的上下文中工作.除非您这样输入应用程序名称,否则以/开头的路径名将不起作用
The reason for the redundant references is that I haven't found any other way to get the template to work from the context of the root directory or the sub-directory. A pathname that starts with a / doesn't work unless you put the application name in it like this
/TheApp-Ver1_0/resources/css/default.css
此问题是绝对路径名以变量而不是常量开头.该变量取决于应用程序在容器中的部署方式.有没有解决这个问题的干净方法?
The problem with this is that the absolute pathname starts with a variable, not a constant. The variable is dependent on how the app is deployed in the container. Is there any clean way of resolving this?
我做了一些搜索来找到这个问题.诚实的.但是,我怀疑这是另一之一,其中BalusC突然出现在其中,提供了与我错过的某个地方广泛讨论的盲目的显而易见的解决方案的链接.
I did some searches to find this question. Honest. However I suspect this is another one of those where BalusC swoops in an provides a link to the blindingly obvious solution extensively discussed somewhere I missed.
推荐答案
<ui:composition>
中的template
路径是相对于Webapp自己的文件夹结构,而不是域根目录(因为它不代表URL!). .因此,如果您以/
开头,它将相对于上下文根进行解析.
The template
path in <ui:composition>
is relative to the webapp's own folder structure, not to the domain root (because it does not represent an URL!). So if you start it with /
, it's just resolved relative to the context root.
<ui:composition template="/WEB-INF/inc/template.xhtml">
(输入/WEB-INF
的优点是最终用户无法通过猜测URL直接打开它)
(putting in /WEB-INF
has the advantage that the enduser cannot open it directly by guessing the URL)
<h:outputStylesheet>
,<h:outputScript>
和<h:graphicImage>
的name
路径始终相对于/resources
根文件夹,而不管是否以/
开头.
The name
path of <h:outputStylesheet>
, <h:outputScript>
and <h:graphicImage>
is always relative to /resources
root folder, regardless of if you start it with /
or not.
<h:outputStylesheet name="css/default.css" />
<h:outputScript name="js/default.js" />
<h:graphicImage name="img/logo.png" />
如果出于某种原因要使用纯HTML而不是JSF组件来包含CSS/JS/图像,那么最好是在#{request.contextPath}
之前添加路径,以便将其设置为相对于域的URL,这样您就无需摆弄上下文相关的URL.另请参见:如何获取基本URL?
If you want to use plain HTML instead of JSF components to include CSS/JS/images for some reason, then best is to prepend the path with #{request.contextPath}
yourself, so that you can make it a domain-relative URL, so that you don't need to fiddle with context-relative URLs. See also: How get the base URL?
这篇关于我模板中的绝对路径名以获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!