因为spring boot取消了对velocity模板语言的支持,所以只能换成其余语言了,如今我选择Thymeleaf,毕竟官方推荐。html
一上手Thymeleaf很是不习惯,它跟经常使用的Java语法格式相差甚远。甚至跟HTML语言很是类似。spring
下面是Java项目经常使用的Thymeleaf语法,我一一作了实验:express
在进行一些简单的操做,例如,分支,循环等。Thymeleaf会做为一种标签的属性,在属性中取值并显示。这就是它的风格。app
例如:less
Controller包中的文件:学习
1 @Controller 2 public class deal { 3 @RequestMapping(path = {"/hello"}) 4 public String Hello_World(Model model) { 5 model.addAttribute("aaa", "naive"); 6 model.addAttribute("l", "<br/>"); 7 return "Hello"; 8 }
Templates包中的模板:this
1 <html> 2 <head> 3 <title>Welcome!</title> 4 </head> 5 <body> 6 <!--/* 7 fdsfd 8 <h1>Welcome</h1> 9 */--> 10 <p th:text=${aaa}></p> 11 <p>th:utext=${aaa}</p> 12 <p th:utext=${aaa}>th:utext=${aaa}</p> 13 // 14 <p th:text=${aaa}></p> 15 <p th:utext=${aaa}></p> 16 <p th:utext=${l}></p> 17 </body> 18 </html>
显示结果:spa
总结:只有把一些标签属性,例如th:text,th:utext等放到像<p>这样的标签内,网页才能正常显示Controller文件定义好的变量值。所以,要学好Thymeleaf,须要把标签学好,须要把HTML学好。。。3d
建议用<!--/* *-->这个,能够注释多行。code
1 <!--/* 2 fdsfd 3 <h1>Welcome</h1> 4 */-->
同时取消注释也简单。
1 <!--/*/ 2 fdsfd 3 <h1>Welcome</h1> 4 /*/-->
th:text或者th:utext
1 <p th:text=${aaa}></p> 2 <p>th:utext=${aaa}</p> 3 <p th:utext=${aaa}>th:utext=${aaa}</p>
显示结果:
总结:th:text均可以显示文本,把属性放到<p></p>标签里面,能够显示C层变量(第一行)。放到<p></p>中间夹住,不能取值(第二行)。同时标签中的值会取消夹住地方的值。(第三行)
th:text不能解析标签,可是th:utext能。以下:
1 model.addAttribute("l", "<br/>");
使用l变量:
1 <p th:text=&{l}></p>
出错:There was an unexpected error (type=Internal Server Error, status=500).Could not parse as expression: "&{l}" (template: "Hello" - line 13, col 6)。它表示th:text没法解析l变量中的标签。
换一种写法:
1 <!--/*--> 2 <p th:text=&{l}></p> 3 <!--*/--> 4 <p th:utext=${l}></p>
5 <p>th:utext=${aaa}</p>
显示结果:
注:4.6节后面,全是干货。
若if成立,则显示<div></div>中间的文本。不然不显示。th:unless是不成立显示。
1 <div th:if="${aaa} == false"> 2 new block 3 </div>
因为aaa不为false,所以new block不会显示出来。
若是将false改成'naive'?
1 <div th:if="${aaa} == 'naive'"> 2 new block 3 </div>
结果显示new block文字。
<p th:text="${aaa}" th:if="${not #strings.isEmpty(aaa)}"></p>
若if为true,显示text部分,不然不显示。
多分支跟其余语言同样。使用:?三目运算符。(condition)?(then):(else)
<p th:text="${aaa}? 'even' : 'odd'"></p>
当aaa为真,""里面是even,不然""里面是odd。
th:each逐个取出liststring中的值,放到n中,最后再一次显示每一个n。代码以下:
Controller包:
1 List<String> ls = Arrays.asList(new String[] {"1","111", "2323a"}); 2 model.addAttribute("liststring", ls);
向Templates包传递ls变量。
Templates包:
1 <div> 2 <p th:text="${n}" th:each="n : ${liststring}"> </p> 3 </div>
结果显示:
单分支中有行代码:
<p th:text="${aaa}" th:if="${not #strings.isEmpty(aaa)}"></p>
strings.isEmpty()方法是Thymeleaf中本身实现的方法。
有时候在模板中须要进行一些复杂的操做,手写不太现实,Thymeleaf早就给咱们想好了。文档第19章讲的就是Thymeleaf的一些类,能够使用它们调用须要的方法。
在新建的包model中建一个类:
1 package cn.scu.toutiao.model; 2 3 public class User { 4 5 public User(String name) { 6 this.name = name; 7 } 8 9 public String name; 10 public String setName(String name) { 11 this.name = name; 12 return this.name; 13 } 14 }
在controller包中传入这个类的对象:
model.addAttribute("user", new User("aw12"));
在Templates包中显示:
1 <p th:text="${user.name}"> </p> 2 <p th:text="${user.setName('fire on fire')}"> </p>
显示结果:
总结:参数不能用双引号,要用单引号。方法前不加#。
类在文档中,记住在方法前加上#。
有时候,每一个页面的头尾都是同样的。这时候建立另外一个文件,存放头部或者尾部,再引入便可。
例如:在Templates包中新建一个页面footer.html。
1 <div th:fragment="copy"> 2 i am strong man manman hahahah~~~ 3 </div>
在hello.html中引入:
<div th:insert="footer :: copy"> </div>
就能够将footer.html文件做为hello.html文件的一部分显示了。
显示结果:
技术总结:
1.在被引入的文件中用th:fragment标明能够被应用的那一部分的名字。
2.在引入的文件用th:insert表示引入哪一个文件的哪一部分。"文件名 :: 部分名"。
其它的内容与上同理,如今已经能够在Thymeleaf中放飞自我了。同时,继续学习HTML吧。
o(╯□╰)o ╯▂╰ ╯0╰ ╯︿╰ ╯ω╰ ╯﹏╰ ╯△╰ ╯▽╰ +▂+ +0+ +︿+ +ω+ +﹏+ +△+ +▽+ ˋ▂ˊ
Thymeleaf经常使用语法传送门