64

In a JSF backing bean (Managed Bean, Weld Bean, doesn't matter), I can get the context path the client is on by calling

FacesContext ctx = FacesContext.getCurrentInstance();
String path = ctx.getExternalContext().getRequestContextPath();

This gives me the path the client currently accesses, like /myapplication. Is it also possible to get the current page, like /home.faces, and how?

Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174

4 Answers4

122

You normally want to use UIViewRoot#getViewId() for this.

String viewId = facesContext.getViewRoot().getViewId();

This is in EL also available as follows:

#{view.viewId}

Exactly this value is reuseable in navigation case outcomes such as <h:link outcome> and <h:button outcome>.


Alternatively, you can also use HttpServletRequest#getRequestURI() to get whatever the enduser is actually seeing in the browser address bar.

String uri = ((HttpServletRequest) externalContext.getRequest()).getRequestURI();

Which is in EL also available as follows:

#{request.requestURI}

Exactly this value is reuseable in <h:outputLink value> or plain <a href>. Note that you can't use it as navigation case outcome.

Mitch Talmadge
  • 4,638
  • 3
  • 26
  • 44
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    This does not reliably work. With PrettyFaces for example this does not give the URL that was called to create the current page. – DoubleMalt Mar 25 '12 at 20:10
  • 1
    @DoubleMalt: OP didn't indicate that he is using PrettyFaces. – BalusC Mar 25 '12 at 20:12
  • 5
    While it is true he also did not explicitly state that he didn't. My point was simply that libraries that extend JSF might break your solution (and PrettyFaces is a pretty common enhancement as the standard implementation's handling of navigation is pretty clumsy). I just wanted to give people that find out, that your clean solution doesn't work for them a clue why. – DoubleMalt Apr 12 '12 at 11:58
  • @DoubleMalt: I don't do PrettyFaces, but I'd be very surprised if its view handler doesn't determine the proper URL for a given navigation case outcome. – BalusC Dec 02 '12 at 00:56
  • 1
    @DoubleMalt, Prettyfaces is supposed to be a mere url prettyfier. Your statement of `the standard implementation's handling of navigation is pretty clumsy`... Well, prettyfaces does nothing about navigation that JSF doesn't. It just maps its url's to a prettier ones. It also has parameter related functionalities, but I don't consider them to be navigation specific. – Aritz Jun 13 '14 at 22:01
  • This sounds like a good idea, however casting getRequest to HttpServletRequest will throw the following error: ClassCastException: com.liferay.portlet.RenderRequestImpl cannot be cast to javax.servlet.http.HttpServletRequest Seems like getRequest() will return a PortletRequestImpl, and I am not sure how to get the URL using such class. – Gaara Aug 20 '14 at 22:41
  • 1
    @Gaara: apparently you're developing a portlet app, not a servlet app. Sorry, portlets are beyond me. Just search around how to get request URI from a `PortletRequest` instance. – BalusC Aug 21 '14 at 05:49
14

Ok, got it, it's

FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest servletRequest = (HttpServletRequest) ctx.getExternalContext().getRequest();
// returns something like "/myapplication/home.faces"
String fullURI = servletRequest.getRequestURI();
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
3
String uri = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURI();
Catfish
  • 18,876
  • 54
  • 209
  • 353
-2
 String str = ((HttpServletRequest) FacesContext.getCurrentInstance() 
 .getExternalContext().getRequest()).getRequestURI(); 
 System.out.println(str);
jsina
  • 4,433
  • 1
  • 30
  • 28