springboot中thymeleaf模板应用

一:Thymeleaf介绍

Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用。

Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。Thymeleaf的可扩展性也非常棒。你可以使用它定义自己的模板属性集合,这样就可以计算自定义表达式并使用自定义逻辑。这意味着Thymeleaf还可以作为模板引擎框架。

二.:Thymeleaf在springboot中使用

1.打开eclipse的插件安装,Help—>Installations new SoftWare—>add  插件地址为: http://www.thymeleaf.org/eclipse-plugin-update-site/   一路next,最后重启Eclipse即可。

注意:要等eclipse真正安装完才会提示重启,不要中途关掉,否则安装失败

2.引入jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

3.编写实例代码

   注意:springboot自动创建了templates文件夹,用来存放静态页面

  

	
	@GetMapping("/toGet")
	public String toGet(Model model) {
		Student student=service.showAll();
		model.addAttribute("student", student);
        return "/allStudent";
	}
	
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">学生表</h1>
<div th:text=${student.age}></div>
</body>
</html>

三.Thymeleaf基本语法

猜你喜欢

转载自blog.csdn.net/yiye2017zhangmu/article/details/82562066