2012-02-11 43 views
2

我有一個非常簡單的頁面,只需提示用戶輸入名稱,然後使用該名稱創建資源。當用戶點擊提交按鈕時,我想直接導航到剛纔創建的實體的頁面。所以,我的網頁看起來是這樣的:使用帶動態ID的Prettyfaces進行導航

<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。現在我想導航到這個實體。

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和3.3.2 PrettyFaces。

+0

請問您可以發佈映射「實體 - 細節」嗎? – chkal 2012-02-11 10:28:51

+0

啊,當然。不知道我該如何設法忘記這一點。 – 2012-02-11 12:35:14

回答

3

您正在映射中使用命名路徑參數。在這種情況下,您可以簡單地從action方法返回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