Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot - how to correctly define template locations?

After struggling for some time with a Spring app (and spring boot, for that matter), it seems I'm finally about to get it working.

I have shifted through dependency resolutions and maven build already. Application starts (and very quickly!) but when I try to access

localhost:8080

I get the following browser message whenever I try to reach the application's landing page:

HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "home/homeNotSignedIn", template might not exist or might not be accessible by any of the configured Template Resolvers

The src/main/resourcesfolder is

src/main/resources
    static // CSS, IMG and JS
    templates // html
    application.properties
    log4j.properties

Now, I understand I may be mixing concepts, but on my ApplicationConfiguration.java I have this:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "b.c.g.c")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    @Description("Thymeleaf template resolver serving HTML 5")
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");

        return templateResolver;
    }

    @Bean
    @Description("Thymeleaf template engine with Spring integration")
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addDialect(new SpringSecurityDialect());
        templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
        templateEngine.setTemplateResolver(templateResolver());

        return templateEngine;
    }

    @Bean
    @Description("Thymeleaf view resolver")
    public ViewResolver viewResolver() {

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setCache(false);
        viewResolver.setOrder(1);

        return viewResolver;
    }

    // other beans
}

And, on application.properties, I have this:

spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

Though I see these excerpts tell the same thing, I bet one can go, right?

So, two questions, actually,

1) how to make sure Spring + Thymeleaf understand where to find the templates?

2) how to get the application to answer to localhost:8080/appName instead of localhost:8080/?

like image 472
gtludwig Avatar asked Sep 01 '16 03:09

gtludwig


People also ask

What is the use of templates folder in spring boot?

The default template directory is src/main/resources/templates . This is the Maven build file. The spring-boot-devtools enables hot swapping, disables template cache and enables live reloading. The spring-boot-starter-thymeleaf is a starter for building Spring MVC applications with Thymeleaf.

Which is better FreeMarker or Thymeleaf?

In the pursuit of comparison between FreeMarker , Thymeleaf, Groovy and Mustache, FreeMarker has the upper hand in performance.

Which is the best template engine for spring boot?

1. JMustache is a template engine which can be easily integrated into a Spring Boot application by using the spring-boot-starter-mustache dependency. Pebble contains support for Spring and Spring Boot within its libraries.

What is better than Thymeleaf?

AngularJS, Vaadin, JSTL, Bootstrap, and React are the most popular alternatives and competitors to Thymeleaf.


1 Answers

Recommended Approach
I will first answer your second question

  1. You have define the application.properties or application.yml (Yaml is better) file in src/main/resources. Spring-Boot comes with default properties file where you can set your context path (look for webproperties), port everything.

     server.context-path=/<appname>
    
  2. To answer your second question spring-boot you can refer the thymeleaf configurations in the properties file.

like image 137
Praveen Kumar K S Avatar answered Oct 03 '22 10:10

Praveen Kumar K S