Reputation: 4923
Is there anyway in JSF to get the URL, the scriptname, from the address bar that refers to the page I am on? What would that way be?
Upvotes: 1
Views: 81
Reputation: 1108722
To get the request URI in the backing bean you'd need to grab the HttpServletRequest
from under the JSF's covers:
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
// ...
It offers several methods to get information about the request, such as getRequestURI()
. Most of them are also available through getRequestXXX()
methods of the ExternalContext
, but the request URI not.
In the view, you can get it by #{request}
. E.g.
<a href="#{request.requestURI}">Click here to navigate recursively</a>
Alternatively, if you're actually interested in the JSF view ID (the "script name" as you say yourself), then rather use UIViewRoot#getViewId()
:
String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
// ...
or in the view itself:
<a href="#{request.contextPath}#{facesContext.viewRoot.viewId}">Click here to navigate recursively</a>
or without the context path:
<h:link outcome="#{facesContext.viewRoot.viewId}" value="Click here to navigate recursively" />
Upvotes: 1