Thymeleaf

什么是Thymeleaf

? ? Thymeleaf的優點是它是基于HTML的,即使視圖沒有渲染成功,也是一個標準的HTML頁面。

前身方法

前期技術都是JSP,JSP的優點是它是Java EE容器的一部分,幾乎所有Java EE服務器都支持JSP。缺點就是它在視圖表現方面的功能很少,假如我們想迭代一個數組之類的,只能使用<%%>來包括Java語句進行。雖然有標準標簽庫(JSTL)的補足,但是使用仍然不太方便。另外JSP只能在JavaEE容器中使用,如果我們希望渲染電子郵件之類的,JSP就無能為力了。

配置引入方法

1、添加依賴

? ? ? ? <!--thymeleaf模板引擎依賴-->

? ? ? ? <dependency>

? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? <artifactId>spring-boot-starter-thymeleaf</artifactId>

? ? ? ? </dependency>

2、Controller寫法

@ControllerpublicclassIndexController{//注入一個student類對象,被spring容器托管---bean@ResourceprivateStudent student;//? ? @RequestMapping(value="/index",method = RequestMethod.GET)@GetMapping("index")publicStringindex(ModelMap map){? ? ? ? student.setAge(20);? ? ? ? student.setName("Tom");? ? ? ? student.setMale("male");? ? ? ? student.setStudentNo("2018");//將模型加入視圖map.addAttribute("student",student);return"index";//返回值就是頁面名稱}}

3、HTML寫法

頂部添加

配合WebJars

1、添加依賴

? ? ? ? ? ? ? ? ? ? org.webjars? ? ? ? ? ? bootstrap? ? ? ? ? ? 3.3.7-1? ? ? ?

2、網頁代碼

主頁

3、Controller代碼

importjavax.annotation.Resource;/**

* Created by lenovo on 2018/9/6.

*/@ControllerpublicclassIndexController{//注入一個student類對象,被spring容器托管---bean@ResourceprivateStudent student;//? ? @RequestMapping(value="/index",method = RequestMethod.GET)@GetMapping("index")publicStringindex(ModelMap map){? ? ? ? student.setAge(20);? ? ? ? student.setName("Tom");? ? ? ? student.setMale("male");? ? ? ? student.setStudentNo("2018");//將模型加入視圖map.addAttribute("student",student);return"index";//返回值就是頁面名稱}}

作者:技術小白熊

鏈接:http://www.lxweimin.com/p/a624cec53d05

來源:簡書

簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容