以编程方式获取当前页面
在JSF支持bean(Managed Bean,Weld Bean,无所谓)中,我可以通过调用客户端的上下文path
FacesContext ctx = FacesContext.getCurrentInstance(); String path = ctx.getExternalContext().getRequestContextPath();
这为我提供了客户端当前访问的path,如/myapplication 。 是否也可以获取当前页面 ,如/ /home.faces ,以及如何?
您通常要使用UIViewRoot#getViewId() 。
String viewId = facesContext.getViewRoot().getViewId();
这在EL中也可用如下:
#{view.viewId}
这个值在导航案例结果中是可重复使用的,如<h:link outcome>和<h:button outcome> 。
或者,您也可以使用HttpServletRequest#getRequestURI()获取最终用户在浏览器地址栏中实际看到的任何内容。
String uri = ((HttpServletRequest) externalContext.getRequest()).getRequestURI();
EL中的哪个也可用如下:
#{request.requestURI}
这个值在<h:outputLink value>或普通的<a href>是可<h:outputLink value> 。 请注意,您不能将其用作导航案例结果。
好的,明白了,是的
FacesContext ctx = FacesContext.getCurrentInstance(); HttpServletRequest servletRequest = (HttpServletRequest) ctx.getExternalContext().getRequest(); // returns something like "/myapplication/home.faces" String fullURI = servletRequest.getRequestURI();
String uri = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURI();
String str = ((HttpServletRequest) FacesContext.getCurrentInstance() .getExternalContext().getRequest()).getRequestURI(); System.out.println(str);