【问题标题】:Navigation using Prettyfaces with dynamic id使用具有动态 ID 的 Prettyfaces 进行导航
【发布时间】:2012-03-03 13:57:02
【问题描述】:

我有一个非常简单的页面,它只是提示用户输入一个名称,然后使用该名称创建一个资源。当用户点击提交按钮时,我想直接将他导航到刚刚创建的实体的页面。所以我的页面是这样的:

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

【问题讨论】:

  • 您能发布映射“实体详细信息”吗?
  • 啊,当然。不知道我怎么能忘记这一点。

标签: jsf-2 primefaces prettyfaces


【解决方案1】:

您在映射中使用命名路径参数。在这种情况下,您可以简单地从 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

【讨论】:

    猜你喜欢
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 2011-12-14
    相关资源
    最近更新 更多