【问题标题】:Redirect to external URL in JSF重定向到 JSF 中的外部 URL
【发布时间】:2011-07-02 19:44:07
【问题描述】:

我一直在处理 JSF 的一个问题,当涉及到重定向到我的应用程序内的页面时,它工作得很好,但是我无法重定向到外部 URL,有人可以指导我吗?

【问题讨论】:

  • 当您尝试重定向到外部 URL 时会发生什么?你想怎么做?给我们看一些代码。
  • @Matt:我敢打赌他在摆弄导航案例和结果值。那确实是不可能的。对于内部页面,<redirect/>outcome?faces-redirect=true 可以正常工作。

标签: jsf redirect jsf-2 external-url


【解决方案1】:

直接在<a><h:outputLink> 中提及URL。

<a href="https://stackoverflow.com">Go to this site!</a>
<!-- or -->
<h:outputLink value="https://stackoverflow.com">Go to this site!</h:outputLink>

或者,如果您需要使用 &lt;h:commandLink&gt; 调用 bean 操作,如下所示,

<h:form>
    <h:commandLink value="Go to this site!" action="#{bean.redirect}" />
</h:form>

然后在操作方法中使用ExternalContext#redirect()

public void redirect() throws IOException {
    // ...

    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    externalContext.redirect("https://stackoverflow.com");
}

请注意,您不需要捕获 IOException,服务器会处理它。还要注意在 URL 中包含方案(https://http:////)的重要性,否则它将相对于当前域进行解释。

【讨论】:

  • 感谢 balus ExternalContext 解决方案有效,可能我没有在我的 URL 上加上“http”前缀,+1 再次快速确定答案
猜你喜欢
  • 1970-01-01
  • 2017-12-11
  • 2012-01-10
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
相关资源
最近更新 更多