user1578872
user1578872

Reputation: 9048

Spring Boot - Thymeleaf template

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>

Upvotes: 0

Views: 1698

Answers (1)

stevecross
stevecross

Reputation: 5684

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);
    }

}

Upvotes: 1

Related Questions