Spring boot项目使用thymeleaf模板过程详解
在spring boot 项目中使用thymeleaf模板,将后台数据传递给前台界面。
1、将后台数据传递给前台有很多种方式,可以将后台要传递的数据转换成json格式,去传递给前台,也可以通过model形式去传递出去,这篇博客主要是使用thymeleaf模板,将后台数据传递给前台。
2、首先要在spring boot 项目中添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3、这里后台有关如何查询数据,得到数据的具体过程就不在多说了,只是写将数据库中查询到的数据取出来,放到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">)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
- Spring Boot thymeleaf模板引擎的使用详解
- SpringBoot使用Thymeleaf模板引擎访问静态html的过程
- springBoot加入thymeleaf模板的方式
- spring boot 项目中使用thymeleaf模板的案例分析
- SpringBoot使用thymeleaf模板过程解析
- Spring Boot集成Thymeleaf模板引擎的完整步骤
- SpringBoot中的Thymeleaf模板
- Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法
- Spring boot搭建web应用集成thymeleaf模板实现登陆
- 详解spring Boot 集成 Thymeleaf模板引擎实例
- springboot用thymeleaf模板的paginate分页完整代码
- springboot中thymeleaf模板使用详解
- Springboot Thymeleaf模板文件调用Java类静态方法
- Java基础总结之Thymeleaf详解
相关文章
详解Java设计模式编程中的Flyweight享元模式的开发结构
这篇文章主要介绍了Java设计模式编程中的Flyweight享元模式的开发结构,享元模式能够最大限度地重用现有的同类对象,需要的朋友可以参考下2016-04-04
Java如何获取resources下的文件路径和创建临时文件
这篇文章主要介绍了Java如何获取resources下的文件路径和创建临时文件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-12-12
spring boot实战教程之shiro session过期时间详解
这篇文章主要给大家介绍了关于spring boot实战教程之shiro session过期时间的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。2017-10-10


最新评论