JJexplode 2022-01-12 10:29 采纳率: 100%
浏览 55
已结题

Springboot在html中引入数组列表时无法获得成员变量

HTML

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<tr th:each="b:${books}" th:if="${b.price ge 3000}">
    <!-- <tr th:unless="${#strings.isEmpty(b.title)}">  -->
    <!-- <tr th:if="${not #strings.isEmpty(b.title)}"> -->
    <td>[[${b.isbn}]]</td>
    <td>[[${b.title}]]</td>
    <td>[[${b.price}]]</td>
</tr>
</body>
</html>

Book类

public class Book {
    private int id;
    private String isbn;
    private String title;
    private int price;
    private String attach;
    public Book(int id, String isbn, String title, int price, String attach) {
        this. id = id;
        this. isbn = isbn;
        this. title = title;
        this. price = price;
        this. attach = attach;
    }
}

Controller

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;

@Controller
public class ViewController {
    ArrayList<Book> bookList = new ArrayList<Book>();
    @GetMapping("/list")
    public String test01(Model model){
        int id = 0;
        String isbn = "ISN00";
        String title = "Sample0";
        int price = 2500;
        String attach = "cd";

        for(int i = 0 ; i < 10 ; i++) {
            id = id +  1;
            isbn = isbn  + 1;
            title = title +1;
            price = price +100;
            if(attach=="cd") {
                title = null;
                attach = "dvd";
            }else {
                attach = "dl";
                title = "dl";
            }
            Book book= new Book(id, isbn, title, price, attach);
            bookList.add(book);
            model.addAttribute("books",bookList);
        }
        return "list";
    }
}

页面上时报500的错误
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jan 12 10:21:48 CST 2022
There was an unexpected error (type=Internal Server Error, status=500).

HTML上显示Book类的price变量找不到

img

以为时Maven依赖的问题

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.14.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

尝试过以后都没用。

这是日志

2022-01-12 10:48:58.663 ERROR 5652 --- [nio-8080-exec-4] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-4] Exception processing template "list": An error happened during template parsing (template: "class path resource [templates/list.html]")
 
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/list.html]")
 
2022-01-12 10:48:58.665 ERROR 5652 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/list.html]")] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'price' cannot be found

  • 写回答

4条回答 默认 最新

  • 燕_青 2022-01-12 11:01
    关注

    是不是你的Book类没有加get,set方法啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 1月20日
  • 已采纳回答 1月12日
  • 修改了问题 1月12日
  • 创建了问题 1月12日

悬赏问题

  • ¥20 Windows 驱动开发版本疑问相关
  • ¥15 MAC 未能打开磁盘映像
  • ¥15 fastcap使用,二维导体输入问题
  • ¥15 hosts修改后不能访问
  • ¥15 关于化学反应速率C++编译问题/FLUENT
  • ¥20 Yolov5训练报错
  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 ASP.net Gridview插件数据更新失败问题
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高