Makoto
Makoto

Reputation: 775

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 ?

Upvotes: 5

Views: 8896

Answers (3)

jmoran
jmoran

Reputation: 164

In my case, none of the before exposed solutions worked for me because I'm using p:layout. So, I had a p:panelMenu in the west p:layoutUnit and my main form was in the center p:layoutUnit. Then, when I tried to navigated to another page, the Menu returned to its initial state and I don't know why.

My solution by now is to simulate a click with jQuery from the action method in the backing bean in this way:

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

Now I can keep the state of the p:panelMenu and navigate to the desired page.

In the first question, you also can click the link with jQuery. I know that every scenario is different but in any case you can use a jQuery call from the backing bean.

Upvotes: 0

Pavel Sedek
Pavel Sedek

Reputation: 529

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";
}

Upvotes: 2

Makky
Makky

Reputation: 17463

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

Upvotes: 4

Related Questions