plp
plp

Reputation: 3

could not access the attributes of objects while in implementing thymeleaf in spring

i cant seem to access the attributes of the objects passed to the html filein thymeleaf. I am building a spring REST Web application.

CODE IN CONTROLLER

@RequestMapping(value = {"/home/books"}, method = RequestMethod.GET)
    public ModelAndView books()
    {
        ModelAndView modelAndView=new ModelAndView();
        Book book =new Book("name","author","id","54");
        //book.addBook(book("name","author","id","54"));
        List<Book> AllBook =bookService.getAllBooks();
        AllBook.add(book);
        modelAndView.addObject("books",AllBook);
        modelAndView.setViewName("books");
        return modelAndView;
    }

books.html file in my template

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
    <head>
        <title>Books</title>
    </head>
    <body>

        <tr th:each="book: ${books}">
            <h1> <td th:text="${book.author}" /></h1>
            <td th:text="${book.name}" />
        </tr>
    </body>
</html>

POJO class

Public class Book implements Serializable{
    @Id
    private String id;
    private String name;
    private String author;
    private String price;
    private int quantity;

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Book(String name, String author, String id, String price) {
        super();
        this.name = name;
        this.author = author;
        this.id = id;
        this.price = price;
    }
    public Book()
    {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }    
}

thee web page dosent show the values of book.author or book.name. IT should show name , author.The page itself is responding but no values are shown. This error is shown There was an unexpected error (type=Internal Server Error, status=500). Exception evaluating SpringEL expression: "book.name" (template: "books" - line 11, col 9)

Did i make any mistakes using thymeleaf? please help

Upvotes: 0

Views: 706

Answers (2)

ISlimani
ISlimani

Reputation: 1673

Define public setters and getters in your POJO

@Entity
public class Book{
    @Id
    private String id;
    private String name;
    private String author;



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    private String price;
    private int quantity;

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Book() {

    }
    public Book(String id, String name, String author, String price, int quantity) {
        super();
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
        this.quantity = quantity;
    }



}

books.html file

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="~{default}">
<head>
<title>Books</title>
</head>
<body>
    <table>
        <tr th:each="book: ${books}">
            <h1>
                <td th:text="${book.author}"></td>
            </h1>
            <td th:text="${book.name}"></td>
        </tr>
    </table>
</body>
</html>

Bookrepository

public interface Bookrepository extends JpaRepository<Book, String>{    

}

BookService

public interface BookService {

    public List<Book> getALlBooks();

    public void addBook(Book book);
}

BookService Implementation

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    Bookrepository bookrepository;

    @Override
    public List<Book> getALlBooks() {

        return bookrepository.findAll();
    }

    @Override
    public void addBook(Book book) {

        bookrepository.save(book);
    }

}

Your Controller

@Controller("/")
public class BooksController{

    @Autowired
    BookService bookService;

    @RequestMapping(value = "home/books", method = RequestMethod.GET)
    public ModelAndView books() {
        ModelAndView modelAndView = new ModelAndView();
        Book book = new Book("id", "name1", "author1", "54", 5);
        Book book2 = new Book("id 2", "name2", "author2", "54", 4);

        bookService.addBook(book);
        bookService.addBook(book2);

        List<Book> AllBook = bookService.getALlBooks();


        // Check here if list is empty and throw an exception

        modelAndView.addObject("books", AllBook);
        modelAndView.setViewName("books");
        return modelAndView;
    }
}

Explanation

Your first problem is caused because you didn't define setters and getter. You had private fields with no way to access them outside your class.

You second problem is caused because you pass empty list. Make sure to check for your variables before passing them. A simple logger won't do you anything bad. Most of the time, it comes very handy.

Upvotes: 1

Alien
Alien

Reputation: 15878

are you sure your html file name is 'book.html' not 'books.html'?

If it is book.html then you should set view name modelAndView.setViewName("book");

otherwise change file name to books.html and set view as modelAndView.setViewName("books");

Upvotes: 0

Related Questions