Spring mvc:在URL末尾添加斜杠时css不起作用

时间:2015-08-20 11:18:17

标签: css spring spring-mvc url

我是Spring MVC的新手,我遇到了CSS问题。当URL以斜杠结尾时CSS不起作用。
链接就像这样     <link rel="stylesheet" href="themes/style.css">
 mvc:资源映射
<mvc:resources mapping="/themes/**" location="/WEB-INF/themes/"/>
和requestMapping就是这样的

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

    ModelAndView model = new ModelAndView("login");

    return model;
}

所以问题是当我输入像../login这样的网址时,css会正常加载,但是当我输入带有结尾斜杠的../login/时,则不会加载css。 那么这里有很多类似的问题,但它们都不适用于Spring MVC。

2 个答案:

答案 0 :(得分:4)

而不是

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

试试这个:

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

当您使用href="themes/style.css"时,对于网址,例如:.../login/,css文件的请求网址如下所示:

.../login/themes/style.css

这是不正确的。当您使用href="/themes/style.css"时,它始终应该以:

结果
.../themes/style.css

<强>更新

如果是jsp页面,则添加 {$ 1}}位于页面顶部 并改变:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

进入

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

答案 1 :(得分:1)

它将100%工作

第1步

<mvc:resources location="/WEB-INF/assets/" mapping="/resources/**"></mvc:resources>

setp 2

<spring:url var="css" value="/resources/css/"/>
<spring:url var="js" value="/resources/js/"/>

<spring:url var="image" value="/resources/image/"/> 

在值

后添加斜杠

第3步

像这样添加你的样式表

 <link rel="stylesheet" 
    href="${css}/common/bootstrap.min.css">