Thymeleaf怎么在SpringBoot中使用
导读:本文共1544字符,通常情况下阅读需要5分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 1.引入依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependenc... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。1.引入依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
2.application.properties
#是否开启缓存,开发时可设置为false,默认为truespring.thymeleaf.cache=true#是否检查模板是否存在,默认为truespring.thymeleaf.check-template=true#是否检查模板位置是否存在,默认为truespring.thymeleaf.check-template-location=true#模板文件编码spring.thymeleaf.encoding=UTF-8#模板文件位置spring.thymeleaf.prefix=classpath:/templates/#Content-Type配置spring.thymeleaf.servlet.content-type=text/html#模板文件后缀spring.thymeleaf.suffix=.html
3.创建实体类和controller类
publicclassBook{privateIntegerid;privateStringname;privateStringauthor;//省略getter/setter}@ControllerpublicclassBookController{@GetMapping("/books")publicModelAndViewbooks(){List<Book>books=newArrayList<>();Bookb1=newBook();b1.setId(1);b1.setAuthor("罗贯中");b1.setName("三国演义");Bookb2=newBook();b2.setId(2);b2.setAuthor("曹雪芹");b2.setName("红楼梦");books.add(b1);books.add(b2);ModelAndViewmv=newModelAndView();mv.addObject("books",books);mv.setViewName("books");returnmv;}}
4.html文件:
<!DOCTYPEhtml><htmllang="en"xmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>图书列表</title></head><body><tableborder="1"><tr><td>图书编号</td><td>图书名称</td><td>图书作者</td></tr><trth:each="book:${books}"><tdth:text="${book.id}"></td><tdth:text="${book.name}"></td><tdth:text="${book.author}"></td></tr></table></body></html>
5.结果:
springboot是什么
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
Thymeleaf怎么在SpringBoot中使用的详细内容,希望对您有所帮助,信息来源于网络。