重定向到 JSF 中的外部 URL [英] Redirect to external URL in JSF

查看:27
本文介绍了重定向到 JSF 中的外部 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been dealing with a problem with JSF, when it comes to redirect to pages inside my app it works just fine, but I haven't been able to redirect to external URL can some one guide me on this?

推荐答案

要么直接在 中提及 URL.

Either just mention the URL directly in <a> or <h:outputLink>.

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

或者,如果您需要使用 调用 bean 操作,如下所示,

Or, if you need to to invoke a bean action using <h:commandLink> like below,

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

然后使用 ExternalContext#redirect() 在 action 方法中.

then use ExternalContext#redirect() in action method.

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

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

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

Note that you don't need to catch that IOException, the server will deal with it. Also note the importance of including the scheme (https:// or http:// or //) in the URL, otherwise it will be interpreted relative to the current domain.

这篇关于重定向到 JSF 中的外部 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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