0
点赞
收藏
分享

微信扫一扫

thymeleaf模板1

大柚子top 2022-04-04 阅读 46

0.html加限制防报错 xmlns:th="http://www.thymeleaf.org"

  1. 标准表达式(在html标签中,也可以在标签里面)(取对象的值)
    <div th:text="${xxx}">${xxx.xxx}</div>

2.作用域也可以添加对象进去
model.addAttribute("user",new User(111,"xxx"))

3.可以update实时更新资源

4.选择变量表达式 *号表达式(是对对象的简化) 与th:object配合使用,把对象拆分,依附关系

 <div th:object="${user}">
      <p th:text="*{id}"></p>
 </div>
  //也可以
   <p th:text="*{user.id}"></p>

5.链接表达式@{url} (以后都是用restful风格)

  script src="xxx" link href="xxx" 有链接的标签
   //绝对路径,加上th代表被接管
  <a th:href="@{www.baidu.com}">百度</a>
  //相对路径,代表当前页面的路径
  //完整路径就是localhost:8080/myboot/aaa
  <a th:href="@{/aaa}">百度</a>
   //传参,''代表拼接字符串,id是作用域的值
  <a th:href="@{'/aaa?id='+${id}}">model的数据</a>

  //推荐传参的方式,(代表传入的参数),不用''号了,直接写路径,相当于xxx?=xxx
  <a th:href="@{/aaa(name='lis',age=20)">model的数据</a>

6.th:each循环map list
1.先在controller

List<User> users =new ArrayList();
 users.add(new User(1,"zs"));
 model.addAttribute("myusers",users);

2.在html中

<div th:each="user,userStat:${myusers}">   
  //循环的变量,循环的当前对象(其实与循环的变量一样):数据来源
<p th:text="${user.id}"></p>
<p th:text="${user.name}"></p>
 </div>

3.可以省略循环变量,不定义也有名字为 循环遍历的xxxStat

 <div th:each="user,userStat:${myusers}">   //item,循环的索引:数据来源
      //size是循环变量的总数,count是第几个
               <p th:text="${userStat.count}+'/'+${userStat.size}"></p>
               //和user一样
             <p th:text="${userStat.current.name}"></p>
             //是否是第一个值,是boolean类型,也有last是否是最后一个
             <p th:text="${userStat.first}"></p>
            <p th:text="${user.id}"></p>
           <p th:text="${user.name}"></p>
 </div>

7.循环数组array
1.controller

User userarray[]=new User[3];
  userarray[0] =new User(1,"马超");
model.addAttribute("userarray",userarray);

2.html(与list一样)

      <div th:each="user:${userarray}">
<p th:text="${user.id}"></p>
       </div>

8.循环map(语法特殊),key和value取值不一样
1.controller

Map<String,User> map=new HashMap<>();
       map.put("user1",new User(1,"ls"));
       model.addxxx("map",map);
  <div th:each="map:${map}">
	<p th:text="${map.key}"></p>
	<p th:text="${map.value}"></p>  
    <p th:text="${map.value.id}"></p>   //value是对象可取值
   </div>

9.循环 list (要用循环嵌套)(hashmap不一定有序)
–1.controller

 List<Map<String,User>> listmap=new ArrayList<>();
  listmap.put("xxx",new User(1,"xxx"))

–2.html

<div th:each="lm:${listmap}">
<div th:each="m:${lm}">
                         <p th:text="${m.key}"></p>
            </div>
  </div>

10.判断if (unless是相反的if)(必须在$ {}判断)

  <div th:if="${age=='0'}">显示文本</div>
    <div th:if="${name}">显示文本</div>   //空字符串''是真,null是假
  <div th:unless="10>0">显示文本</div>  
举报

相关推荐

0 条评论