How I can set my Views files(.html) resource from "webapp" to "resources" folder in SpringMVC project

Viewed 55

I want to know how I can set my Views files(.html) resource from "webapp" to "resources" folder in SpringMVC project.

this is my project directory

Here is my config file code, now my .html files resource folder is in webapp folder, and I want to change it to resources folder.

@Configuration
@EnableWebMvc
@ComponentScan("Controllers")
public class ApplicationConfig implements WebMvcConfigurer {
    private final ApplicationContext applicationContext;

    @Autowired
    public ApplicationConfig(ApplicationContext applicationContext){
        this.applicationContext = applicationContext;
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver(){
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("resources");
        templateResolver.setSuffix(".html");
        return templateResolver;
    }

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

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {//to set viewer
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        registry.viewResolver(resolver);
    }

}
2 Answers

Today I faced the same problem. I need to transfer templates, css files and images from the webapp folder to the resources folder. In search of an answer, I went through the code of stores that are publicly available on github. I found a project in which all these resources are stored in the resources folder. As I understand it, another class is used there to configure the application. Here is the code. So far, I have not been able to make it work in my project, but I will dig in this direction. Perhaps this will help you too.

package com.geekbrains.geekspring.config;

import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

@Configuration
public class AppConfig extends WebMvcAutoConfiguration {
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        if (!registry.hasMappingForPattern("/webjars/**")) {
//            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
//        }
//    }
}

Ok, I seem to have solved the problem. I think you should be able to too. You only need to change one line of code. My mistake was that when I specified the path in this line: templateResolver.setPrefix("/WEB-INF/views/");

I thought I was specifying it in MyProjectName/src/main/webapp/WEB-INF/views/ This is where my templates used to be. But in fact, I specified the path in the server folder that is generated during compilation based on the folders we created in src. Now this folder is called "target". So, in fact, I pointed the way to MyProjectName/target/shop/WEB-INF/views/

Now I have moved all my templates to MyProjectName/src/main/resources/templates folder Since a new folder has appeared here, then on its basis in the "target" directory at startup project generated a new folder along the path MyProjectName/target/shop/WEB-INF/classes/templates

Accordingly, I indicated a new path following this logic templateResolver.setPrefix("/WEB-INF/classes/templates/");

And everything worked! It remains to transfer css and images.

my project directory

My config file

package ru.shop.three_d_print.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.config.annotation.*;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import javax.sql.DataSource;

@Configuration
@ComponentScan("ru.shop.three_d_print")
@EnableWebMvc

public class SpringConfig implements WebMvcConfigurer
{
  private final ApplicationContext applicationContext;

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

@Autowired
public SpringConfig(ApplicationContext applicationContext)
{
    this.applicationContext = applicationContext;
}

@Bean
public SpringResourceTemplateResolver templateResolver()
{
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(applicationContext);
    templateResolver.setPrefix("/WEB-INF/classes/templates/");
    templateResolver.setSuffix(".html");
    return templateResolver;
}

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

@Override
public void configureViewResolvers(ViewResolverRegistry registry)
{
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine());
    registry.viewResolver(resolver);
}
}
Related