使用<p:commandButton>重定向

Jan*_*kis 0 primefaces jsf-2

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

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

代码背后:

 public String doSave(){       
    categoryAddEvent.fire(categoryProducer.getSelectedCategory());
    return Pages.LIST_CATEGORIES;
}
Run Code Online (Sandbox Code Playgroud)

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

Arc*_*l2p 6

你可以用两种方式做到:

  • 导航:

调用一个动作,将commandButton组件设置为ajax false,并返回一个返回String的bean方法(如您所知).

xhtml页面:

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

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

xhtml页面:

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

java bean:

public void doSave(){       
    categoryAddEvent.fire(categoryProducer.getSelectedCategory());
    FacesContext.getCurrentInstance().getExternalContext().redirect(Pages.LIST_CATEGORIES);
}
Run Code Online (Sandbox Code Playgroud)