Thymeleaf: org.thymeleaf.exceptions.TemplateInputException



我正在尝试使用thymeleaf与spring MVC基于Java的配置。然后使用JPA连接到H2DB。当我启动spring boot并连接到"http://localhost: 8080/"时,我得到以下错误。我无法访问"index.html"

template "index":模板解析时发生错误(template: "类路径资源[templates/index.html]")org. thymleaf .exceptions. templateinputexception:模板解析时发生错误(template: "类路径资源[templates/index.html]")java.lang.IllegalStateException: BindingResult和bean名称'Book'的普通目标对象都不能作为请求属性

控制器类

package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.model.Book;
import com.example.demo.repository.Repository;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Controller
public class BookController{
private final Repository repository;

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

@PostMapping("/add")
public String addBook(@ModelAttribute Book book) {
repository.save(book);
return "redirect:/";
}
}

模型类

package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;

private String title;
private String detail;
}
□repository interface
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.model.Book;
public interface Repository extends JpaRepository <Book, Long>{
}

index . html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>H2DB</title>
</head>
<body>
<form th:action="@{/add}" th:object="${Book}" method="post">
<label for="title">title:</label> <input type="text" th:field="*{itle}">
<label for="detail">detail:</label> <input type="text"
th:field="*{detail}">
<button>登録する</button>
</form>
</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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>H2DB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>H2DB</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<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.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

我使用的是Eclipse Version: 2021-09 (4.21.0)

尝试在Model的index方法中传递一个空book对象。我想你可以用数据填满它。

相关内容

  • 没有找到相关文章

最新更新