How to return to the same JSF page when clicking a button from another JSF page

I have two JSF pages, suppose that A and B. From these two pages A and BI, you can go to page C. Now on page C a button (OK button) appears, clicking on which it should go back to A or B depending from where (or A or B), page C was called. Any help would be greatly appreciated.

+4
source share
2 answers

Solution Using View Parameter

The idea is that you can pass the current view identifier as a parameter to get the next (landing) page. On the landing page, you can use it to go back.

Basic example:

Types A and B:

<h:body> Hello from page A (B) <h:link value="Go to page C via link" outcome="target"> <f:param name="backurl" value="#{view.viewId}"/> </h:link> <h:form> <h:commandButton value="Go to page C via command button" action="#{baseBean.doAction}"/> </h:form> </h:body> 

View C:

 <f:metadata> <f:viewParam name="backurl" value="#{backBean.backurl}"/> </f:metadata> <h:body> Hello from page C <h:link value="Go back via link" outcome="#{backBean.backurl}"> </h:link> <h:form> <h:commandButton value="Go to page C via command button" action="#{backBean.back}"/> </h:form> </h:body> 

Bean Pages A / B:

 @ManagedBean @RequestScoped public class BaseBean { public String doAction() { String url = FacesContext.getCurrentInstance().getViewRoot().getViewId(); return "/target?faces-redirect=true&backurl=" + url; } } 

Bean Page C:

 @ManagedBean @RequestScoped public class BackBean { private String backurl; public String back() { return backurl + "?faces-redirect=true"; } public String getBackurl() { return backurl; } public void setBackurl(String backurl) { this.backurl = backurl; } } 

The last thing to mention is that the view identifier may differ from the URL in the web browser.

Solution enhancement when only <h:link>

Taking into account the legitimate comment of BalusC and its previous answer to the question, Canceling a button does not work in case of a verification error , if you do not need to use <h:commandButton> on the landing page, therefore, when returning to the previous page, you do not need to perform any business tasks, you can leave the task <h:link> . In this case, the support of a bean (target view) is not needed at all, and the structure of the target view can be minimized to:

 <f:metadata> <f:viewParam name="backurl"/> </f:metadata> <h:body> Hello from page C <h:link value="Go back" outcome="#{backurl}" rendered="#{not empty backurl}"/> </h:body> 
+6
source

You can implement the comesFrom property in your Beanclass. This property you can specify the name of your page, where you came from, when you go to page C ( A if it is on page A, B if it is on page B).
By clicking on the "OK" button on page C you can check your comesFrom property and return to the property value.

0
source

All Articles