javascript
SpringBoot实战(五)之Thymeleaf
Thymeleaf同jsp、volocity、freemarker等共同的職能是MVC模式中的視圖展示層,即View。
當然了,SpringBoot中也可以用jsp,不過不推薦這種用法,比較推崇的就是使用Thymeleaf。
關于Thymeleaf學習,建議參考官方文檔:https://www.thymeleaf.org/documentation.html
官方文檔例子,應有盡有。
之前接觸過Thymeleaf是因為公司項目初建期間用過它搭建過測試環境,后來根據領導的指示,需要快速開發,而且當時對于Thymeleaf不是十分了解,當時對于它的了解認識只是展示前端數據的,同jsp職能一樣,當然這也是它們的共性。比較詳細的深入了解和使用,是幫助一位學妹解決畢業論文的項目問題。那時幫助她寫了好十幾個類代碼和對應的十幾個html代碼。當時感觸比較深的是,Thymeleaf是如此的好用,比jsp還好用,jsp要遍歷之類,要么加<%%>,或者引用jstl標簽庫進行數據遍歷等。而Thymeleat就不需要。簡潔直觀,我比較推崇Thymeleaf,特別是在前后端分離的項目,有的時候不用jsp,光純html+js的確有點吃力,即便使用了vue.js或者angular.js、react.js等雖然增加了便利性,但是了,如果一個項目前端語言不是十分統一的話,對于將來維護成本方面會大大提高,不易維護。
Thymeleaf對于前后端分離,我認為還是不錯的。
?
下面進入demo例子講解:
?
一、導入maven依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.springframework</groupId><artifactId>gs-uploading-files</artifactId><version>0.1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.RELEASE</version></parent><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>?
二、編寫實體
package hello;public class User {private Integer id;private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}?
三、編寫Controller
package hello;import java.util.ArrayList; import java.util.List;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;@Controller public class TestController {@GetMapping(value="/test")public String test(Model model) {List<User> userList = new ArrayList<User>();for (int i = 0; i <10; i++) {User user =new User();user.setId(1);user.setName("張三");userList.add(user);}model.addAttribute("users", userList);return "test";} }?
四、編寫配置文件,將其放置在/src/main/resources下
application.properties
server.port=8080 spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML5?
五、在src/main/resources目錄下新建templates目錄并新建html文件
test.htm
<html xmlns:th="http://www.thymeleaf.org"> <body><h2>用戶列表</h2><div><ul><li th:each="user:${users}"><span th:text="${user.id}"></span>-<span th:text="${user.name}"></span>-</li></ul></div> </body> </html>?
六、編寫啟動類
package hello;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}?
最后結果如下圖所示:
這里沒有加入mysql或者其他數據相關的,如果要引用數據庫,引用對應數據庫的依賴和配置對應的數據庫連接池即可。
這里可以參考我的關于Springboot+MyBatis+Thymeleaf
示例地址為:https://github.com/youcong1996/springboot-mybatis-demo
轉載于:https://www.cnblogs.com/youcong/p/9385918.html
總結
以上是生活随笔為你收集整理的SpringBoot实战(五)之Thymeleaf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DRF的序列化组件
- 下一篇: 皮小秀乌迪尔符文天赋(上单乌迪尔天赋符文