如何通过JSF中的上下文路径获取基本URL? [英] How get the base URL via context path in JSF?

查看:62
本文介绍了如何通过JSF中的上下文路径获取基本URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的结构:

WebContent
    resources
        components
            top.xhtml

    company
        about_us.xhtml

    index.xhtml

top.xhtml是一个组件,也在index.xthmlabout_us.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>

所以我的问题是,当当前页面为index.xhtml时,组件会正确生成URL,但是当当前页面为about_us.xhtml时,它将生成错误的URL.我不能使用相对路径,因为它也会生成错误的URL.我认为这是因为该组件基于*.xhtml页面的当前路径.

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?

推荐答案

无法根据服务器端的文件结构来解析URL. URL是根据相关资源的真实公共网址解析的.是必须要调用它们的是Web浏览器,而不是Web服务器.

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以#{request}的风格提供了${pageContext.request}的简写:

JSF EL offers a shorthand to ${pageContext.request} in flavor of #{request}:

<li><a href="#{request.contextPath}/index.xhtml">Home</a></li>
<li><a href="#{request.contextPath}/about_us.xhtml">About us</a></li>

如有必要,您可以使用 <c:set> 标签使其更短.将其放在主模板中的某个位置,将对所有页面可用:

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:

<c:set var="root" value="#{request.contextPath}/" />
...
<li><a href="#{root}index.xhtml">Home</a></li>
<li><a href="#{root}about_us.xhtml">About us</a></li>

JSF 2.x提供了<h:link>,它可以获取相对于outcome中上下文根的视图ID,并且它将自动附加上下文路径和FacesServlet映射:

JSF 2.x offers the <h:link> which can take a view ID relative to the context root in outcome and it will append the context path and FacesServlet mapping automatically:

<li><h:link value="Home" outcome="index" /></li>
<li><h:link value="About us" outcome="about_us" /></li>

HTML提供了 <base> 标记,使文档中的所有相对URL都与此基准相对.您可以利用它.将其放入<h:head>.

<base href="#{request.requestURL.substring(0, request.requestURL.length() - request.requestURI.length())}#{request.contextPath}/" />
...
<li><a href="index.xhtml">Home</a></li>
<li><a href="about_us.xhtml">About us</a></li>

(注意:这需要EL 2.2,否则您最好使用JSTL fn:substring(),另请参见

(note: this requires EL 2.2, otherwise you'd better use JSTL fn:substring(), see also this answer)

这应该最终在生成的HTML中结束,例如

This should end up in the generated HTML something like as

<base href="http://example.com/webname/" />

请注意,<base>标记有一个警告:它使页面中的所有跳转锚点也像<a href="#top">一样相对于它!另请参见是否建议使用<基地> html标签?在JSF中,您可以像<a href="#{request.requestURI}#top">top</a><h:link value="top" fragment="top" />一样解决它.

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" />.

这篇关于如何通过JSF中的上下文路径获取基本URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆