Reputation: 2882
I have spring boot project with this structure
srs-
-main
-java
-MvcConfigurer.java
-LkApplication.java
-resource
-static
-css
-templates
-layout
-defoultLayout.ftl
-header.ftl
-footer.ftl
-index.ftl
in defoultLayout.ftl
I include CSS file
<head>
<meta charset="utf-8">
<link rel='stylesheet' href='../../static/css/bootstrap.min.css'>
<link rel='stylesheet' href='../../static/css/core.css'>
</head>
But it not loaded. I have some project with like structure and them all loaded but this not.
My classes:
@SpringBootApplication
public class LkApplication {
public static void main(String[] args) {
SpringApplication.run(LkApplication.class, args);
}
}
and
@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();
}
}
How can I load resources files to my springboot project?
I use: springboot,freemarker,bootstrap
Upvotes: 1
Views: 2443
Reputation: 298
What worked for me, was:
<link rel="stylesheet" href="bootstrap-3.3.7/css/bootstrap.min.css" />
<script src="jquery/jquery-3.2.1.min.js"></script>
<script src="bootstrap-3.3.7/js/bootstrap.min.js"></script>
The default location in SpringBoot is:
Which means the starting point is already in the right directory and you need to go from there, i.e.:
bootstrap-3.3.7/css/bootstrap.min.css
No slashes or anything upfront!
Upvotes: 0
Reputation: 12817
Spring Boot by default looks for resources at resources
directory. I had the same problem in my prject.
change the directory name resource
to resources
// note s at the end
after this change, refresh project, it will work
Upvotes: 0
Reputation: 151
If it's maven or gradle, the standard folder structure should be src/main/resources
. The build will then place them in your (assuming) executable spring boot jar.
Providing you don't have security blocking any endpoints you should be able to reference the files as follows;
<link rel='stylesheet' href='/css/core.css'>
note 'static' is stripped from the path.
Upvotes: 1