静态资源的 URI 中的上下文路径,我真的需要指定它吗?
我在 test.html 中有一个简单的 Web 应用程序
webapp
static
images
- a.gif
pages
- test.html
WEB-INF
pages
- test.jsp
,
<img src="/static/images/a.gif"/>
问题是在我将 uri 更改为之前图像不会显示,
<img src="/web app name/static/images/a.gif"/>
但我在 URI 处加载 test.html
http://server/web app name/static/pages/test.html
我在 web.xml 中配置了静态资源映射,如下所示。
<servlet>
<servlet-name>springWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resourceServlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>resourceServlet</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
我错过了什么吗?我确实想在开发阶段将这些静态资源保留在应用程序中,而不是将它们移动到 HTTP 服务器。
多谢。
I have a simple web app
webapp
static
images
- a.gif
pages
- test.html
WEB-INF
pages
- test.jsp
in test.html, there is
<img src="/static/images/a.gif"/>
the problem is that the image is not displaying until I change the uri to
<img src="/web app name/static/images/a.gif"/>
but I'm loading test.html at URI
http://server/web app name/static/pages/test.html
I configured static resources mapping in my web.xml as follows.
<servlet>
<servlet-name>springWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resourceServlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>resourceServlet</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
Am I missing anything? I do want to keep those static resources within the app in DEV phase instead of moving them to a HTTP server.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出于这个原因,最好使用 spring:url 标签或 JSTL c:url 标签将 URL 包装在 HTML 中。这些标签将自动添加上下文路径。
例如:
或者,您可以在开发中使用上下文路径“”。这样你的网址就会与生产相匹配。每个 servlet 容器的完成方式都不同 - 例如,对于 Tomcat,您可以将应用程序部署到 webapps/ROOT。
It's good practice to use either the spring:url tag or the JSTL c:url tag to wrap URLs in your HTML for this very reason. Those tags will automatically add the context path.
For example:
Alternatively you can use a context path of "" in development. That way your urls would match production. The way this is done is different for each servlet container - for example for Tomcat you would deploy your app to webapps/ROOT.
使其更通用的一种方法是使用
Alternative,如果您知道目录结构,则可以使用相对 url,例如
One way to make it a bit more generic is to use
Alternative if you know your directory structure you could use relative urls, such as