本文介绍了重定向到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?

推荐答案

要么直接在<a><h:outputLink>中提及URL.

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

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

或者,如果您需要使用<h:commandLink>调用如下所示的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() 的作用方式.

then use ExternalContext#redirect() in action method.

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

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

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

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

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

09-05 12:43
查看更多