以编程方式获取当前页面 [英] Get current page programmatically

查看:120
本文介绍了以编程方式获取当前页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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.

这篇关于以编程方式获取当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆