我有具有这种结构的 Spring Boot 项目
srs-
-main
-java
-MvcConfigurer.java
-LkApplication.java
-resource
-static
-css
-templates
-layout
-defoultLayout.ftl
-header.ftl
-footer.ftl
-index.ftl
在 defoultLayout.ftl
中我包含 CSS 文件
<head>
<meta charset="utf-8">
<link rel='stylesheet' href='../../static/css/bootstrap.min.css'>
<link rel='stylesheet' href='../../static/css/core.css'>
</head>
但它没有加载。我有一些具有类似结构的项目,它们都已加载,但没有。
我的类(class):
@SpringBootApplication
public class LkApplication {
public static void main(String[] args) {
SpringApplication.run(LkApplication.class, args);
}
}
和
@Configuration
@EnableWebMvc
public class MvcConfigurer extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
resolver.setPrefix("");
resolver.setSuffix(".ftl");
resolver.setContentType("text/html; charset=UTF-8");
return resolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
factory.setTemplateLoaderPaths("classpath:templates", "src/main/resource/templates");
factory.setDefaultEncoding("UTF-8");
FreeMarkerConfigurer result = new FreeMarkerConfigurer();
result.setConfiguration(factory.createConfiguration());
return result;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
如何将资源文件加载到我的 springboot 项目中?
我使用:springboot、freemarker、bootstrap
最佳答案
如果是maven或者gradle,标准文件夹结构应该是src/main/resources
。然后,构建会将它们放入您的(假设的)可执行 spring boot jar 中。
如果您没有安全阻止任何端点,您应该能够按如下方式引用这些文件;
<link rel='stylesheet' href='/css/core.css'>
注意“静态”已从路径中删除。
关于java - Spring boot项目未加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38089756/