小编典典

Spring MVC:在URL末尾添加斜杠时CSS不起作用

spring-mvc

我是Spring MVC的新手,但是CSS还是有问题。如果网址以斜杠结尾,则CSS不起作用。
链接像这样
<link rel="stylesheet" href="themes/style.css">
mvc:resources映射
<mvc:resources mapping="/themes/**" location="/WEB-INF/themes/"/>
和requestMapping像这样

@RequestMapping("/login")
public ModelAndView loginPage() {

    ModelAndView model = new ModelAndView("login");

    return model;
}

所以问题是当我像../login输入CSS 一样正常输入URL
时,但是当我../login/以斜杠结尾时,则CSS不会加载。嗯,这里有很多类似的问题,但是都不是Spring MVC的问题。


阅读 336

收藏
2020-06-01

共1个答案

小编典典

代替

<link rel="stylesheet" href="themes/style.css">

尝试这个:

<link rel="stylesheet" href="/themes/style.css">

当您将href="themes/style.css"then用于url时:.../login/css文件的请求url看起来像:

.../login/themes/style.css

这是不正确的。当您使用时,href="/themes/style.css"它总是会导致:

.../themes/style.css

更新:

如果是jsp页面,则<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>在页面顶部添加 并更改:

<link rel="stylesheet" href="themes/style.css">

进入

<link rel="stylesheet" href="<c:url value="/themes/style.css" />">
2020-06-01