2

ユーザーに名前の入力を求めるだけの非常に単純なページがあり、その名前でリソースを作成します。ユーザーが送信ボタンを押すと、作成したばかりのエンティティのページに直接ナビゲートしたいと思います。したがって、私のページは次のようになります。

<h:form id="form">
    <p:fieldset legend="Create new">
        <p:panelGrid columns="2">
            <h:outputText value="Name" />
            <p:inputText value="#{createBean.entity.name}" />
        </p:panelGrid>

        <p:commandButton value="Create Entity" ajax="false"
            action="#{createBean.submit}">
        </p:commandButton>
    </p:fieldset>
</h:form>

submitアクションはcreateBean、エンティティを永続化する必要があります。これは、副作用として、エンティティに ID を割り当てます。そして今、このエンティティに移動したいと思います。

public void submit() {
    /* Persist entity, entity.getId() will now
       return a meaningful value. */

    FacesContext context = FacesContext.getCurrentInstance();
    NavigationHandler handler = FacesContext.getCurrentInstance().getApplication().getNavigationHandler();

    // How could I pass the ID?
    handler.handleNavigation(context, null, "pretty:entity-detail");
}

のマッピングはentity-detail次のようになります。

<url-mapping id="entity">
    <pattern value="/entities" />
    <view-id value="/views/entity/list.xhtml"/>
</url-mapping>

<url-mapping parentId="entity" id="entity-detail">
    <pattern value="/view/#{id}" />
    <view-id value="/views/entity/entityDetail.xhtml"/>
</url-mapping>

記録として: Apache MyFaces 2.1.5 と PrettyFaces 3.3.2 を使用しています。

4

1 に答える 1

3

マッピングで名前付きパス パラメータを使用しています。この場合、アクション メソッドから viewId を返すだけで、対応するクエリ パラメータを追加できます。

public String submit() {

    /* Persist entity, entity.getId() will now
       return a meaningful value. */

    long id = ....

    return "/views/entity/entityDetail.xhtml?faces-redirect=true&id=" + id;

}

EL 注入パラメータの場合、プロセスは少し異なります。詳細については、ドキュメントのこの章を参照してください。

于 2012-02-11T14:22:54.620 に答える