java - Spring - 无法加载资源(CSS)

标签 java css spring spring-mvc

好吧,我已经在网上寻找了 2 天的解决方案,但无论我尝试什么都没有效果,我希望这里有人可以解决我的问题:

我有一个 Spring 元素,当我用服务器加载并打开它时,我无法将 css 文件加载到 index.html 中(是的,我不使用 jsp):

为了清楚起见,我将发布所有相关代码:

index.html(你可以看到我尝试了很多路径_)

<html>
<head>
    <title>CouponSystem Home Page</title>

    <link rel="stylesheet" href="couponsystem/resources/css/style.css" type="text/css" />
    <link rel="stylesheet" href="/couponsystem/resources/style.css" type="text/css" />
    <link rel="stylesheet" href="couponsystem/resources/style.css" type="text/css" />
    <link rel="stylesheet" href="resources/css/style.css" type="text/css" />
    <link rel="stylesheet" href="/resources/css/style.css" type="text/css" />
    <link rel="stylesheet" href="resources/style.css" type="text/css" />
    <link rel="stylesheet" href="/resources/style.css" type="text/css" />
</head>
<body>
<h2>CouponSystem- Home Page</h2>
<hr>

<div class="red-text">Red text</div>
    <br>
    <div class="green-text">Green text</div>
<hr>
</body>
</html>

应用配置:

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.orel.couponsystem")
@PropertySource({ "classpath:persistence-mysql.properties" })
public class AppConfig extends WebMvcConfigurationSupport implements 
ApplicationContextAware {

@Autowired
private Environment env;

private ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
}

@Bean
public ViewResolver viewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine((ISpringTemplateEngine) templateEngine());
    resolver.setCharacterEncoding("UTF-8");
    return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setEnableSpringELCompiler(true);
    engine.setTemplateResolver(templateResolver());
    return engine;
}

private ITemplateResolver templateResolver() {
    SpringResourceTemplateResolver resolver = new 
  SpringResourceTemplateResolver();
    resolver.setApplicationContext(applicationContext);
    resolver.setPrefix("/WEB-INF/view/");
    resolver.setTemplateMode(TemplateMode.HTML);
    return resolver;
    }

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

我的元素结构: my project structure:

enter image description here

样式.css

body{
    background-color:red;
}
.button {
  font-size: 20px;
  background: #ccc;
}

.red-text {
  color: red;
  font-size: 30px;
}

.green-text {
  color: green;
  font-size: 20px;
}

非常感谢!!!

更新: **还更新了 AppConfig 我的结构: structure with index file

最佳答案

如果您使用的是 spring 而不是建议使用 'src/main/resource',那么请使用 Artifact sprint-sample-mvc 进行更正。

关于java - Spring - 无法加载资源(CSS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52311401/

相关文章:

java - 如何在Windows下翻译JFileChooser列标题?

java - Java 1.6 中的 future 任务

javascript - 锚定html框架

javascript - 如何使用 jquery 将 div 动画化为不同的方向并淡出

html - 更多元素在一个div中的相对位置

css - Spring Boot - 不能包含静态资源

java - 如何拒绝使用 JFileChooser 选择相同的文件

java - 为什么 ArrayList 不覆盖 equals() 以获得更好的性能?

java - Spring Web 响应式客户端

ajax - 如何将 csrf token 添加到 ajax 请求