Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...