问题描述
我一直在处理 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>
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!