如何通过 JSF 中的上下文路径获取基本 URL?
我有这样的结构:
WebContent
resources
components
top.xhtml
company
about_us.xhtml
index.xhtml
top.xhtml
是一个组件,也在 index.xthml
和 about_us.xhtml
中使用。
top.xhtml
<ul>
<li><a href="index.xhtml">Home</a></li>
<li><a href="company/about_us.xhtml">About us</a></li>
...
</ul>
所以我的问题是,当当前页面是 index.xhtml
时,组件会正确生成 URL,但是当当前页面是 about_us.xhtml
时,组件会正确生成 URL。 code>,它会生成错误的 URL。我不能使用相对路径,因为它也会生成错误的 URL。我认为这是因为该组件是基于 *.xhtml
页面的当前路径的。
我能找到的唯一解决方案是:
<ul>
<li><a href="${pageContext.request.contextPath}/webname/index.xhtml">Home</a></li>
<li><a href="${pageContext.request.contextPath}/webname/about_us.xhtml">About us</a></li>
...
</ul>
但我认为根本不“优雅”。有什么想法吗?
I have this structure:
WebContent
resources
components
top.xhtml
company
about_us.xhtml
index.xhtml
top.xhtml
is a component, that is used in index.xthml
and about_us.xhtml
too.
top.xhtml
<ul>
<li><a href="index.xhtml">Home</a></li>
<li><a href="company/about_us.xhtml">About us</a></li>
...
</ul>
So my problem is, when the current page is index.xhtml
the component generates URLs correctly, but when the current page is about_us.xhtml
, it generates wrong URLs. I cannot use relative path because it's going to generate the wrong URL too. I think it is because the component is based on the current path of the *.xhtml
page.
The only solution I could found out is:
<ul>
<li><a href="${pageContext.request.contextPath}/webname/index.xhtml">Home</a></li>
<li><a href="${pageContext.request.contextPath}/webname/about_us.xhtml">About us</a></li>
...
</ul>
But I think is not 'elegant' at all. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
URL 不会根据服务器端的文件结构进行解析。 URL 是根据相关资源的真实公共网址进行解析的。即是网络浏览器必须调用它们,而不是网络服务器。
有几种方法可以减轻痛苦:
JSF EL 提供了
${pageContext.request}
的简写形式,具有#{request}
的风格:如果需要,您可以使用
标记使其更短。将其放在主模板中的某个位置,它将可用于所有页面:JSF 2.x 提供
,它可以获取相对于结果
,它将自动附加上下文路径和FacesServlet
映射:HTML提供
标签,使文档中的所有相对 URL 都相对于此基数。你可以利用它。将其放入
中。(注意:这需要 EL 2.2,否则你最好使用 JSTL
fn:substring()
,另请参阅 这个答案)这应该最终出现在生成的 HTML 中,例如
注意,
标记有一个警告:它使页面中的所有跳转锚点都像 < code> 也与它相关!另请参阅 是否建议使用 <基地> html 标签? 在 JSF 中你可以像top
或
。URLs are not resolved based on the file structure in the server side. URLs are resolved based on the real public web addresses of the resources in question. It's namely the webbrowser who has got to invoke them, not the webserver.
There are several ways to soften the pain:
JSF EL offers a shorthand to
${pageContext.request}
in flavor of#{request}
:You can if necessary use
<c:set>
tag to make it yet shorter. Put it somewhere in the master template, it'll be available to all pages:JSF 2.x offers the
<h:link>
which can take a view ID relative to the context root inoutcome
and it will append the context path andFacesServlet
mapping automatically:HTML offers the
<base>
tag which makes all relative URLs in the document relative to this base. You could make use of it. Put it in the<h:head>
.(note: this requires EL 2.2, otherwise you'd better use JSTL
fn:substring()
, see also this answer)This should end up in the generated HTML something like as
Note that the
<base>
tag has a caveat: it makes all jump anchors in the page like<a href="#top">
relative to it as well! See also Is it recommended to use the <base> html tag? In JSF you could solve it like<a href="#{request.requestURI}#top">top</a>
or<h:link value="top" fragment="top" />
.JSTL 1.2 变体源自 BalusC 答案
JSTL 1.2 variation leveraged from BalusC answer