我一直在处理JSF的问题,当涉及到重定向到应用内部的页面时,它运行良好,但我无法重定向到外部URL,有人可以指导我吗?
我一直在处理JSF的问题,当涉及到重定向到应用内部的页面时,它运行良好,但我无法重定向到外部URL,有人可以指导我吗?
可以直接在<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://
或//
)的重要性,否则它将被解释为相对于当前域。
<redirect/>
或outcome?faces-redirect=true
都能很好地工作。 - BalusC