thymeleaf模板引擎

首先SpringBoot这个项目是以jar的方式,不是war,第二点我们用的是嵌入式的tomcat,所以现在默认不支持jsp。

jsp其实就是一个模板引擎,springboot支持的就是thymeleaf引擎。

导入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>、

一.变量输出与字符串操作:

1.1   th:text
<span th :text="hello"></span>
<span th :text="${msg}"></span>
1.2  th:value:

把一个值放入到input标签的value中

<input type=“text" name ="username"th:value="${msg}"/>

三.日期格式化处理


格式化日期,默认的以浏览器默认语言为格式化标准
${#dates.format(key)}
 
按照自定义的格式做日期转换
${#dates.format(key,'yyy/MM/dd')}
 
year:取年
Month:取月
Day:取日
${#dates.year(key)}
${#dates.month(key)}
${#dates.day(key)}

四.条件判断

4.1 th:if:

<span th:if="${sex} == '男'">
性别:男
</span>
<span th:if="${sex} == '女'">
性别:女
</span>

4.2 th:switch:

<div th:switch="${id}">
    <span th:case="1">ID 为 1</span>
    <span th:case="2">ID 为 2</span>
    <span th:case="3">ID 为 3</span>
</div>

五.迭代遍历

5.1 th:each:

@RequestMapping("/show3")
public String showInfo3(Model model){
   List<Users> list = new ArrayList<>();
   list.add(new Users(1,"张三",20));
   list.add(new Users(2,"李四",22));
   list.add(new Users(3,"王五",24));
   model.addAttribute("list", list); 
   return "index3";

}
<table border="1">
     <tr>
    <th>ID</th>
     <th>Name</th>
     <th>Age</th>
     </tr>
     <tr th:each="u : ${list}">
     <td th:text="${u.userid}"></td>
     <td th:text="${u.username}"></td>
     <td th:text="${u.userage}"></td>
     </tr>
</table>

七.URL相关

绝对路径

<a th:href="@{http://www.baidu.com}">绝对路径</a><br/>

相对路径

相对于当前项目的根

<a th:href="@{/show}">相对路径</a>

相对于服务器路径的根

<a th:href="@{~/project2/resourcename}">相对于服务器的根</a

7.3 在 url 中实现参数传递:

<a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a>

7.4 在 url 中通过 restful 风格进行参数传递:

<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}"> 相 对 路 径 - 传 参-restful</a>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值