JSF:指向 /webapp 子目录中网页的超链接

发布于 2024-10-15 06:51:43 字数 1183 浏览 2 评论 0原文

我有一个 .xhtml 页面列表,保存在 /src/main/webapp/pages/ 文件夹中。 现在我想创建它们的超链接。目前唯一有效的是默认主页:/src/main/webapp/pages/default.xhtml。

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>/pages/default.xhtml</welcome-file>
  </welcome-file-list>

  <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map these files with JSF -->
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>

对于其他人,如果我有一个链接,例如:

<a href="/pages/page1.xhtml">Page 1</a>

我收到以下错误:

/page1.xhtml 未在以下位置找到 作为资源的外部上下文

我的问题是:如何在相对于web应用程序根目录的href 中指定我想要的页面

I have a list of .xhtml pages that I keep in my /src/main/webapp/pages/ folder.
Now I want to create hyperlinks to them. Currently the only one that works is the default home page: /src/main/webapp/pages/default.xhtml.

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>/pages/default.xhtml</welcome-file>
  </welcome-file-list>

  <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map these files with JSF -->
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>

For the others, if I have a link such as:

<a href="/pages/page1.xhtml">Page 1</a>

I get the following error:

/page1.xhtml Not Found in
ExternalContext as a Resource

My question is: How do I specify the page I want in a href relative to the webapp root.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-10-22 06:51:43

关于相对链接(即不以 http:// 开头的链接),您需要了解两件主要事情:

  • 以前导斜杠 / 开头的相对链接是相对于域根的。
  • 不带前导斜杠的相对链接是相对于请求 URL 的(就像在浏览器地址栏中一样)。

如果当前 URL 是 http://example.com/app 并且页面包含链接

<a href="/pages/page1.xhtml">

,那么它将指向到 http://example.com/pages/page1.xhtml (失败)。


如果当前 URL 是 http://example.com/app 并且页面包含链接

<a href="pages/page1.xhtml">

,那么它将指向到 http://example.com/app/pages/page1.xhtml (有效)。


如果当前 URL 为 http://example.com/app/pages/default.xhtml 并且页面包含一个链接

<a href="pages/page1.xhtml">

,那么它将指向 http://example.com /app/pages/pages/page1.xhtml(失败)。


您的问题是欢迎页面是通过转发而不是重定向打开的。这样,浏览器地址栏中的请求 URL 就会保留在 http://example.com/app 上它实际上显示 http://example.com/app/pages/default.xhtml 的内容。为了让链接在所有情况下都能工作,您需要一个像

<a href="/app/pages/page1.xhtml">

“Thus”这样的链接,包括上下文路径,即 webapp 根目录。如果您唯一的问题是您想要动态包含上下文路径,那么只需打印 HttpServletRequest#getContextPath()

<a href="#{request.contextPath}/pages/page1.xhtml">

另请参阅:

Two major things you need to know about relative links (i.e. the ones not starting with http://):

  • Relative links starting with a leading slash / are relative to the domain root.
  • Relative links without a leading slash are relative to request URL (as it is in browser address bar).

If the current URL is http://example.com/app and the page contains a link

<a href="/pages/page1.xhtml">

then it will point to http://example.com/pages/page1.xhtml (fails).


If the current URL is http://example.com/app and the page contains a link

<a href="pages/page1.xhtml">

then it will point to http://example.com/app/pages/page1.xhtml (works).


If the current URL is http://example.com/app/pages/default.xhtml and the page contains a link

<a href="pages/page1.xhtml">

then it will point to http://example.com/app/pages/pages/page1.xhtml (fails).


Your problem is that the welcome page is opened by a forward rather than a redirect. This way the request URL as it is in browser address bar stays on http://example.com/app while it is actually displaying the content of http://example.com/app/pages/default.xhtml. To get the links to work in all circumstances you want a link like

<a href="/app/pages/page1.xhtml">

Thus, including the context path, which is the webapp root. If your sole problem is that you want to include the context path dynamically, then just print HttpServletRequest#getContextPath()

<a href="#{request.contextPath}/pages/page1.xhtml">

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文