Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用springmvc进行静态内容呈现_Java_Spring_Spring Mvc - Fatal编程技术网

Java 使用springmvc进行静态内容呈现

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

我是春天的新手,正在尝试

我有一个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存在于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" />