Spring Boot Web开发中Thymeleaf模板引擎的使用
2024-09-01 00:11:34
这里使用的是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
最新文章
- java.net.UnknownHostException: promote.cache-dns.local: unknown error
- Python基础篇【第2篇】: Python内置函数(一)
- 关于oracle 11g 64位与 32位的 plsql、及其他32位应用程序共存的问题
- [转]SQL:JOIN用法
- andirod
- php加载memcache
- php文件上传之单文件上传
- (转载)JavaScript中定义变量
- 关于下拉刷新你是否真的非常理解还是只会搬砖?附 Android 实例子源代码文件下载地址380个合集
- 全栈project师?给把瑞士军刀你去砍鬼子好不好!?
- HDU 2069 Coin Change
- Linux下的下载工具介绍----aria2
- mongodb 性能提高之利用索引, 待续
- ural1090 In the Army Now
- Java变量和对象的作用域
- hibulder中使用git教程
- asp.net core 系列 15 中间件
- checkout 多选 全选(亲测有效)
- Ngnix配置
- mysql5.7.23手动配置安装windows版