Thymeleaf的布局:在jar

时间:2017-09-24 18:57:53

标签: java spring maven spring-boot thymeleaf

所以,我正在编写一个以 Thymeleaf 作为模板引擎的Spring启动应用程序。 当我通过Spring Tool Suite运行我的项目时,一切都按预期工作。

然而,当我运行mvn clean package并运行生成的jar时,我的布局:片段停止工作,说它无法解决它:

  

org.thymeleaf.exceptions.TemplateInputException:解析错误   模板"布局",模板可能不存在或可能不存在   可由任何已配置的模板解析器(索引)

访问

我的layout.html:

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

    <head>
        <title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE" >my Site</title>
        <meta charset="UTF-8"/>
    </head>
    <body onload="init()">
        <header>
            <nav class="navbar navbar-inverse">
                <!-- Navigation bar -->
            </nav>
        </header>
        <section layout:fragment="vsebina" style="padding: 25px;"></section>
    </body>
</html>

和我的index.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:th="http://www.thymeleaf.org" 
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorate="~{layout}"
        layout:decorator="Layout">

    <head>
        <title>Index</title>
    </head>
    <body>
        <section layout:fragment="vsebina">
            ...
            Content of my index page
            ...
        </section>
    </body>
</html>

和我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>com.mypackage</groupId>
    <artifactId>mySite</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>mySite</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.21</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

似乎有些奇怪,在一种情况下它起作用而在另一种情况下它不起作用。或者我做错了什么?

1 个答案:

答案 0 :(得分:1)

显然问题出在html标签中:

<html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:th="http://www.thymeleaf.org" 
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorate="~{layout}"
        layout:decorator="Layout">

这需要改为:

<html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:th="http://www.thymeleaf.org" 
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorate="~{layout}"
        layout:decorator="layout">

layout:decorator="Layout"layout:decorator="layout"

在Spring Tool Suite中,资本L没有引起问题,但是当应用程序被打包到jar中时,它开始引起问题。