配置 Tomcat webapp 以提供来自外部目录的静态内容 [英] Configuring a Tomcat webapp to serve static content from external directory

查看:66
本文介绍了配置 Tomcat webapp 以提供来自外部目录的静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不是太琐碎 - 我对 Tomcat 管理很陌生.

I hope this is not too trivial - I'm very new at Tomcat administration.

我正在为全新的 Web 应用程序构建开发部署流程.我们在不同的目录中拥有客户端资产和客户端将与之通信的 Web 服务.这些服务由 Tomcat 6 webapp 提供,位于/my-app/services/.

I'm building the development deployment process for a brand-new web application. We have the client assets and the web services the client will talk to in separate directories. The services are served from a Tomcat 6 webapp, being served at /my-app/services/.

我需要更新配置,以便 Tomcat Web 应用程序将提供来自客户端目录以及 Web 服务的静态客户端资产.理想情况下,这些静态资产将从/my-app/提供,而服务目录仅保留用于动态内容.

I need to update the configuration such that the Tomcat webapp will serve up the static client assets from the client directory as well as the web services. Ideally, these static assets will be served from /my-app/, with just the services directory reserved for the dynamic content.

目前 tomcat 配置了单个 webapp,其中包含 META-INF 中的一个简单的 context.xml.我尝试在 server.xml 中手动设置两个上下文,并在 tomcat/conf//中设置两个上下文(分别将它们命名为 my-app#services.xml 和 my-app.xml,并设置 my-app.xml 到客户端资产目录的相对路径).像这样设置会导致 Web 服务内容不再成功启动(未能找到所需的类),但静态内容现在从正确的路径提供.

Currently tomcat is configured with the single webapp containing a trivial context.xml in META-INF. I have tried setting up two contexts manually in server.xml, and also setting both contexts up in tomcat/conf// (naming them my-app#services.xml and my-app.xml respectively, and setting the docBase of my-app.xml to the relative path to the client assets directory). Setting it up like this results in the web services content no longer successfully starting (failing to find the required classes), but the static content is now served from the correct path.

我真的不知道在这里做什么.如果有 Tomcat 经验的人可以为这个问题勾勒出适当的解决方案,我将不胜感激.如果我上面描述的功能不可用,那么小的改动是完全可行的——我只是想让一些东西工作,这样我就可以回去写代码了!

I'm really at a loss as to what to do here. If somebody with Tomcat experience could just sketch out the appropriate solution for this problem, I would be so grateful. If the functionality I've described above is not workable, minor changes are totally feasible - I just want to get something working so that I can go back to writing code!

注意:我们希望将静态客户端资产与服务分开,因为这可以让我们保持两个系统解耦,还可以让我们更轻松地在客户端部署中做一些花哨的事情,例如查看源文件和重新运行当情况发生变化时进行单元测试.

NB: We want to keep the static client assets separate from the services, as this lets us keep the two systems decoupled, and also lets us more easily do fancy things with client deployment such as watching the source files and re-running the unit tests when things change.

预先感谢您提供的任何帮助.

Thanks in advance for any help offered.

推荐答案

你应该在 server.xml 中添加
<Context path="/my-app/static" docBase="/someFolder/my-app/static-web" debug="0" reloadable="true" crossContext="false"/>

You should add in server.xml
<Context path="/my-app/static" docBase="/someFolder/my-app/static-web" debug="0" reloadable="true" crossContext="false"/>

使用斜线进行实验.如果斜线不正确,它将不起作用.
路径 - 是可以访问静态文件的 URL.
docBase - 包含静态文件的硬盘文件夹
我猜你不能在一条路径上映射很多上下文.

Experiment with slashes. It wouldn't work if slashes is incorrect.
path - is URL where your static files will be accessible.
docBase - folder at hard disk with static files
I guess you cannot map many contextes on single path.

另一个技巧是编写一个 servlet/filter,它将从文件夹返回静态上下文

Another trick is to write a servlet/filter which will return static context from folder

这篇关于配置 Tomcat webapp 以提供来自外部目录的静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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