2012-02-11 44 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