背景
我正在开发一个应用程序(使用 Spring MVC
),其基本路径为:
http://localhost:8080/myapplication/
我有一个样式表 /css/style.css
,我试图在 JSP 中用 绝对路径
引用它:
<link rel="stylesheet" href="/css/style.css" type="text/css" media="screen, projection">
问题
样式表永远不会在浏览器中加载。当我通过浏览器的查看源代码功能访问样式表链接时,该链接显示为:
http://localhost:8080/css/style.css
本来应该是:
http://localhost:8080/myapplication/css/style.css
在使用 Struts
时,我曾经使用 html:rewrite
标记来解决这个问题。 Spring MVC
中是否有任何等效的标记/技术?
感谢您的宝贵时间。
最佳答案
使用 JSTL c:url 标签。
<c:url value="/css/style.css" var="url" />
<link rel="stylesheet" href="${url}" type="text/css" media="screen, projection">
您还可以使用 pageContext 作为上下文路径的前缀。
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" type="text/css" media="screen, projection">
关于java - Spring MVC 绝对URL问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2521606/