【问题标题】:Is there something better to generate absolute links?有没有更好的方法来生成绝对链接?
【发布时间】:2011-12-20 04:27:53
【问题描述】:

在 JSF 2.0 中生成绝对链接有更好的方法吗?现在我正在使用<h:outputLink/>#{facesContext.externalContext.requestContextPath} 一样丑陋,如下所示。我不想使用 JSTL 和<c:url />

<h:outputLink value="#{facesContext.externalContext.requestContextPath}/pages/home.jsf">Home</h:outputLink>

【问题讨论】:

  • 导航规则呢?
  • 没有导航规则。我只需要在 JSF 2.0 中找到一种简单的方法来自动生成使用上下文路径的链接。通常我使用&lt;c:url /&gt;,但要求不要使用JSTL。

标签: jsf-2


【解决方案1】:

您可以将#{facesContext.externalContext.requestContextPath} 缩短为#{request.contextPath}。你甚至可以摆脱它来使用HTML &lt;base&gt; tag instead

在这种特殊情况下,最好改用&lt;h:link&gt;。它可以在outcome 属性中采用上下文相关的导航案例路径:

<h:link value="Home" outcome="pages/home" />

JSF 将在生成&lt;a&gt; 元素时注意添加正确的上下文路径和FacesServlet 映射:

<a href="/contextname/pages/home.jsf">Home</a>

另见:

【讨论】:

  • 如果我没有结果或导航规则怎么办,例如:&lt;h:link value="download" outcome="res/file.pdf" /&gt;。这给出了一个错误:Unable to find matching navigation case from view ID '/pages/home.xhtml' for outcome 'res/file.pdf'
  • 然后使用 &lt;h:outputLink value="#{request.contextPath}/res/file.pdf"&gt;Download&lt;/h:outputLink&gt; 或仅使用 &lt;a href="#{request.contextPath}/res/file.pdf"&gt;Download&lt;/a&gt; 或使用 HTML &lt;base&gt;
猜你喜欢
  • 2012-03-01
  • 2015-08-17
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多