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">)
上一篇:详解java中保持compareTo和equals同步
栏 目:JAVA代码
本文标题:Spring boot项目使用thymeleaf模板过程详解
本文地址:http://www.codeinn.net/misctech/96511.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




