从META-INF/context.xml获取Web应用程序上下文路径,以产生导航结果 [英] Get the web application context path from META-INF/context.xml to produce an outcome for navigating
问题描述
我有一个运行在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
中检索上下文路径,因此我可以发送导航:/syslac/page.xhtml
中window.location要使用的视图arg,其中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.
推荐答案
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.
<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;
另请参见:
- 如何获取基本URL?
- 要用于链接/的URL导航到其他JSF页面
- How get the base URL?
- What URL to use to link / navigate to other JSF pages
See also:
这篇关于从META-INF/context.xml获取Web应用程序上下文路径,以产生导航结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!