0

I am using the edit pencil of the row edit datatable in prime faces edit pencil. Here is the jsf page in the website of primefaces

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

and here is the onRowEdit in the managed bean

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

In the EditView bean, i tried to make the signature like this public String onRowCancel(RowEditEvent event) to redirect to another page after row edit, it is ignored. I tried this

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

but it didn't redirect to the success page, it just prints the Car Edited msg with its id. I want a way to redirect to another page after the row editing method. How can it be done?

Kingo Mostafa
  • 357
  • 5
  • 21
  • 1. Post minimal xhtml with the table and rowCancel event binding. Post onRowCancel source. 2. The problem is not clear. Does the method not get called? Or does it get called, but its redirect logic doesn't work? – Vsevolod Golovanov Jul 30 '15 at 07:58
  • possible duplicate of [Sending a redirect from inside an ajax listener method](http://stackoverflow.com/questions/18589616/sending-a-redirect-from-inside-an-ajax-listener-method) – Kukeltje Jul 30 '15 at 08:10
  • Search for generic issues... Leave what is the onRowEdit? it is called on an ajax event, so search for 'jsf redirect ajax event' and you will find the answer in google very quickly. And keep in mind that your message wil not survice the redirect! Search on google for a solution to that. – Kukeltje Jul 30 '15 at 08:11
  • I wouldn't advise using that possible duplicate's answer, since it talks of servlet level redirecting instead of JSF navigation. – Vsevolod Golovanov Jul 30 '15 at 08:21
  • Then use this one as a duplicate: http://stackoverflow.com/questions/30456512/jsf-redirecting-from-bean-on-primefaces-ajax-event – Kukeltje Jul 30 '15 at 09:20
  • And using the 'servlet level' is not by definition wrong... See http://stackoverflow.com/questions/11277366/what-is-the-difference-between-redirect-and-navigation-forward-and-when-to-use-w – Kukeltje Jul 30 '15 at 09:23
  • It's wrong in an abstraction layer sense. web.xml and servlet filters define the way that URLs are mapped to viewIds. If you hardcode URLs in code, than it could be harder to change the mapping, e.g. if you'd decide to adopt user friendly readable URLs in the future. – Vsevolod Golovanov Jul 30 '15 at 10:34

1 Answers1

0

Redirect by returning outcome works only in action methods, e.g. as in p:commandButton action. You need to do the redirect programmatically. See example.

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");
}
Vsevolod Golovanov
  • 4,068
  • 3
  • 31
  • 65
  • Then find the other duplicate... I know it does not give you reputation then... but there is a duplicate – Kukeltje Jul 30 '15 at 09:10
  • I posted the wrong one, but cannot correct that... Please file this one as a duplicate... http://stackoverflow.com/questions/30456512/jsf-redirecting-from-bean-on-primefaces-ajax-event – Kukeltje Jul 30 '15 at 09:20
  • And btw, I don't think this example does a redirect...Unless it is a xml configured outcome which in turn points to a real redirect – Kukeltje Jul 30 '15 at 09:26
  • @Kukeltje, you're right, I should've found an existing answered question. Flagged. Yes, this answer relies on an xml configured outcome, since judging by the question, that's what the asker uses. – Vsevolod Golovanov Jul 30 '15 at 10:35