Primefaces: actionListener and redirection at the same time

I would like when I click on

<p:commandButton> 

a Bean is called for processing, and at the same time, the page must 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 do i do Is there a better way to achieve this?

+6
source share
3 answers

Use the action property 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 begin. But remember that you will need to define a navigation rule in faces-config.xml

+4
source

Use the action attribute, but don't save the actionListener anymore.

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

To perform an action without validating the form (e.g. DELETE), you can use the process attribute as follows:

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

Note returning destination link using faces-redirect=true

 public String process() { ... return "/path/to/some.xhtml?faces-redirect=true"; } 
+2
source

In my case, none of the previously open solutions worked for me, because I use p: layout. So I had p: ​​panelMenu in the west of p: layoutUnit, and my main form was in the center of p: layoutUnit. Then, when I tried to go to another page, the menu returned to its original state, and I do not know why.

My solution so far is to simulate a click with jQuery from an action method in a bean base as follows:

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

Now I can save the state p: panelMenu and go to the desired page.

In the first question, you can also click the link using jQuery. I know that each script is different, but you can use the jQuery call from the bean backup anyway.

0
source

All Articles