Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/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
Spring mvc springmvc资源路径_Spring Mvc - Fatal编程技术网

Spring mvc springmvc资源路径

Spring mvc springmvc资源路径,spring-mvc,Spring Mvc,我已将.JSP页面的样式设置为: <link rel="stylesheet" type="text/css" href="resources/styles/main.css" /> 它适用于以下链接: localhost/webapp localhost/webapp/dir 但是当我尝试“localhost/webapp/dir/0”时,我得到一个404错误代码 servlet context.xml: <mvc:resources mapping="/resou

我已将.JSP页面的样式设置为:

<link rel="stylesheet" type="text/css" href="resources/styles/main.css" />

它适用于以下链接:

  • localhost/webapp
  • localhost/webapp/dir
但是当我尝试“localhost/webapp/dir/0”时,我得到一个404错误代码

servlet context.xml

<mvc:resources mapping="/resources/**" location="/resources/" />

您的路径是相对的,因此如果以不同的“深度”呈现JSP页面,链接将失败。要避免这种情况,请通过添加前导斜杠来使用绝对URL路径。Spring/servlet容器可以为您自动插入URL的上下文部分,这样,如果您将应用程序部署为“根”webapp,它将工作。要做到这一点,您可以使用
标记,但我更喜欢用以下代码少写一行JSP代码:

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/styles/main.css" />

Nickdos的答案是正确的,但我通常以不同的方式解决这个问题。你可以利用。您在每页的标题部分声明它,所有相关链接(
)和资源(
,…
)都将基于此href。因此,使用EL您可以做类似的事情(抱歉,但我目前无法尝试):