JSF中如何重定向到外部URL

41

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


当您尝试重定向到外部URL时会发生什么?您是如何尝试实现的?请展示一些代码。 - Matt Ball
1
@Matt:我敢打赌他在摆弄导航案例和结果值。那确实不可能。对于内部页面,<redirect/>outcome?faces-redirect=true 都能很好地工作。 - BalusC
1个回答

103

可以直接在<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>

或者,如果您需要使用 <h:commandLink> 来调用一个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”前缀,再次感谢你快速而确定的回答。 - Necronet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接