Spring Boot - Thymeleaf template

user1578872

I am using Spring Boot 1.2.7 and Thymeleaf.

All the html pages are inside the src/main/resource/templates folder and everything works fine when I say return "<viewName>" inside the controller.

Now, I would like to use different folder structure for AngularJS.

Would like to move the pages to some other folder, say webapps/pages.

Tried configuring the resolver as below,

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ServletContextTemplateResolver getViewResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("webapps/pages/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("LEGACYHTML5");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

Still not working... Am I missing any other config or should I not use Thymeleaf in this case?

pom.xml for your reference,

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>
stevecross

You can configure one TemplateResolver inside the application.properties (or application.yml) file, if you do not want to use the standard configuration. You can find a list of available options in the docs.

Then introduce a new @Configuration for additional TemplateResolvers:

@Configuration
public class ThymeleafConfig {

    @Autowired
    private SpringTemplateEngine templateEngine;

    @PostConstruct
    public void init() {
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();

        resolver.setPrefix("webapps/pages/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("LEGACYHTML5");
        resolver.setOrder(templateEngine.getTemplateResolvers().size());

        templateEngine.addTemplateResolver(resolver);
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring boot - Thymeleaf template - multiple resolvers

Thymeleaf exceptions for resolving template in spring boot

Several template locations for Thymeleaf in Spring Boot

How to locate Thymeleaf template from spring boot

Spring boot Thymeleaf context parameter is not passed to template

Adding Custom Thymeleaf Template Resolver to Spring Boot

Spring Boot + Thymeleaf css is not applied to template

spring boot calling thymeleaf template failed

Template Parsing Error with Thymeleaf 3 and Spring Boot 2.1

Spring boot + Thymeleaf - multiple template resolvers with fallback to default

Error occuring template parsing. (Spring Boot + Thymeleaf)

Spring Boot cannot change Thymeleaf template directory with Java config

Error during template parsing.Spring boot and Thymeleaf

How to link another html page in Spring Boot Thymeleaf Template Engine?

Spring boot sending emails using Thymeleaf as template - configuration does not work

How to call methods from a Thymeleaf template in Spring Boot?

Configuration of thymeleaf text template in spring boot application.properties failed

spring boot and thymeleaf

Pagination with Spring Boot and Thymeleaf

Spring Boot, Thymeleaf and @Controller

Spring Boot, Thymeleaf and CSS

Issues with Spring Boot and Thymeleaf

thymeleaf caching with spring boot

Thymeleaf dropdown with Spring Boot

Spring mapping resources for template Thymeleaf

Spring - Thymeleaf: Exception processing template

Issue with spring boot's thymeleaf auto configuration trying to resolve error template for rest application

How do I return a template(thymeleaf) in spring boot and resolve it to a particular endpoint

Thymeleaf template (in Spring boot application) behind reverse proxy not forming url's correctly