Sprng MVC Thymeleaf href坏了

时间:2017-12-28 15:01:22

标签: spring-mvc thymeleaf

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Dark Flower Store - 闇の花屋</title>
</head>
<body>
<a th:href="@{/books}">Books</a>
<a href="/bookstore/books">Books</a>
</body>
</html>

第二个链接正常工作,而第一个链接成为纯文本。

页面源中的链接也会准确显示我在代码中输入的内容,而不是转换为链接。

这是HomeController:

package com.hanabinoir.bookstore;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

和pom.xml中的依赖项:

<dependencies>
    <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.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

eclipse STS加载工作区时出现NullPointerException错误,启动服务器时与JPA相关的错误很少。

  

错误677804 --- [ost-startStop-1]   org.hibernate.tool.hbm2ddl.SchemaExport:HHH000389:不成功:   drop table book ERROR 677804 --- [ost-startStop-1]   org.hibernate.tool.hbm2ddl.SchemaExport:Schema&#39; SA&#39;不存在

2 个答案:

答案 0 :(得分:0)

index.html 文件从 / webapp 文件夹移至 / templates 文件夹后,Thymeleaf引擎最终可以正常工作。

答案 1 :(得分:0)

您可能希望让您的href保持脱机状态 - 即通过查看您的html文件而不运行该应用程序。

<a href='book.html' th:href="@{/books}">Books</a>

请注意,当应用程序运行时,它将呈现th:href属性并忽略普通href。但是当您离线时,html将无法识别th:attrib的所有属性,只有普通的href才能正常工作。

它也适用于CSS。

<link rel="stylesheet" href="../static/css/style.css" th:href="@{/css/style.css}">

<script src="../static/js/jquery-1.10.2.js" th:src="@{/js/jquery-1.10.2.js}"></script>

这是从JSP等其他模板中使用Thymeleaf的优势之一。