简体   繁体   English

在春季实施百里香叶时无法访问对象的属性

[英]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. 我似乎无法访问传递到百里香html文件中的对象的属性。 I am building a spring REST Web application. 我正在构建一个Spring REST Web应用程序。

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 我模板中的books.html文件

<!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 POJO课

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. 您的网页上将显示book.author或book.name的值。 IT should show name , author.The page itself is responding but no values are shown. IT应该显示name,author。页面本身正在响应,但未显示任何值。 This error is shown There was an unexpected error (type=Internal Server Error, status=500). 显示此错误发生意外错误(类型=内部服务器错误,状态= 500)。 Exception evaluating SpringEL expression: "book.name" (template: "books" - line 11, col 9) 评估SpringEL表达式的异常:“ book.name”(模板:“ books”-第11行,第9行)

Did i make any mistakes using thymeleaf? 我使用百里香做任何错误吗? please help 请帮忙

Define public setters and getters in your POJO 界定公共settersgetters在你的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 books.html文件

<!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 Bookrepository

public interface Bookrepository extends JpaRepository<Book, String>{    

}

BookService bookService的

public interface BookService {

    public List<Book> getALlBooks();

    public void addBook(Book book);
}

BookService Implementation BookService实施

@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 . 您遇到的第一个问题是因为您没有定义settersgetter 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. 一个简单的logger不会对您有任何不好的影响。 Most of the time, it comes very handy. 大多数时候,它非常方便。

are you sure your html file name is 'book.html' not 'books.html'? 您确定HTML文件名是'book.html'而不是'books.html'吗?

If it is book.html then you should set view name modelAndView.setViewName("book"); 如果是book.html,则应设置视图名称modelAndView.setViewName("book");

otherwise change file name to books.html and set view as modelAndView.setViewName("books"); 否则,将文件名更改为books.html并将视图设置为modelAndView.setViewName("books");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM