39

私はJSFの問題に取り組んできました。アプリ内のページにリダイレクトする場合は問題なく機能しますが、外部URLにリダイレクトできませんでした。誰かがこれについて教えてくれますか?

4

1 に答える 1

96

<a>またはに直接URLを記載して<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>

<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://または//)を含めることの重要性にも注意してください。そうしないと、現在のドメインに関連して解釈されます。

于 2011-02-23T14:55:13.843 に答える