Home > Enterprise >  Thymeleaf: org.thymeleaf.exceptions.TemplateInputException
Thymeleaf: org.thymeleaf.exceptions.TemplateInputException

Time:09-23

i am trying to use thymeleaf with spring mvc java based configuration.Then use JPA to connect to H2DB. When I boot spring boot and connect to "http: // localhost: 8080 /", I get the following error. I can't get to "index.html".

template "index": An error happened during template parsing (template: "class path resource [templates/index.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/index.html]") java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'Book' available as request attribute

Controller Class

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:/";
        }
}

model class

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>


My 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>

I use Eclipse Version: 2021-09 (4.21.0)

CodePudding user response:

Try to pass an empty book object in the index method by the Model. I think then you can fill it with data.

  • Related