Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current page programmatically

Tags:

java

jsf

jsf-2

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?

like image 529
Wolkenarchitekt Avatar asked Jul 15 '10 08:07

Wolkenarchitekt


2 Answers

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.

like image 184
BalusC Avatar answered Sep 26 '22 12:09

BalusC


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();
like image 26
Wolkenarchitekt Avatar answered Sep 22 '22 12:09

Wolkenarchitekt