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