更改Tomcat Web应用程序上下文 [英] Changing Tomcat web application context

查看:136
本文介绍了更改Tomcat Web应用程序上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,它的设计始终在根上下文(/)下工作。所有 css js 以及以 / 开头的链接(例如 /css/style.css )。现在我需要将此Web应用程序移动到某个不同的上下文(假设 / app1 )。可以轻松更改 server.xml 配置文件,并使用<$ c $在新上下文中显示Web应用程序 app1 c> [Context path =] item。但网页被破坏是因为所有链接现在都不正确。它们指向旧的根上下文 /css/style.css 而不是新的 app1 上下文。
有没有什么神奇的方法来解决这个问题而不通过添加一些上下文变量来修复每个链接?
使用的服务器 - Tomcat 5.应用程序是用Java编写的,使用JSP,Struts2,Spring和UrlRewrite过滤器。更有趣的是听到与理论辩论中的这些问题作斗争的真实经验。
谢谢。

I have a web application, which was designed and always worked under root context ("/"). So all css, js and links started with / (for example /css/style.css). Now I need to move this web application to some different context (let's say /app1). It is easy to change server.xml configuration file and bring up web application in new context app1 using [Context path=""] item. But web pages are broken because all links are now incorrect. They point to old root context /css/style.css instead of new app1 context. Is there any magic way to fix this problem without fixing each link by prefixing with some "context" variable? Used server - Tomcat 5. Application is written with Java and uses JSP, Struts2, Spring and UrlRewrite filter. More interesting is to hear real experience in fighting with such problems that theoretical debates. Thank you.

P.S。我没有看到UrlRewrite过滤器如何帮助我,因为它只能在 app1 上下文中使用。所以对 /css/style.css 等链接的请求将不会传递给它。

P.S. I do not see how UrlRewrite filter can help me because it will work only in app1 context. So requests to links like /css/style.css will not be passed to it.

推荐答案

您应该始终通过URL重写来创建URL,不仅可以将会话信息添加到URL(如果需要),还可以添加上下文路径。你应该从应用程序的顶部创建所有url作为绝对路径,然后让url-rewriting句柄在适当的时候将context-path添加到前面。

You should always create urls via url re-writing, not only so that session info can be added to the url, if required, but also so that the context path can be added. You should create all urls as absolutely paths from the top of the application and then let url-rewriting handle adding the context-path to the front, as appropriate.

<c:url value="/css/site.css"/>

这将呈现 /& lt; context-path& gt; / jss / site.css /& lt; context-path& gt; /css/site.css; jsessionid = 134345434543 进入jsp文件,如果他们没有启用cookie。您还可以使用c:url标记将url呈现为​​变量,然后在整个文档中多次使用该变量。只需在标记中添加 var =x属性,然后 $ {x} 就会将网址呈现给您文档。如果您没有使用jsp来渲染输出,则需要为视图层找到适当的机制,但它们都有一个。如果你在java代码中渲染一个url,只需查看c:url标记的源代码,你就会看到它是如何完成的。

That will render /&lt;context-path&gt;/css/site.css or /&lt;context-path&gt;/css/site.css;jsessionid=134345434543 into a jsp file if they don't have cookies enabled. You can also use the c:url tag to render the url into a variable and then use that variable multiple times throughout your document. Just add a var="x" attribute to the tag and then ${x} will render the url into your doc. If you aren't using jsp to render your output, you'll need to find the appropriate mechanism for your view layer, but they will all have one. If you are rendering a url in java code, just take a look at the source code to the c:url tag and you'll see how it is done.

一个尴尬是CSS文件(和js文件)没有被处理,所以css和js文件中的url需要是相对路径,否则每当你更改上下文路径时它们都会中断。大多数js已经使用了相对路径,因为库维护者不知道你要将库安装到哪个路径。另一方面,CSS背景图像通常被指定为绝对URL,因为相同的CSS文件可以包含在文件层次结构的不同级别的html文件中。除了创建适当的符号链接以使相对URL始终有效或者通过JSP提供问题CSS文件以便可以适当地重写URL时,我没有简单的解决方法。我确定你可以运行的过滤器或apache模块可以更换网址,但是当你部署到新的上下文路径时,你仍然必须手动更新你的过滤器/模块。

The one awkwardness is that CSS files (and js files) aren't processed, so urls in css and js files need to be relative paths or they will break whenever you change the context path. Most js already uses relative paths since library maintainers don't know what path you are going to install their library to. CSS backround images, on the other hand, are often specified as absolute urls, since the same CSS file may be included into html files at different levels of a file hierarchy. There is no easy fix for this that I am aware of other than to create appropriate symlinks such that the relative url always works or else serve up the problem CSS files via a JSP so that the urls can be rewritten as appropriate. I'm sure there are probably filters or apache modules you can run which will do the url replacement, but then you've still got to manually update your filter/module whenever you deploy to a new context path.

这篇关于更改Tomcat Web应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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