用于链接/导航到其他 JSF 页面的 URL [英] What URL to use to link / navigate to other JSF pages

查看:18
本文介绍了用于链接/导航到其他 JSF 页面的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一些文件位于子文件夹中时,我对如何在 Java Server Faces 项目中的文件之间进行链接感到困惑.(我打算附上屏幕截图,以便您可以看到 Nebeans 项目视图和我正在尝试的示例的文件视图......但我还不允许添加图像).

I am confused about how to link between files in a Java Server Faces project, when some of the files are in sub-folders. (I was going to attach screenshots so you can see the Nebeans project view, and files view of the example I am trying.... but I'm not allowed to add images yet).

在我的示例项目中,我有名为index.xhtml"和calculate/calculate.xhtml"的文件.我还有一个名为template.xhtml"的文件,两者都使用.问题是在模板中使用哪种 url 格式适用于这两个文件.

I my example project I have files called "index.xhtml" and "calculate/calculate.xhtml". I also have a file called "template.xhtml" which is used by both. The question is what url format to use in the template that will work for both files.

在直接的 html 中,我将使用类似以下的内容来提供返回主页的链接,该链接可在任何位置使用:

In straight html I would just use something like the following to provide a link back to the home page that would work from any location:

<a href="/index.html">Home</a>

但我不知道什么是 JSF 应用程序的根文件夹,以及是使用项目视图"文件夹结构还是文件列表"文件夹结构.

But I can't figure out what counts as the root folder for a JSF application, and whether to use the "project view" folder structure or the "files list" folder structure.

推荐答案

首先,JSF是一个HTML代码生成器.所以它在 J​​SF 中与在普通"HTML 中没有什么不同.在 HTML 中创建链接时,您不应该只查看 webapp 项目中的文件系统结构.您应该查看这些资源的公共 URL 结构.即必须调用和下载这些资源的是网络浏览器,而不是网络服务器.网络浏览器对网络服务器中的文件系统结构一无所知.这并不特定于 JSF 项目.这适用于所有网络项目.

First of all, JSF is a HTML code generator. So it's not different in JSF than in "plain" HTML. You should just not look at file system structure in webapp project when creating links in HTML. You should look at public URL structure of those resources. It's namely the webbrowser who has got to invoke and download those resources, not the webserver. The webbrowser knows absolutely nothing about the file system structure in the webserver. This is not specific to JSF projects. This applies to all web projects.

相对 URL 与其在 webapp 项目的文件系统结构中的位置无关.它们与当前打开的 HTML 文档的请求 URL 相关,正是您在浏览器地址栏中看到的那个.应该注意的是,当 HTML 文档中存在 <base> 元素时,HTML 文档中所有不以 / 开头的相对 URL 都将变为相对于它.

Relative URLs are not relative to their location in the file system structure in webapp project. They are relative to the request URL of the currently opened HTML document, exactly the one you see in browser's address bar. Noted should be that when a <base> element is present in the HTML document, then all relative URLs in the HTML document not starting with / will become relative to it.

给定一个配置了 FacesServlet 映射 URL 模式的 *.xhtml 的 webapp,并使用上下文部署到 localhost:8080/context 的路径,项目的 web 根目录中 /index.xhtml 文件的 URL 如下:

Given a webapp which is configured with FacesServlet mapping URL pattern of *.xhtml, and is deployed to localhost:8080 with a context path of /context, the URL of a /index.xhtml file in project's web root will be as below:

http://localhost:8080/context/index.xhtml
----   -------------- ------- -----------
  |           |          |         `-- resource
  |           |          `-- path (can be multiple folders)
  |           `-- domain (and port)
  `-- scheme

当你当前在 http://localhost:8080/context/index.xhtml 中,并且你想创建一个到 http://localhost:8080/context 的链接/calculate/calculate.xhtml,那么下面所有的方式最终都会指向完全相同的绝对URL.

When you're currently in http://localhost:8080/context/index.xhtml, and you want to create a link to http://localhost:8080/context/calculate/calculate.xhtml, then all the ways below will ultimately point to exactly the same absolute URL.

  • // 开头的相对 URL 是相对于当前方案的.

  • A relative URL starting with // is relative to the current scheme.

<a href="//localhost:8080/context/calculate/calculate.xhtml">link</a>

  • / 开头的相对 URL 是相对于域的.

  • A relative URL starting with / is relative to the domain.

    <a href="/context/calculate/calculate.xhtml">link</a>
    

  • / 开头的相对 URLnot 是相对于路径的.

  • A relative URL not starting with / is relative to the path.

    <a href="calculate/calculate.xhtml">link</a>
    

  • 当你目前在http://localhost:8080/context/calculate/calculate.xhtml,你想链接到http://localhost:8080/context/index.xhtml,那么同样的规则适用:

    And when you're currently in http://localhost:8080/context/calculate/calculate.xhtml, and you want to link to http://localhost:8080/context/index.xhtml, then the same rules apply:

    • // 开头的相对 URL 是相对于当前方案的.

    • A relative URL starting with // is relative to the current scheme.

    <a href="//localhost:8080/context/index.xhtml">link</a>
    

  • / 开头的相对 URL 是相对于域的.

  • A relative URL starting with / is relative to the domain.

    <a href="/context/index.xhtml">link</a>
    

  • / 开头的相对 URLnot 是相对于路径的.

  • A relative URL not starting with / is relative to the path.

    <a href="../index.xhtml">link</a>
    

  • 您现在可能已经意识到,以 / 开头的相对 URL 依赖于当前路径和域.因此,这就是您真正希望在 Web 应用程序中随处使用的 URL,而不必担心在更改域或在服务器中移动文件时出现维护问题.唯一剩下的就是上下文路径的动态性.您可能已经知道这个值在 webapp 内部是无法控制的.您真的很想避免对其进行硬编码.但是,您可以在 EL 的帮助下轻松地让 JSF 以编程方式打印它.它仅由 HttpServletRequest 提供#getContextPath()HttpServletRequest 在 EL 中可用作隐式对象 #{request}.

    As you probably realize by now, a relative URL starting with / is not dependent from the current path and domain. So, that's the URL you really want to use everywhere in your web application without worrying about maintenance trouble when changing domain or moving around files in the server. The only thing left is the dynamicness of the context path. You probably already know that this value is not controllable from inside the webapp. You'd really like to avoid hardcoding it. You can however easily let JSF print it programmatically with a little help of EL. It's namely just available by HttpServletRequest#getContextPath() and the HttpServletRequest is in EL available as implicit object #{request}.

    <a href="#{request.contextPath}/index.xhtml">link</a>
    <a href="#{request.contextPath}/calculate/calculate.xhtml">link</a>
    

    每次重复此操作只会变得乏味.幸运的是,JSF 提供了 <h:link> 组件用于生成 HTML <a> 元素并自动内联当前上下文路径.

    It only gets tedious to repeat this everytime. Fortunately, JSF offers the <h:link> component for the very purpose of generating a HTML <a> element with the current context path automatically inlined.

    <h:link value="link" outcome="index.xhtml" />
    <h:link value="link" outcome="calculate/calculate.xhtml" />
    

    注意 outcome 必须代表一个 JSF 视图 ID,它不一定与 URL 路径相同(当你将 FacesServlet 映射到 *.xhtml).您甚至可以在此处省略文件扩展名,JSF 会自动将其检测为隐式导航"机制的一部分.

    Note that the outcome must represent a JSF view ID, which is not necessarily the same as the URL path (it will be when you map FacesServlet on *.xhtml). You can even omit the file extension here, JSF will automatically detect it as part of "implicit navigation" mechanism.

    <h:link value="link" outcome="index" />
    <h:link value="link" outcome="calculate/calculate" />
    

    另见:

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