Work fine in eclipse but "Can't find template" if run as jar

Viewed 1841

The program works fine in Windows with IDE, but when I release to the jar and run it Ubuntu, it won't render the HTML.

Src:

@Controller
@EnableAutoConfiguration
@RequestMapping("/")
public class JumanController {

    @GetMapping("/")
    public String index(Model model) {
        return "index";
    }

...

Main:

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        TomcatURLStreamHandlerFactory.disable();
        SpringApplication.run(JumanController.class, args);
    }
}

HTML:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
...

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    ...

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>
    </dependencies>

</project>

setting:

spring.thymeleaf.enabled=true 
spring.thymeleaf.prefix=classpath:/resources/templates/ 

I tried to change the prefix to classpath:/templates/, still not work.

Warning when server boot:

2018-09-21 10:46:47.154 WARN 4048 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

Error when access index:

2018-09-21 10:47:15.253 ERROR 4048 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "index": Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

Template dir:

src/main/resources/templates/index.html

Why look for the path src/main/templates/ not src/main/resources/templates/?

I only need a simple Spring boot contains a single static or template page, and could release to a jar. I removed the application.properties want to make everything run as default(AutoConfiguration), But the result is the same.

Any suggestion or hello world demo project will be helpful...

2 Answers

Solved:

  1. change templates dir from src/main/resources/templates/ to /resources/templates/. (same level with src)

  2. set spring.thymeleaf.prefix to classpath:/templates/

Source code: https://github.com/arkceajin/jumanpp-java

Make sure that your application.properties file has the following entry- spring.thymeleaf.prefix=classpath:/templates/

Related