当前位置:网站首页>Thymeleaf th:with use of local variables

Thymeleaf th:with use of local variables

2022-07-05 14:17:00 fengyehongWorld

Background data preparation

ZTestEntity zTestEntity = new ZTestEntity();
List<Person> personList = Arrays.asList(
	new Person("1", " Jia Feitian ", "20")
	, new Person("2", " Zhang San ", "21")
);
zTestEntity.setPersonList(personList);

return zTestEntity;

// ---------------------------------------------------

@GetMapping("/init2")
public ModelAndView init2() {
    

    ModelAndView modelAndView = new ModelAndView();
    ZTestEntity entity = service.init();
    modelAndView.addObject("entity", entity);
    modelAndView.setViewName("test2");
    return  modelAndView;
}

The front desk
test2.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>test2 Title of the page </title>
</head>
<body>
    <div id="container" th:object="${entity}">
        <!--  Use th:with take person0Id and personInfo Is treated as a local variable ,  Act on div On any child element within  -->
        <div th:with=" person0Id=*{personList[0].personId} ,personInfo=*{personList[1]} ">
            <!--  Inside div External use div Defined local variables  -->
            <div th:text="${person0Id}"></div>
            <div>[[${personInfo.personId}]]</div>
            <hr>

            <!-- Thymeleaf Another way to write labels , Take advantage of html5 Of  data-  attribute  th: Tag name   All can be used  data-th- This form of tag name is used to replace  -->
            <div data-th-text="${personInfo.personName}"></div>
        </div>
    </div>
</body>
</html>

effect
 Insert picture description here

原网站

版权声明
本文为[fengyehongWorld]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051410196450.html