静态资源的URI中的上下文路径,我真的需要指定它吗? [英] Context path in URIs of static resources, do I really need to specify it?
问题描述
我有一个简单的网络应用
webapp
$ p test.html中有$ p>
静态
图片
- 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>
我错过了什么吗?我确实希望在DEV阶段将这些静态资源保留在应用程序中,而不是将它们移动到HTTP服务器。
非常感谢。
解决方案出于这个原因,最好使用spring:url标记或JSTL c:url标记来包装HTML中的URL。这些标签会自动添加上下文路径。
例如:
< img src =< spring:url value ='/ static / images / a.gif'/>/>
或者,您可以在开发中使用的上下文路径。那样你的网址就会与生产相匹配。对于每个servlet容器,这样做的方式是不同的 - 例如对于Tomcat,您可以将应用程序部署到webapps / ROOT。
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.
解决方案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:
<img src="<spring:url value='/static/images/a.gif'/>"/>
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.
这篇关于静态资源的URI中的上下文路径,我真的需要指定它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!