【问题标题】:Thymeleaf's layout:fragment not working after packaged in jarThymeleaf 的布局:在 jar 中打包后片段不起作用
【发布时间】:2018-03-05 17:54:06
【问题描述】:

所以,我正在编写一个以 Thymeleaf 作为模板引擎的 Spring Boot 应用程序。 当我通过 Spring Tool Suite 运行我的项目时,一切正常。

但是,当我运行 mvn clean package 并运行生成的 jar 时,我的 layout:fragment 停止工作,说它无法解决它:

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>

在一种情况下它有效而在另一种情况下却无效,这似乎很奇怪。还是我做错了什么?

【问题讨论】:

    标签: java spring maven spring-boot thymeleaf


    【解决方案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 中时,它开始出现问题。

    【讨论】:

    • 大胆猜测:你是在windows机器上开发的?在 Windows 上,文件系统不区分大小写。在 linux 上(或在 zip 容器内,如 jar 文件),文件名区分大小写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多