更改 Tomcat Web 应用程序上下文
我有一个网络应用程序,它是在根上下文(“/”)下设计并始终工作的。因此,所有 css
、js
和链接均以 /
开头(例如 /css/style.css
)。现在我需要将此 Web 应用程序移动到某个不同的上下文(假设为 /app1
)。使用 [Context path=""]
项可以轻松更改 server.xml
配置文件并在新上下文 app1
中启动 Web 应用程序。但网页已损坏,因为所有链接现在都不正确。它们指向旧的根上下文 /css/style.css
而不是新的 app1
上下文。 有没有什么神奇的方法可以解决这个问题,而无需通过添加一些“上下文”变量作为前缀来修复每个链接? 使用的服务器 - Tomcat 5。应用程序是用 Java 编写的,并使用 JSP、Struts2、Spring 和 UrlRewrite 过滤器。更有趣的是听到与理论争论的此类问题作斗争的真实经验。 谢谢。
PS 我不知道 UrlRewrite 过滤器如何帮助我,因为它只能在 app1
上下文中工作。因此,对 /css/style.css
等链接的请求将不会传递给它。
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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 URL 重写将 ROOT 重定向到您的应用程序,这是否会消除应用程序处于 ROOT 状态的能力?如果是这样,通过切换上下文可以获得什么?
我认为链接资源的一般方法是附加一个“上下文”变量并使链接绝对:
${pagecontext.request.contextpath}/css/style.css
或仅使链接相对:css/style.css
除非您有无法修改代码的特定原因,否则我会在链接上进行搜索/替换并完成它。您应该查找不超过三到四个表达式,/css、/images、/javascript 等。
If you use URL rewriting to redirect ROOT to your application, won't that eliminate the ability to have a an application in ROOT? If so, what is gained by switching the context?
I think the general way to link resources is to either append a "context" variable and make the link absolute:
${pagecontext.request.contextpath}/css/style.css
or just make the link relative:css/style.css
Unless you have specific reasons for being unable to modify the code, I would do a search/replace on the links and be done with it. You should have no more than three or four expressions to find, /css, /images, /javascript, etc.
您应该始终通过 url 重写来创建 url,这样不仅可以在需要时将会话信息添加到 url,而且还可以添加上下文路径。您应该将所有 url 创建为应用程序顶部的绝对路径,然后让 url 重写处理将上下文路径添加到前面(如果合适)。
这将渲染
/<context-path>/css/site.css
或/<context-path>/css/site.css;jsessionid =134345434543
到 jsp 文件中(如果没有启用 cookie)。您还可以使用 c:url 标记将 url 呈现到变量中,然后在整个文档中多次使用该变量。只需向标记添加var="x"
属性,然后${x}
就会将 url 呈现到您的文档中。如果您不使用 jsp 来呈现输出,则需要为视图层找到适当的机制,但它们都有一个。如果您在 java 代码中渲染 url,只需查看 c:url 标记的源代码,您就会看到它是如何完成的。一个尴尬之处在于 CSS 文件(和 js 文件)不会被处理,因此 css 和 js 文件中的 url 需要是相对路径,否则每当您更改上下文路径时它们就会中断。大多数 js 已经使用相对路径,因为库维护者不知道您要将其库安装到哪个路径。另一方面,CSS 背景图像通常被指定为绝对 URL,因为相同的 CSS 文件可能包含在文件层次结构的不同级别的 html 文件中。据我所知,除了创建适当的符号链接以使相对 URL 始终有效或通过 JSP 提供有问题的 CSS 文件以便可以适当地重写 URL 之外,没有简单的解决办法。我确信您可能可以运行一些过滤器或 apache 模块来进行 url 替换,但是每当您部署到新的上下文路径时,您仍然需要手动更新您的过滤器/模块。
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.
That will render
/<context-path>/css/site.css
or/<context-path>/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 avar="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.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.