6

<f:metadata>、<f:viewParam>、および <f:viewAction> は何に使用できますか?

レンダリング前のビュー イベント リスナーがあります。

<f:metadata>
    <f:event type="preRenderView" listener="#{loginBean.performWeakLogin()}" />
</f:metadata>

次のメソッドを呼び出します。

public String performWeakLogin() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String parameter_value = (String) facesContext.getExternalContext().getRequestParameterMap().get("txtName");

    if (parameter_value != null && parameter_value.equalsIgnoreCase("pippo")) {
        try {
            return "mainPortal";
        } catch (IOException ex) {
            return null;
        }
    } else {
        return null;
    }
}

および次のナビゲーション ルール:

<navigation-rule>
    <from-view-id>/stdPortal/index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>mainPortal</from-outcome>
        <to-view-id>/stdPortal/stdPages/mainPortal.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
</navigation-rule>

ただし、ナビゲーションは実行しません。次のようにコマンドボタンを使用すると機能します。

<p:commandButton ... action="#{loginBean.performWeakLogin()}"  /> 
4

2 に答える 2

10

メソッドの戻り値に基づくナビゲーションは、ActionSource2インターフェースを実装し、コンポーネントの属性MethodExpressionなど、そのための属性を提供するコンポーネントによってのみ実行されます。コンポーネントのaction属性は、リクエスト値の適用UICommandフェーズでキューに入れられ、アプリケーションの呼び出しフェーズで呼び出されます。

<f:event listener>単なるコンポーネント システム イベントリスナ メソッドであり、アクション メソッドではありません。次のように、ナビゲーションを手動で実行する必要があります。

public void performWeakLogin() {
    // ...

    FacesContext fc = FacesContext.getCurrentInstance();
    fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "mainPortal");
}

または、特定の URL にリダイレクトを送信することもできます。これは、内部ではなく外部に移動したい場合に便利です。

public void performWeakLogin() throws IOException {
    // ...

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath() + "/stdPortal/stdPages/mainPortal.xhtml");
}

具体的な問題とは関係ありませんが、サーブレット フィルターは、リクエスト ベースの承認/認証を実行するジョブに適しています。

以下も参照してください。

于 2013-04-19T14:06:21.627 に答える
1

JSF 2.1 で JBoss 7 を使用しています。BalusC の解決策は、web.xml でデフォルトのエラー ページを設定していたにもかかわらず、JBoss のデフォルトのエラー ページにリダイレクトしていました。

<error-page>
    <error-code>404</error-code>
    <location>/myapp/404.xhtml</location>
</error-page>

自分のエラー ページにリダイレクトするために、応答を使用してエラーを送信しました。

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)facesContext.getExternalContext().getResponse();
try {
    response.sendError(404);
} catch (IOException ioe) {
    ioe.printStackTrace();
}
facesContext.responseComplete();
于 2013-12-17T09:27:43.030 に答える