数据表行编辑的onRowEdit()方法忽略重定向到另一个页面

时间:2015-07-30 07:06:47

标签: jsf primefaces

我在主要面孔中使用行编辑数据表的编辑铅笔 edit pencil。 这是primefaces网站上的jsf页面

<h:form id="form">
   <p:growl id="msgs" showDetail="true"/>

   <p:dataTable id="cars1" var="car" value="#{dtEditView.cars1}" editable="true" style="margin-bottom:20px">

       <p:ajax event="rowEdit" listener="#{dtEditView.onRowEdit}" update=":form:msgs" />
       <p:ajax event="rowEditCancel" listener="#{dtEditView.onRowCancel}" update=":form:msgs" />

       <p:column headerText="Id">
           <p:cellEditor>
               <f:facet name="output"><h:outputText value="#{car.id}" /></f:facet>
               <f:facet name="input"><p:inputText id="modelInput" value="#{car.id}" style="width:100%"/></f:facet>
           </p:cellEditor>
       </p:column>

       <p:column style="width:32px">
           <p:rowEditor />
       </p:column>
   </p:dataTable>
</h:form>

,这是托管bean中的onRowEdit

public void onRowEdit(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Car Edited", ((Car) event.getObject()).getId());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

在EditView bean中,我尝试使用此public String onRowCancel(RowEditEvent event)之类的签名在行编辑后重定向到另一个页面,它将被忽略。 我试过这个

    public String onRowEdit(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Car Edited", ((Car) event.getObject()).getId());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return "success";
    }

但它没有重定向到成功页面,只是打印带有id的Car Edited msg。 我想要一种在行编辑方法之后重定向到另一个页面的方法。 怎么办呢?

1 个答案:

答案 0 :(得分:0)

通过返回结果重定向仅适用于action方法,例如与p:commandButton action中一样。您需要以编程方式执行重定向。见例。

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;

// ...

public String onRowEdit(RowEditEvent event) {
    // ...
    FacesContext ctx = FacesContext.getCurrentInstance();
    NavigationHandler navigationHandler = ctx.getApplication().getNavigationHandler();
    navigationHandler.handleNavigation(ctx, null, "success");
}