这里使用的是idea
1.新建Spring Boot项目
File-->New-->Project...,然后选择左边的Spring Initializr-->Next,可根据自己的需求修改,也可默认,-->Next,选择依赖项。-->Template Engines中的Thymeleaf-->Next-->finish。
2.建立实体类
package com.example.thymeleaf.entity; public class Person { private String name; private Integer age; public Person(){ super(); } public Person(String name,Integer age){ super(); this.age=age; this.name=name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.在ThymeleafApplication.java文件中,修改如下
package com.example.thymeleaf; import com.example.thymeleaf.entity.Person; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @SpringBootApplication public class ThymeleafApplication { @RequestMapping("/") public String index(Model model){ Person single = new Person("aa",11); model.addAttribute("singlePerson",single); return "index"; } public static void main(String[] args) { SpringApplication.run(ThymeleafApplication.class, args); } }
3.在resource-->templates下建立index.html文件,内容如下。
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello World!</title> </head> <body> <p th:text="${singlePerson.name}"></p> </body> </html>
Thymeleaf是一个Java类库,我的理解是类似于JSP文件也类似与Anjularjs。如果想了解详解内容请到官网查看:http://www.thymeleaf.org/
4.浏览器中输入localhost:8080显示内容如下
aa