使用 <p:commandButton> 重定向

Redirect using <p:commandButton>

下一行应该保存一个新项目并重定向到另一个页面。到目前为止,它保存正确,但没有重定向。没有错误或警告。

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

后面的代码:

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

正如我所说,第一行正确执行,第二行似乎没有做任何事情。知道我可能做错了什么吗?

您可以通过两种方式完成:

  • 导航:

调用一个动作,将 commandButton 组件设置为 ajax false,并且 bean 方法返回一个字符串(正如您已经拥有的那样)。

xhtml 页面:

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

调用 actionListener,commandButton 组件设置为 ajax true,bean 方法不返回值,而是自行执行到所需页面的重定向。

xhtml 页面:

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

java豆子:

public void doSave(){       
    categoryAddEvent.fire(categoryProducer.getSelectedCategory());
    FacesContext.getCurrentInstance().getExternalContext().redirect(Pages.LIST_CATEGORIES);
}