2012-02-11 11 views
2

私は単純にユーザーに名前を入力し、その名前のリソースを作成する非常に簡単なページを持っています。ユーザーが送信ボタンを押すと、作成したエンティティのページに直接移動したいと思います。だから私のページには、次のようになります。動的IDを持つPrettyfaceを使用したナビゲーション

<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> 

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

<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と3.3.2 PrettyFacesを使用して

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のマッピングは次のようになります。

+0

マッピング「entity-detail」を投稿できますか? – chkal

+0

ああ、確かに。どのように私はそれを忘れることができるかわからない。 –

答えて

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注入パラメータについては、プロセスが少し異なります。詳細については、ドキュメントのthis chapterを参照してください。

関連する問題