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 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导