Thymeleaf: org.thymeleaf.exceptions.TemplateInputException

q051

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)

Zortras

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Thymeleaf : org.thymeleaf.exceptions.TemplateInputException

Thyemleaf nested iteration triggers org.thymeleaf.exceptions.TemplateInputException

Getting org.thymeleaf.exceptions.TemplateInputException: Error resolving template

issue with springboot project : org.thymeleaf.exceptions.TemplateInputException

My spring-boot app gives the following error "org.thymeleaf.exceptions.TemplateInputException:"

org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="login", line 36 - column 3

org.thymeleaf.exceptions.TemplateInputException: Error resolving fragment: "${content}": template or fragment could not be resolved (template: "base"

how to get rid of org.thymeleaf.exceptions.TemplateInputException: while using thymeleaf expression to print data in form of bootstrap cards?

Spring Boot Testing Thymeleaf org.thymeleaf.exceptions.TemplateProcessingException

org.thymeleaf.exceptions.TemplateProcessingException: Concatenation href

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "ServiceAmc.id" (results:30)

org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context

Thymeleaf exceptions for resolving template in spring boot

Thymeleaf: ClassNotFoundException: org.unbescape.html.HtmlEscape

Can not import org.thymeleaf.templatemode.TemplateMode

Spring Boot + Thymeleaf ERROR java.lang.ClassNotFoundException: org.thymeleaf.dom.Attribute

Error while rendering HTML template using Thymeleaf 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'

Thymeleaf registration page - Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'

THYMELEAF with Post Request : Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'

Could not find artifact org.thymeleaf.extras:thymeleaf-extras-springsecurity:pom

org.thymeleaf.templatemode.StandardTemplateModeHandlers can't be resolved

Failed to instantiate [org.thymeleaf.templateresolver.ServletContextTemplateResolver]: No default constructor found

If statement in thymeleaf

If/else in thymeleaf

Templates with Thymeleaf

Reflection and thymeleaf

Thymeleaf with Datatables

Markdown in Thymeleaf

Thymeleaf and @Uniqueconstraint