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>
source share