【问题标题】:Redirect using <p:commandButton>使用 <p:commandButton> 重定向
【发布时间】:2015-04-15 10:13:35
【问题描述】:

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

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

后面的代码:

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

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

【问题讨论】:

标签: jsf-2 primefaces


【解决方案1】:

您可以通过两种方式做到这一点:

  • 导航:

调用一个动作,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 bean:

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

【讨论】:

  • 如果你进一步看,我的第一个jsp(我以jsp为例,因为他可以使用xhtml或他想要的任何东西),我使用'action'而不是'actionListener'来调用bean方法(和他一样)。
猜你喜欢
  • 2013-05-04
  • 2012-05-15
  • 1970-01-01
  • 2012-05-09
  • 2023-03-09
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 2013-08-01
相关资源
最近更新 更多