Thymeleaf怎么在SpringBoot中使用(springboot,thymeleaf,编程语言)

时间:2024-05-10 09:23:11 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

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.结果:

Thymeleaf怎么在SpringBoot中使用

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Thymeleaf怎么在SpringBoot中使用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:iOS中APP签名机制中原理是什么下一篇:

27 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18