user5620472 user5620472 - 5 months ago 52
Java Question

Spring boot project not load resources

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

Answer

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.

Comments