Java 使用springmvc进行静态内容呈现
我是春天的新手,正在尝试 我有一个jsp文件,其中包括css和图像,如下所示Java 使用springmvc进行静态内容呈现,java,spring,spring-mvc,Java,Spring,Spring Mvc,我是春天的新手,正在尝试 我有一个jsp文件,其中包括css和图像,如下所示 <link href="css/style.css" rel="stylesheet" type="text/css" /> 但是当点击路径/localhost/myapp/test/tst时,css和图像路径将解析为 /localhost/myapp/test/tst/css/style.css 实际上,我希望它是/localhost/myapp/css/style.css css和html存在于we
<link href="css/style.css" rel="stylesheet" type="text/css" />
但是当点击路径/localhost/myapp/test/tst
时,css和图像路径将解析为
/localhost/myapp/test/tst/css/style.css
实际上,我希望它是/localhost/myapp/css/style.css
css和html存在于webapp的根目录中
在我的spring配置中,我是如何实现以下目标的
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
您的CSS href当前相对于您的页面,您需要通过添加前导斜杠(加上上下文等)使其相对于服务器根。添加contextPath也是一个好主意,这样当您更改webapp名称或将其部署为根webapp时,这些资源仍然可以使用
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />
为什么不使用
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />