Tomcat 在 Spring MVC 应用程序上提供静态资源
我正在构建一个 Spring MVC 应用程序,并且 frontController servlet 映射在“/”中拦截所有请求,我能够从 tomcat 提供静态内容(.js、.css、.png...)不是到了春天。 我的应用程序结构是
-webapp/
styles/
images/
WEB-INF/
views/
默认的,因为 frontController 映射到我的应用程序的上下文根上,它处理所有请求,但不提供任何静态资源。 静态资源的 mvc 配置如下。
<mvc:resources mapping="/resources/**" location="/"/>
该页面的代码是:
<img src="resources/images/logo.png" />
我需要配置 Tomcat 来为静态资源提供服务没有 spring 交互。
有什么建议吗?
I'm building a Spring MVC application, and the frontController servlet is mapped in "/" intercepting all requests, I'd to be able to serve the static contents (.js,.css,.png...) from tomcat and not by Spring.
My app structure is
-webapp/
styles/
images/
WEB-INF/
views/
By default, because the frontController is mapped on the context root of my app its handles all requests but don't serve any static resource.
The mvc configurarion for static resources is follow.
<mvc:resources mapping="/resources/**" location="/"/>
And the page's code is:
<img src="resources/images/logo.png" />
I need to configure Tomcat to serve the static resources with no spring interaction.
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以重新映射 tomcats 默认 servlet(处理静态内容),例如
You can remap tomcats default servlet (which handles static content), e.g.
查看此邮件列表线程,看看是否这就是你正在寻找的。
Have a look at this mailing list thread and see if that does what you're looking for.
另一个潜在的解决方案 - 只需将以下内容添加到 Spring DispatcherServlet.xml (Spring 文档)
优点(与 @nos 的解决方案相比)
为它们创建额外的 url 映射。
缺点
此外,无论人们喜欢哪种解决方案,我建议将以下内容添加到您的 web.xml 以防止目录列表(例如 URL/图像)
Another potential solution - Just add the following to your Spring DispatcherServlet.xml (Spring Docs)
Pros (as compared to @nos's solution)
to create additional url-mappings for them.
Cons
Additionally, irrespective of the solution that one prefers, I'd suggest adding the following to your web.xml to prevent directory listings (on, say URL/images)