以编程方式获取当前页面

以编程方式获取当前页面

问题描述:

在JSF支持bean(Managed Bean,Weld Bean,无所谓)中,我可以通过调用

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();

这为我提供了客户端当前访问的路径,例如 / myapplication
是否也可以获得当前的页面,例如 /home.faces ,以及如何获得?

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?

您通常要使用 UIViewRoot#getViewId()

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

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

这在EL中也可用如下:

This is in EL also available as follows:

#{view.viewId}

正是这个值可在导航案例结果中重复使用,例如< h:链接结果> < h:按钮结果>

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

或者,您也可以使用 HttpServletRequest#getRequestURI()获取最终用户在浏览器地址栏中实际看到的内容。

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();

EL中的哪个也可用如下:

Which is in EL also available as follows:

#{request.requestURI}

正是这个值可在< h:outputLink值> 或普通< a href> 中重复使用。请注意,您不能将其用作导航案例结果。

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