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

最后都变了- 提交于 2019-11-28 13:01:22

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!