1

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?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
JoJo
  • 3,993
  • 8
  • 36
  • 63

1 Answers1

1

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" />

Related:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452