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

蓝咒 提交于 2019-11-27 07:38:52

问题


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

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/syslac"/>

While in my view xhtml page I have this fragment code where p:commandButton has a oncomplete tag that will execute the handleLoginRequest function.

<h:form>
            <h:panelGrid columns="2" cellpadding="5">
               <h:outputLabel for="username" value="Usuario:" />
               <p:inputText value="#{loginBean.usuarioVendedor.usuarioSistema}" id="username" required="true" label="username" />
               <h:outputLabel for="password" value="Contrasena:" />
               <h:inputSecret value="#{loginBean.usuarioVendedor.clave}" id="password" required="true" label="password" />
               <f:facet name="footer">
                  <p:commandButton value="Ingresar" update=":growl" actionListener="#{loginBean.loguearse}" oncomplete="handleLoginRequest(xhr, status, args)" />
               </f:facet>
            </h:panelGrid>
         </h:form>

The script:

      <script type="text/javascript">function handleLoginRequest(xhr, status, args) 
{
                if (args.validationFailed || !args.loggedIn) {
                    jQuery('#dialog').effect("shake", {times: 2}, 100);
                } else {
                    dlg.hide();
                    jQuery('#loginLink').fadeOut();
                    window.location = args.view;
                }
}
</script>

But I can not retrieve the context path from META-INF/context.xml through logginBean so that I can send view arg to be used by window.location in navigation: /syslac/page.xhtml where syslac is context path of the application.


回答1:


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


来源:https://stackoverflow.com/questions/46556861/get-the-web-application-context-path-from-meta-inf-context-xml-to-produce-an-out

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