springboot--thymeleaf模板一個簡單的web項目

已經學會了怎么建立一個簡單的springboot項目,還想要建立一個簡單的html頁面,如許在Controller中直接能跳轉到這個頁面 。
于是從網上找各類資本,發現了thymeleaf這個工具,這個工具是springboot保舉利用的前臺頁面模板,利用這個模板也可以獲取Controller中的各類參數,很好用,下面就記實一下若何利用它 。
2建立一個簡單的springboot項目

需要這些哦
ecplise
thymeleaf
方式/
1起首引入thymeleaf包,將其放在pom.xml文件中 。

springboot--thymeleaf模板一個簡單的web項目



2其次要知道thymeleaf的默認設置裝備擺設,從官網上可以查詢到thymeleaf的默認設置裝備擺設,如下 。 有一個設置裝備擺設就是緩存最好封閉:spring.thymeleaf.cache=false,直接放到application.properties文件中 。

springboot--thymeleaf模板一個簡單的web項目



3踩過的坑:thymeleaf要求,html文檔,必需嚴酷的有開閉標簽,即有<tr>,必需有</tr>;因為是利用ecplise主動建立的html,<meta>標簽,并沒有</meta>標簽,導致法式后臺一向報錯,加上就好了 。

springboot--thymeleaf模板一個簡單的web項目



4編寫Controller,利用ModelAndview返回到Hello頁面(mv.setViewName("Hello")就會指標的目的templates/Hello.html文件;可以看上面的thymeleaf默認設置裝備擺設),并攜帶數據到html頁面 。

springboot--thymeleaf模板一個簡單的web項目



5編寫html頁面Hello.html 及 application.properties設置裝備擺設,如下圖

springboot--thymeleaf模板一個簡單的web項目



springboot--thymeleaf模板一個簡單的web項目



6【springboot--thymeleaf模板一個簡單的web項目】啟動項目,打開谷歌瀏覽器,拜候/sjn/hello辦事,查看結果

springboot--thymeleaf模板一個簡單的web項目




以上內容就是springboot--thymeleaf模板一個簡單的web項目的內容啦,希望對你有所幫助哦!

    猜你喜歡