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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化