如何用thymeleaf读取html中的列表

oknrviil  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(237)

我有以下html代码:

<form id="tableForm" th:object="${teachersLessonsRows}" method="post">
    <table class="table">
        <caption>My lessons</caption>
        <tr>
            <th>Name</th>
            <th>Student's Name</th>
            <th>Lesson's day</th>
            <th>Estimate</th>
        </tr>
        <th:block th:each="teachersLessonsRow, i:${teachersLessonsRows}">
            <tr th:each="studentEstimates, j:${teachersLessonsRow.getStudentEstimates()}">
                <td th:text="${teachersLessonsRow.getLessonName()}"></td>
                <td th:text="${studentEstimates.getStudentName()}"></td>
                <td th:text="'#'+${studentEstimates.getDayNum()}"></td>
                <td>
                    <input th:id="${teachersLessonsRow.getLessonName()+' '+studentEstimates.getStudentName()+' '+studentEstimates.getDayNum()}"
                           type="text" th:text="${studentEstimates.estimate}" onkeyup="checkEdit(this.id);"
                           th:field="*{add(${teachersLessonsRow})}">
                </td>
            </tr>
        </th:block>
    </table>

当我在intelij中启动我的代码时,我得到了这个错误:错误12079---[nio-8080-exec-8]org.thymeleaf.templateengine:[thymeleaf][http-nio-8080-exec-8]异常处理模板“lessonsteacher”:在执行处理器“org.thymeleaf.spring5.processor.springinputgeneralfieldtagprocessor”时出错(模板:“lessonsteacher”-第53行,第36列)由以下原因引起:org.springframework.beans.notreadablepropertyexception:bean类[java.util.arraylist]的无效属性“add(${teacherslessonsrow})”:bean属性“add(${teacherslessonsrow})”不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?
我知道这一行th:field=“*{add(${teacherslessonsrow})”有错误,但在哪里?
关于我的java课程:

public class TeachersLessonsRow {
    private String lessonName;
    private List<StudentEstimate> studentEstimates;

    TeachersLessonsRow() {}

    public TeachersLessonsRow(String lessonName, List<StudentEstimate> studentEstimates) {
        this.lessonName = lessonName;
        this.studentEstimates = studentEstimates;
    }

    public String getLessonName() {
        return lessonName;
    }

    public void setEventName(String eventName) {
        this.lessonName = eventName;
    }

    public List<StudentEstimate> getStudentEstimates() {
        return studentEstimates;
    }

    public void setStudentEstimates(List<StudentEstimate> studentEstimates) {
        this.studentEstimates = studentEstimates;
    }
}

public class StudentEstimate {
    private String studentName;
    private int dayNum;
    private int estimate;

    public StudentEstimate() {}

    public StudentEstimate(String studentName, int estimate) {
        this.studentName = studentName;
        this.estimate = estimate;
    }

    public StudentEstimate(String studentName, int dayNum, int estimate) {
        this.dayNum = dayNum;
        this.studentName = studentName;
        this.estimate = estimate;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public int getEstimate() {
        return estimate;
    }

    public void setEstimate(int estimate) {
        this.estimate = estimate;
    }

    public int getDayNum() {
        return dayNum;
    }

    public void setDayNum(int dayNum) {
        this.dayNum = dayNum;
    }
}

当我不添加这个错误行时,我没有问题。但是当我试图在@postmapping中获取我的列表时,我有一个错误。在我的控制器中,我有:

@PostMapping(URL_TEACHER + URL_LESSONS)
public ModelAndView teacherSearchLesson(
        ModelMap modelMap,
        @RequestParam(value = "les_input", required = false) String lesson,
        @RequestParam(value = "fml_input", required = false) String fml,
        @RequestParam(value = "grp_input", required = false) String grp,
        @RequestParam(value = "crs_input", required = false) String crs,
        @ModelAttribute(value = "teachersLessonsRows") TableForm form
) {
    System.out.println(form);

    TeachersLessonsRow teachersLessonsRow = null;
    List<TeachersLessonsRow> teachersLessonsRows = new ArrayList<>();

    ...

    modelMap.addAttribute("teachersLessonsRows", teachersLessonsRows);
    return new ModelAndView("lessonsTeacher", modelMap);
}

tableform是一个类,它包含此列表的list TeachersLessOnRows、getter和setter。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题