9

フラッシュ スコープを使用して、@viewscoped コントローラー間で設定オブジェクトを渡します。しかし、そのうちの 1 つでページをリロードすると、フラッシュ マップが空になり、設定オブジェクトが初期化されません。ページのリロード時にフラッシュ スコープを保持することは可能ですか?

設定を保存/取得するための私のソースコード:

FistPage.xhtml

...
<p:commandButton value="next"
    action="#{firstPageController.transferConfig}"  
    process="@this" />
...

FirstPageController.java

@ManagedBean(name = "firstPageController")
@ViewScoped
public class FirstPageController {
...
public String transferConfig() {
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("searchConfig",   searchConfig);
return "/secondPage.xhtml?faces-redirect=true";
}
...
}

SecondPage.xhtml

...
<h:outputLabel value="value">
    <f:event type="preRenderComponent" listener="#{secondPageController.onPageLoad()}"/>
</h:outputLabel>
...

SecondPageController.java

@ManagedBean(name = "secondPageController")
@ViewScoped
public class SecondPageController {
    ...
    public void onPageLoad() 
    {
        flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();

        searchConfig = ((SearchFilterConfig) flash.get("searchConfig"));

        flash.putNow("searchConfig", searchConfig);

        flash.keep("searchConfig");
    }
    ...
}

Mojarra 2.1.29を使用しています

ありがとう

4

1 に答える 1

8

プレイグラウンド プロジェクトでいくつかのテストを行っところ . それがJSFドキュメントで説明されている方法です:{flash.keep}

<navigation-case>実装では、が含まれるの場合でも、フラッシュの適切な動作が保持されるようにする必要があり<redirect />ます。実装では、同じセッションでGETリクエストが隣接している場合でも、フラッシュの適切な動作が維持されるようにする必要があります。これにより、Faces アプリケーションはPost/Redirect/Get設計パターンを十分に活用できます。

ここに、素敵な基本的なテスト ケースがあります。

page1.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head />
<h:body>
    <h:form>
        <h:button id="nextButton" value="Next (button)" outcome="page2.xhtml" />
        <c:set target="#{flash}" property="foo" value="bar" />
    </h:form>
</h:body>
</html>

page2.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<head />
<body>foo = #{flash.keep.foo}
</body>
</html>

最初のページを開いてボタンをクリックすると、2 番目のページにリダイレクトされます。次に、2 番目のページを何度でも更新すると、パラメーターが保持されていることがわかります。


Mojarra 2.2.6でテスト済み

于 2014-07-31T11:26:48.853 に答える