Thymeleaf读取对象、List、Map、数组中的变量值

Controller代码:

@RequestMapping("test")
public String test(ModelMap mm) {
     
    Teacher teacher = new Teacher();
    teacher.setTeacherName("老王");
    Class cls = new Class();
    cls.setClassName("历史");
    cls.setTeacher(teacher);// 赋值对象属性
    List<Teacher> teachers = new ArrayList<>();// List
    teachers.add(teacher);
    Map<String, Teacher> teacherMap = new HashMap<>();// Map
    teacherMap.put("key", teacher);
    Teacher[] teacherArray = {
     teacher};// Array
    mm.addAttribute("object", cls);
    mm.addAttribute("list", teachers);
    mm.addAttribute("map", teacherMap);
    mm.addAttribute("array", teacherArray);
    return "test";
}

页面:

<span th:text="${object.teacher.teacherName}">span><br>
<span th:text="${list[0].teacherName}">span><br>
<span th:text="${map['key'].teacherName}">span><br>
<span th:text="${array[0].teacherName}">span>

工具类:

class Teacher {
     
    private String teacherName;
	... ...
}
class Class {
     
    private String className;
    private Teacher teacher;
	... ...
}

你可能感兴趣的:(Thymeleaf)