primefaces:actionListener和重定向同时进行

时间:2014-02-14 14:49:31

标签: java primefaces

我想点击

   <p:commandButton>

,调用Bean进行处理,同时将页面重定向到另一个页面。

换句话说,我想结合起来:

<p:commandButton actionListener="#{processBean.process}" >
</p:commandButton> 

<h:link outcome="redirection"> 
</h:link>

在faces-config.xml中配置“重定向”

我该怎么办? 有没有更好的方法来实现这一目标?

3 个答案:

答案 0 :(得分:4)

使用action

p:commandButton.属性
<p:commandButton actionListener="#{processBean.process}" >
</p:commandButton> 

将其更改为

<p:commandButton actionListener="#{processBean.process}" action="viewIDToRedirect" >
</p:commandButton> 

首先,actionListener将被调用,然后导航将启动。但请记住,您需要在faces-config.xml

中定义导航规则

答案 1 :(得分:2)

使用action属性,但不再保留actionListener

<p:commandButton action="#{processBean.process}" /> 

要在不验证表单的情况下执行操作(例如DELETE),您可以按如下方式使用process属性:

<p:commandButton action="#{processBean.process}" process="@this" /> 

请注意使用faces-redirect=true

返回目标链接
public String process() {
    ...
    return "/path/to/some.xhtml?faces-redirect=true";
}

答案 2 :(得分:0)

就我而言,以前没有一个暴露过的解决方案对我有用,因为我正在使用p:layout。所以,我在西部有一个p:panelMenu:layoutUnit,我的主窗体位于中心p:layoutUnit。然后,当我尝试导航到另一个页面时,菜单返回到其初始状态,我不知道为什么。

我现在的解决方案是以这种方式模拟来自支持bean中的action方法的jQuery单击:

RequestContext.getCurrentInstance().execute("$('#menuItemId').click()");

现在我可以保持p:panelMenu的状态并导航到所需的页面。

在第一个问题中,您还可以单击jQuery链接。我知道每个场景都不同,但无论如何你可以使用来自辅助bean的jQuery调用。