css - Spring MVC 不加载静态文件

标签 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">

知道为什么会这样吗?

最佳答案

试试这个:

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

我假设文件夹:js/css/img/resources 中已经属于您的类路径的文件夹。

关于css - Spring MVC 不加载静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48022918/

相关文章:

带有日期的 Spring MVC 输入标记引发错误 : HTTP Status 400 -The request sent by the client was syntactically incorrect

javascript - 使用 jQuery 确定视口(viewport)大小

html - 将列表放入一页并保持元素比例

java - 使用 Spring MVC 返回一个 PDF 文件

java - 使用 Spring websockets 的自定义 Stomp header

java - 如何检查我的交易方式是否真的支持交易?

css - 在已使用 Skype 插件更改的 CSS 中设置电话号码的样式

javascript - 附加奇怪的行为

java - Spring MVC : Error 400 The request sent by the client was syntactically incorrect

spring-mvc - Redis 间歇性超时