关于java:在eclipse中工作正常,但如果以jar运行,则”找不到模板”

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

该程序在带有IDE的Windows中可以正常运行,但是当我释放到jar中并在Ubuntu下运行它时,它不会呈现HTML。

Src:

1
2
3
4
5
6
7
8
9
10
11
@Controller
@EnableAutoConfiguration
@RequestMapping("/")
public class JumanController {

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

...

主要:

1
2
3
4
5
6
7
8
9
@SpringBootApplication
public class App
{
    public static void main( String[] args )
    {
        TomcatURLStreamHandlerFactory.disable();
        SpringApplication.run(JumanController.class, args);
    }
}

HTML:

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

pom.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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>
            spring-boot-starter-web</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            spring-boot-starter-thymeleaf</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>
    </dependencies>

</project>

设置:

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

我试图将prefix更改为classpath:/templates/,但仍然无法正常工作。

服务器启动时的警告:

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)

访问索引时出错:

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

模板目录:

1
src/main/resources/templates/index.html

为什么要查找路径src/main/templates/而不是src/main/resources/templates/

我只需要一个简单的Spring boot,其中包含一个statictemplate页面,并且可以释放到jar中。我删除了application.properties,希望使所有内容都默认运行(AutoConfiguration),但是结果是相同的。

任何建议或世界演示项目都将有所帮助...


确保您的application.properties文件具有以下条目-
spring.thymeleaf.prefix =类路径:/ templates /


已解决:

  • 将模板目录从src/main/resources/templates/更改为/resources/templates/。 (与src处于同一级别)

  • spring.thymeleaf.prefix设置为classpath:/templates/

  • 源代码:https://github.com/arkceajin/jumanpp-java