Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primefaces: actionListener and redirection at the same time

I would like when I click on a

   <p:commandButton>

,a Bean for processing is called and at the same time, the page should be redirected to another page.

In other words, I would like to combine:

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

And:

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

where "redirection" is configured in faces-config.xml

How should I do ? Is there a better way to achieve this ?

like image 774
Makoto Avatar asked Feb 14 '14 14:02

Makoto


2 Answers

Use action property of p:commandButton.

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

change it to

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

First, actionListener will be called then navigation will kick in. But remember you'll need to define the Navigation rule in faces-config.xml

like image 80
Makky Avatar answered Nov 14 '22 20:11

Makky


Use action attribute, but don't keep actionListener anymore.

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

To perform the action without validating the form (eg. DELETE), you could then use process attribute as follows:

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

Note returning the target link with faces-redirect=true

public String process() {
    ...
    return "/path/to/some.xhtml?faces-redirect=true";
}
like image 33
Pavel Sedek Avatar answered Nov 14 '22 22:11

Pavel Sedek