4

Following line should save a new item and redirect to another page. So far, it saves correctly, but it doesn´t redirect. No errors or warnings.

<p:commandButton id="savebutton" ajax="false" value="#{msg['addCategory.save']}" actionListener="#{addCategoryController.doSave()}" />

Code behind:

 public String doSave(){       
    categoryAddEvent.fire(categoryProducer.getSelectedCategory());
    return Pages.LIST_CATEGORIES;
}

As I said, the first line executes correctly, the second one doesn´t seem to do anything. Any ideas what I could be doing wrong?

Arcangel2p
  • 323
  • 2
  • 10
Jannis Alexakis
  • 1,279
  • 4
  • 19
  • 38
  • 1
    You'd better use `action` instead of `actionListener` to perform navigation. More information: http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener – Mathieu Castets Apr 15 '15 at 10:25
  • Where is your 'onclick'? Please change the subject – Kukeltje Apr 15 '15 at 10:32

1 Answers1

14

You can do it in two ways:

  • Navigation:

Calling an action, with the commandButton component set as ajax false, and the bean method returning a String (as you already have).

xhtml page:

<p:commandButton id="savebutton" ajax="false" value="#{msg['addCategory.save']}" action="#{addCategoryController.doSave()}" />
  • Redirect:

Calling an actionListener, with the commandButton component set as ajax true, with the bean method not returning value, but instead performing itself the redirection to the desired page.

xhtml page:

<p:commandButton id="savebutton" ajax="true" value="#{msg['addCategory.save']}" actionListener="#{addCategoryController.doSave()}" />

java bean:

public void doSave(){       
    categoryAddEvent.fire(categoryProducer.getSelectedCategory());
    FacesContext.getCurrentInstance().getExternalContext().redirect(Pages.LIST_CATEGORIES);
}
Arcangel2p
  • 323
  • 2
  • 10
  • If you look further, my first jsp (i set jsp as an example, as he can use xhtml or whatever he wants), I call the bean method using 'action' instead of 'actionListener' (as he does). – Arcangel2p Apr 15 '15 at 11:14