Spring MVC没有加载静态文件

时间:2017-12-29 12:51:54

标签: css spring spring-mvc

问题: Spring有时加载css,有时不加载

当我最初运行项目时,我得到没有样式的html:

enter image description here

当我输入登录凭据并点击登录时,它会转到此URL:

http://localhost:8080/project/resources/img/facebook.png

如果我按下后退按钮(并执行注销),那么它会给我带有css的表单,如:

enter image description here

我可以按预期登录。

我的代码:在WebConfig.java

    @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
    registry.addResourceHandler("/img/**").addResourceLocations("classpath:/img/");
    registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
}

在Login.jsp中

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="resources/css/bootstrap.min.css" rel="stylesheet">
<!-- styles -->
<link href="resources/css/styles.css" rel="stylesheet">

任何想法为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

试试这个:

@Override
protected void addResourceHandlers( ResourceHandlerRegistry registry ) {
    registry.addResourceHandler( "/js/**", "/css/**", "/img/**" ).addResourceLocations( "classpath:js/", "classpath:css/", "classpath:img/" );
}

我认为文件夹:js/css/img/位于已属于您的类路径的resources文件夹中。