Exception evaluating SpringEL expression, spring guru course

user8799994

Book.java

package pl.spring.guru.spring5webapp.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String title;
    private String isbn;
    @OneToOne
    private Publisher publisher;

   @ManyToMany
   @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = 
   "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id"))

   private Set<Author> authors = new HashSet<>();

   public Book() {
   }

   public Book(String title, String isbn, Publisher publisher){
       this.title=title;
       this.isbn=isbn;
       this.publisher=publisher;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getIsbn() {
      return isbn;
  }

   public void setIsbn(String isbn) {
      this.isbn = isbn;
  }

  public Publisher getPublisher() {
      return publisher;
  }

  public void setPublisher(Publisher publisher) {
     this.publisher = publisher;
  }

  public Set<Author> getAuthors() {
     return authors;
  }

  public void setAuthors(Set<Author> authors) {
     this.authors = authors;
  }
}

BookController.java

package pl.spring.guru.spring5webapp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pl.spring.guru.spring5webapp.repositories.BookRepository;

@Controller
public class BookController {

    private BookRepository bookRepository;

    public BookController(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @RequestMapping(value = "/books")
    public String getBooks(Model model){
        model.addAttribute("books",bookRepository.findAll());

    return "books";
  }
}

books.html - thymeleaf template

<!DOCTYPE html>
<html lang="en" xmlns:th=”http://www.thymeleaf.org”>
<head>
    <meta charset="UTF-8">
    <title>Spring Framework Guru</title>
</head>
<body>
<h1>Books list</h1>
<table>
   <tr>
       <th>ID</th>
       <th>Title</th>
       <th>Author</th>
   </tr>
   <tr th:each="book : ${books}">
        <td th:text="${book.id}">123</td>
       <td th:text="${book.title}">Spring in Action</td>
       <td th:text="${book.publisher.name}">Wrox</td>
   </tr>
</table>

</body>
</html>

when I'm trying go to localhost/books i get a

Exception evaluating SpringEL expression: "book.id" (template: "books" - line 16, col 13)

I'm just learning the spring and I'm starting to use the spring framework guru tutorial. I don't understand too, why adding a empty constructor in book.java is require. Without it i have next problem:

No default constructor for entity: : pl.spring.guru.spring5webapp.model.Book; nested exception is org.hibernate.InstantiationException: No default constructor for entity: : pl.spring.guru.spring5webapp.model.Book

Quagaar

You're missing a getter for id. Without that, the template engine cannot access it.

As for the default constructor - it is required by Spring to create an instance of the Entity. How should Spring know what parameters to use if no default constructor was available.

Apart from the tutorial you should take a look at the Spring Framework reference docs. It's a lot to read but especially the DI basic are explained quite good.

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Exception evaluating SpringEL with Spring MVC

"Exception evaluating SpringEL expression" error while trying to iterate List(<Object>) in Thymeleaf Spring Boot

How to fix "Exception evaluating SpringEL expression" error after submitting a variable Spring/Thymeleaf

how to resolve this : Exception evaluating SpringEL expression: "#authorization.expression('isAuthenticated()')"?

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

Exception evaluating SpringEL expression: "#dates.format(passation.datepassation, 'dd-MMM-yyyy')"

Thymeleaf: "Exception evaluating OGNL expression" in a simple for loop

erlang: exception error when evaluating an arithmetic expression

Getting class cast expression while evaluating the spring integration expression

Json to Spring Expression mapping exception

Evaluating an if expression behaves differently then evaluating a BOOL expression

Evaluating an expression in gekko

Evaluating Expression Tree

Evaluating call expression

evaluating statement cin==(expression)

Evaluating a mathematical expression in a string

CASE expression in MySQL not evaluating

Evaluating JavaScript expression with a worker

Evaluating an Expression using MVEL

Evaluating Expression in Function call

XPath expression not evaluating

Evaluating mathematical expression in R

Angular JS expression not evaluating

Ternary generator expression not evaluating

Evaluating expression in Lua in an Environment

Evaluating boolean expression with recursive

Evaluating a postfix expression

ng-if expression evaluating incorrectly

Evaluating the expression 1 <= month <= 12