代码之家  ›  专栏  ›  技术社区  ›  Wolkenarchitekt

以编程方式获取当前页

  •  61
  • Wolkenarchitekt  · 技术社区  · 16 年前

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

    /myapplication . 有没有可能得到电流 页码 /home.faces ,怎么办?

    4 回复  |  直到 13 年前
        1
  •  118
  •   Mitch Talmadge    9 年前

    你通常想用 UIViewRoot#getViewId() 为了这个。

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

    #{view.viewId}
    

    正是这个值在导航情况下是可重用的,例如 <h:link outcome> <h:button outcome> .


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

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

    在EL中也可获得如下信息:

    #{request.requestURI}
    

    正是这个值在 <h:outputLink value> 或者普通的 <a href>

        2
  •  14
  •   Wolkenarchitekt    16 年前

    好的,明白了,是的

    FacesContext ctx = FacesContext.getCurrentInstance();
    HttpServletRequest servletRequest = (HttpServletRequest) ctx.getExternalContext().getRequest();
    // returns something like "/myapplication/home.faces"
    String fullURI = servletRequest.getRequestURI();
    
        3
  •  3
  •   Catfish    13 年前
    String uri = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURI();
    
        4
  •  -1
  •   jsina    13 年前
     String str = ((HttpServletRequest) FacesContext.getCurrentInstance() 
     .getExternalContext().getRequest()).getRequestURI(); 
     System.out.println(str);