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日

悬赏问题

  • ¥15 FOR循环语句显示查询超过300S错误怎么办
  • ¥15 个人通讯录管理系统 C语言 程序设计 需要能运行成功的 结构体和数组 Visual C++编译器
  • ¥15 数电设计题 没有设计思路 不知道用什么芯片进行设计 求提供设计思路
  • ¥15 在动态多目标优化问题中,第一幅图展示的是问题DF6的相关定义和绘制的POS和POF图,请问图中公式PS(t)和PF(t)是如何推导的
  • ¥60 先数学建模,接着设计一种优化算法结合案例给出智能仓储四向穿梭车的调度计划
  • ¥15 Errno2:No such file or directory,在当前文件确实没有该图片,怎么解决?
  • ¥15 博世摄像头数据存储的问题(iscsi)
  • ¥15 如何实现对学生籍贯信息管理系统的选择排序
  • ¥15 写一个51单片机的时钟代码
  • ¥15 git clone报错