Jannis Alexakis
Jannis Alexakis

Reputation: 1319

Redirect using <p:commandButton>

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?

Upvotes: 4

Views: 18169

Answers (1)

Arcangel2p
Arcangel2p

Reputation: 320

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);
}

Upvotes: 14

Related Questions