简体   繁体   English

Java-管理上下文路径

[英]Java - Managing Context Path

I have been working on an application that is currently being to deployed to numerous environments. 我一直在研究目前正在部署到众多环境的应用程序。 However, we are having issues with the context path when deployed to certain environments. 但是,当部署到某些环境时,上下文路径存在问题。 This is a Java/Spring application running on Tomcat 6. The deployment process is handled by system administrators so I don't have much visibility into the process so I'm trying to come up with some ways of handling the scenario. 这是在Tomcat 6上运行的Java / Spring应用程序。部署过程由系统管理员处理,因此我对该过程没有太多的了解,因此我尝试提出一些处理方案的方法。 With all URLS, we prepend them with request.getContextPath() . 对于所有URL,我们在它们前面加上request.getContextPath() Under normal circumstances, this works fine. 在正常情况下,这可以正常工作。 For example, if WAR has the filename site1.war, it gets deployed to 例如,如果WAR的文件名为site1.war,它将被部署到

/site1/

However, we have a couple of other environments where the WAR is deployed and it contains a rootpath outside of the application root so it ends up with 但是,我们还有其他两个环境,其中部署了WAR,并且在应用程序根目录之外包含一个根路径,因此最终导致

/otherroot/site1/

So the application root path is /site1/ and is unaware of this other rootpath /otherroot/ . 因此,应用程序的根路径为/site1/ ,并且不知道该其他根路径/otherroot/ In this case, calling getContextPath() returns /site1/ when in reality the path that we want is /otherroot/site1/ . 在这种情况下,调用getContextPath()会返回/site1/ ,而实际上我们想要的路径是/otherroot/site1/

Now, one thing that has me confused is that when we use #{request.contextpath} within a JSF page when defining images (images, stylesheets or javascript files), the URL is correct when looking at the source. 现在,让我感到困惑的是,当我们在JSF页面中使用#{request.contextpath}定义图像(图像,样式表或javascript文件)时,URL在查看源代码时是正确的。 For example, given the /otherroot/site/ url, if I set 例如,给定/otherroot/site/网址,如果我设置的话

<img src="#{request.contextPath}/image1.png"/> , when I look at the source of the rendered page, I see /otherroot/site1/image1.png . <img src="#{request.contextPath}/image1.png"/> ,当我查看呈现页面的源时,我看到了/otherroot/site1/image1.png Now, if I set some JS variable to hold the context path every time the page is rendered, I get a different result. 现在,如果我每次设置页面时都设置一些JS变量来保存上下文路径,则会得到不同的结果。 For example, 例如,

<script>
    APP.ROOT_PATH = '#{request.contextPath}';
</script>

This renders the following: 这将呈现以下内容:

APP.ROOT_PATH = '/site1'

This is causing some issues because we make use of asynchronous requests so we use the url to properly request data from the client side. 这引起了一些问题,因为我们利用了异步请求,因此我们使用url正确地从客户端请求数据。

So my question is, what is the best strategy for writing an application that can run under any given path? 所以我的问题是,编写可以在任何给定路径下运行的应用程序的最佳策略是什么?

You can achieve this by using interceptors which would identify the environment for you. 您可以使用拦截器为您识别环境,从而实现这一目标。 You can pass the environment path through model to UI which could be used by UI. 您可以将环境路径通过模型传递给UI,UI可以使用该路径。 For eg. 例如。 /otherroot/site1/ , in this site1 is the context name and otherrrot is the environment. / otherroot / site1 / ,在此site1中是上下文名称,otherrrot是环境。

When yo hit your server to render a jsp, interceptor will analyze this url and come to know that url has something to do with otherroot1. 当您点击服务器以呈现jsp时,拦截器将分析该URL,并知道该URL与otherroot1有关。 This environemnt and related data can be set in model which could be used to fetch js, images etc and for any other ajax call. 可以在模型中设置此环境和相关数据,该模型可用于获取js,图像等以及用于任何其他ajax调用。

No need to rely on url context path once page is rendered. 呈现页面后,无需依赖url上下文路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM