Thymeleaf + springboot不能一起工作

时间:2016-10-18 21:38:49

标签: jsp spring-mvc intellij-idea spring-boot thymeleaf

我想使用带百里香的sp​​ringboot。我创建了一个控制器,如下所示:

我有这样的项目结构:

package com.mbtimeet.controllers;

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


@Controller
public class HomeController {

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

我有一个index.html:

<!DOCTYPE html>
<html xmlns:th=“http://www.thymeleaf.org”         xmlns="http://www.w3.org/1999/html">
<head>
</head>
<body>
Hi there.
</body>
</html>

它只是不起作用。在我打开&#34; http://localhost:8080/hello&#34;页面我得到一个例外。

日志:http://pastebin.com/MLPgpqph

Build.gradle:

buildscript {
    ext {
        springBootVersion = '1.4.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

jar {
    baseName = 'mbti-meet'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-devtools")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

错误:

2016-10-18 22:58:15.413 ERROR 4896 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="index", line 2 - column 16] with root cause

我遵循了一个教程,唯一的区别是用户使用了maven。

1 个答案:

答案 0 :(得分:2)

日志消息告诉您确切的问题。出于某种原因,当您创建index.html文件时,xmlns:th=“http://www.thymeleaf.org”获得了UTF-8格式的引号而不是ascii的引号"。因此,Thymeleaf在解析文件时遇到问题。将属性更新为xmlns:th="http://www.thymeleaf.org"