Get the web application context path from META-INF/context.xml to produce an outcome for navigating

前端 未结 1 1064
慢半拍i
慢半拍i 2020-12-11 12:22

I have a primefaces web application running on tomcat 8. In META-INF/context.xml I defined the following:



        
相关标签:
1条回答
  • 2020-12-11 12:35

    The context path is in backing bean available by ExternalContext#getRequestContextPath().

    String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
    

    So you could do for example:

    String loginURI = contextPath + "/login.xhtml";
    // ...
    

    Note that this is completely unnecessary when using as JSF navigation outcome. For the correct approach, see the second "See also" link in bottom.

    The context path is available in EL by HttpServletRequest#getContextPath().

    #{request.contextPath}
    

    So you could do for example:

    <h:outputScript>
        // ...
        window.location = "#{request.contextPath}" + args.view;
    </h:outputScript>
    

    Or when your script is in a .js file (correct practice!):

    <html lang="en" data-baseuri="#{request.contextPath}">
    

    window.location = document.documentElement.dataset.baseuri + args.view;
    

    See also:

    • How get the base URL?
    • What URL to use to link / navigate to other JSF pages
    0 讨论(0)
提交回复
热议问题