thymeleaf视图解析器

获取域对象中的数据

<body>
<h1>basic table</h1>
[[${session.name}]]
</body>
    @RequestMapping("/basic_table")
    public ModelAndView basic_table(HttpSession session)
    {
        ModelAndView mav=new ModelAndView();
        mav.setViewName("/table/basic_table");
        session.setAttribute("name","name123");
        return  mav;
    }

1.路径设置

<a th:href="@{/hello}">HelloWorld</a><br/>

th:href="@{/}"代表上下文路径即 http://localhost:8080/Springmvc_demo1_war_exploded/

后面加个hello 就是访问

http://localhost:8080/Springmvc_demo1_war_exploded/target/hello

如果项目配置了访问前缀,(就是所有请求,不管是静态资源还是请求,都需要添加/all才能访问http://localhost:8080/all/success)

则@{/test2} 会自动添加/all/

 2.设置标签的属性

@RequestMapping("/test3")
    public String test3(HttpServletRequest request) {
        request.setAttribute("red", "red");
        return "success";
    }
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:class="${red}"></div>
</body>
</html>
<style>
    .red{
        width: 100px;
        height: 100px;
        border: 1px solid  red;
    }
</style>

 3.迭代

<div th:each="user : ${users}">
    [[${user.uname}]]
    [[${user.pwd}]]
</div>

 

<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
  <td th:text="${prod.name}">Onions</td>
  <td th:text="${prod.price}">2.41</td>
  <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

4. 条件运算

<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>
<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p>
</div>

6.公共样式

比如我有后台管理系统,上面和左边是一样的,这些部分引用的样式表也一样,所以没必要重复写,弄个公共的,后面使用引用该部分即可。

模板

<body >
<div th:fragment="content1" id="1">
<div class="title"></div>
<div class="left"></div>
</div>
</body>

引用

1.th:replace  保留模板的标签 去掉引用的的标签 (保留id为1的标签,去掉id为2的标签)

<div  th:replace="common :: content1" id="2"> </div>

2. th:include 保留引用的标签 去掉模板的标签 (保留id为2的标签,去掉id为1的标签)

<div  th:include="common :: content1" id="2"> </div>

 3.

th:insert: 引用标签和模板标签同时存在

4.id

可以用id代替模板中原来的 th:fragment

使用模板的时候需要在id名前加#号,表示选择器

<div th:replace="common :: #script"></div>
<div id="script">
<script th:src="@{/test.js}">
</script>

5.模板页引用其他页面,比如,模板页引入css文件,引入js文件,需要给文件路径加上 @{/}

加上后并不会影响正常访问,但是表明了这个路径是交给thymeleaf解析,如果不写的话,thyme leaf在加载该模板文件时,并不会当作路径去处理,而是当作普通的字符串,所以会报错。

<link th:include="common :: css1">
    <div th:fragment="css1">
    <link rel="stylesheet" type="text/css" th:href="@{/common/css.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/common/css2.css}">
    </div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值