Henrique Ordine
Henrique Ordine

Reputation: 3337

So how do I set a Thymeleaf template resolver for my springboot webapp then

I built a spring-boot webapp with a single home.html file under src/main/resources/template and when I return "home" from my spring controller it works perfectly. I'm packaging it as a war and it runs wonderfully with spring-boot's embedded tomcat server. If I run it with mvn spring-boot:run, that is.

But later if I start my application with java -jar myjar.war it claims the server has started up and all is well, but when I try to access it at localhost:8080 it fails with:

Error resolving template "home", template might not exist or might not be accessible by any of the configured Template Resolvers

So what do I have to do next then?

This is my pom.xml, by the way:

  <groupId>com.ciber</groupId>
  <artifactId>energyworx-conversion-tool</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

  <properties>
    <java.version>1.7</java.version>    
    <start-class>com.ciber.ewct.WebApp</start-class>
  </properties>

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>            
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>          
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin> 
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <!-- <jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000</jvmArguments> -->
            </configuration>
        </plugin>
    </plugins>
  </build>

After turning debug logging level on, I see this about thymeleaf configuration, which is the same for when I run it with mvn spring-boot:run and java -jar:

[THYMELEAF] * Cache Factory implementation: org.thymeleaf.cache.StandardCacheManager
[THYMELEAF] * Template modes:
[THYMELEAF]     * LEGACYHTML5
[THYMELEAF]     * XHTML
[THYMELEAF]     * HTML5
[THYMELEAF]     * VALIDXML
[THYMELEAF]     * VALIDXHTML
[THYMELEAF]     * XML
[THYMELEAF] * Template resolvers (in order):
[THYMELEAF]     * org.thymeleaf.templateresolver.TemplateResolver
[THYMELEAF] * Message resolvers (in order):
[THYMELEAF]     * org.thymeleaf.spring4.messageresolver.SpringMessageResolver
[THYMELEAF] * Dialect [1 of 2]: org.thymeleaf.spring4.dialect.SpringStandardDialect
[THYMELEAF]     * Prefix: "th"
[THYMELEAF] * Dialect [2 of 2]: nz.net.ultraq.thymeleaf.LayoutDialect
[THYMELEAF]     * Prefix: "layout"
[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK

Upvotes: 1

Views: 2197

Answers (2)

Henrique Ordine
Henrique Ordine

Reputation: 3337

I seem to have fixed it, with @DanielLavoie's help, by reverting all the changes I made to make the application a deployable war.

Those were:

  • remove the <packaking>war</packaging> so that a jar is generated instead;
  • remove <scope>provided</scope> from the dependency spring-boot-starter-tomcat;
  • remove extends SpringBootServletInitializer from my Application class;

I don't like the solution because according to the documentation I followed you'd be able to make the war deployable and also standalone enabled. But it'll have to do for now.

Upvotes: 0

Daniel Lavoie
Daniel Lavoie

Reputation: 1922

I'm sad to say that you are actually doing it wrong.

You configured your build to exclude the webapp server from your package.

<packaking>war</packaging>
...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

With this setup, you have a war that can be deployed inside an existing Servlet container. You won't be able to start your app with a java -jar command.

If you want to run you webapp with a java command line. Replace your packaging to jar and remove the provided instruction from your pom.

<packaking>jar</packaging>
...
<!--
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>
-->

Then you will have an embedded tomcat inside your jar and the command java -jar myjar.jar will properly.

Edit : Finally, I would raise the log level on Spring Web package to see how the Template Resolver tries to lookup your template.

Regards, Daniel

Upvotes: 1

Related Questions