Spring boot項目使用thymeleaf模板過程詳解
在spring boot 項目中使用thymeleaf模板,將后臺數(shù)據傳遞給前臺界面。
1、將后臺數(shù)據傳遞給前臺有很多種方式,可以將后臺要傳遞的數(shù)據轉換成json格式,去傳遞給前臺,也可以通過model形式去傳遞出去,這篇博客主要是使用thymeleaf模板,將后臺數(shù)據傳遞給前臺。
2、首先要在spring boot 項目中添加如下依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
3、這里后臺有關如何查詢數(shù)據,得到數(shù)據的具體過程就不在多說了,只是寫將數(shù)據庫中查詢到的數(shù)據取出來,放到model里面。這里就一個例子吧。
@RequestMapping('/')public String index(Model model){Person single=new Person('aa',11);List<Person> people =new ArrayList<Person>();Person p1=new Person('xx',22);Person p2=new Person('dd',33);Person p3=new Person('zz',44);people.add(p1);people.add(p2);people.add(p3);model.addAttribute('singlePerson',single);model.addAttribute('people',people);return 'index';}
4.前臺界面的寫法,
<span th:text='${person.name}'></span> <span th:text='${person.age}'></span>
通過這樣的方法就可以取到放入model中的person的name和age了。
(注:前臺界面要添加上這個代碼:<html xmlns:th='http://www.thymeleleaf.org'>)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. JSP+Servlet實現(xiàn)文件上傳到服務器功能2. CSS可以做的幾個令你嘆為觀止的實例分享3. 讀大數(shù)據量的XML文件的讀取問題4. jsp實現(xiàn)textarea中的文字保存換行空格存到數(shù)據庫的方法5. 將properties文件的配置設置為整個Web應用的全局變量實現(xiàn)方法6. 低版本IE正常運行HTML5+CSS3網站的3種解決方案7. javascript xml xsl取值及數(shù)據修改第1/2頁8. jsp文件下載功能實現(xiàn)代碼9. JSP之表單提交get和post的區(qū)別詳解及實例10. jsp+servlet實現(xiàn)猜數(shù)字游戲
