SpringBoot使用thymeleaf
来源:互联网 发布:php二合一网站 编辑:程序博客网 时间:2024/06/16 14:31
thymeleaf音译:塞姆理符
1.新建SpringBoot项目,选择thymeleaf依赖,会自动导入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
2.创建POJO
package com.cvsea;public class Person { private String name; private Integer age; public Person(String P_Name,Integer P_Age) { this.name=P_Name; this.age=P_Age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
3.创建演示页面,thymeleaf模板引擎页面放在src/main/resources/templates下。
index.html
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"/><title>Insert title here</title></head><body><div> <h3>访问model</h3> <span th:text="${singlePeson.name}"></span></div><div th:if="${not #lists.isEmpty(people)}"> <h3>访问列表</h3> <ul> <li th:each="person:${people}"> <span th:text="${person.name}"></span> <span th:text="${person.age}"></span> <button th:onclick="'getName(\''+${person.name}+'\')'">获取名字</button> </li> </ul></div><script th:inline="javascript">var single=[[${singlePeson}]]console.log(single.name+"/"+single.age);function getName(name){ console.log(name);}</script></body></html>
4.注入数据
package com.cvsea;import java.util.ArrayList;import java.util.List;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@SpringBootApplicationpublic class Learning1Application { @RequestMapping("/") public String hello(Model model) { Person person=new Person("pxs",26); model.addAttribute("singlePeson",person); List<Person> people=new ArrayList<Person>(); people.add(new Person("pxs",26)); people.add(new Person("nxy",26)); people.add(new Person("lgp",26)); model.addAttribute("people",people); return "index"; } public static void main(String[] args) { SpringApplication.run(Learning1Application.class, args); }}
5.运行效果
阅读全文
0 0
- SpringBoot使用thymeleaf
- springboot(四):thymeleaf使用详解
- SpringBoot使用thymeleaf模板引擎
- springboot使用thymeleaf 解析异常
- Springboot+Thymeleaf配置与使用
- SpringBoot (四) :thymeleaf 使用详解
- springboot(四):thymeleaf使用详解
- springboot(四):thymeleaf使用详解
- springboot中使用thymeleaf模板
- springboot(四):thymeleaf使用详解
- SpringBoot -- thymeleaf
- springboot-thymeleaf
- SpringBoot-thymeleaf
- SpringBoot + thymeleaf
- springboot thymeleaf
- Springboot 中 使用thymeleaf 显示自定义Logo
- SpringBoot使用thymeleaf的简单记录
- SpringBoot--使用Thymeleaf模板开发web项目
- 算法和排序的区别
- PHP设计模式
- 交换机性能指标:背板带宽、交换容量、包转发率
- Java中常见的异常
- KafkaOffsetMonitor安装部署
- SpringBoot使用thymeleaf
- 学生系统优化总结(一)
- dede网站模板制作流程
- 冒泡排序
- centos 安装mysql
- 5.抽象类,接口
- C++ 地址和地址值
- Python基础(简明Python教程)
- 对于系统权限管理的认识与设计