从 META-INF/context.xml 获取 Web 应用程序上下文路径以生成导航结果 [英] Get the web application context path from META-INF/context.xml to produce an outcome for navigating

查看:19
本文介绍了从 META-INF/context.xml 获取 Web 应用程序上下文路径以生成导航结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 tomcat 8 上运行的 primefaces Web 应用程序.在 META-INF/context.xml 中,我定义了以下内容:

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"/>

虽然在我的视图 xhtml 页面中,我有这个片段代码,其中 p:commandButton 有一个 oncomplete 标记,它将执行 handleLoginRequest 函数.

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>

脚本:

      <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>

但我无法通过 logginBean 从 META-INF/context.xml 检索上下文路径,以便我可以发送视图 arg 供 window.location 在导航中使用:/syslac/page.xhtml 其中 syslac 是应用程序的上下文路径.

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.

推荐答案

上下文路径位于 ExternalContext#getRequestContextPath().

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

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

所以你可以这样做:

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

请注意,当用作 JSF 导航结果时,这完全没有必要.有关正确方法,请参阅底部的第二个另请参阅"链接.

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

上下文路径可在 EL 中通过 HttpServletRequest#getContextPath().

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

#{request.contextPath}

所以你可以这样做:

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

或者当您的脚本位于 .js 文件中时(正确做法!):

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;

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆