且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重定向到 JSF 中的外部 URL

更新时间:2023-10-25 23:36:40

要么直接在 中提及 URL.

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

<a href="https://***.com">Go to this site!</a>
<!-- or -->
<h:outputLink value="https://***.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://***.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.