`
嚣张把刀
  • 浏览: 9449 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Spring Boot使用thymeleaf模板

阅读更多

Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。可以完全替代JSP。

Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

那么Spring Boot怎样和thymeleaf整合呢?

首先新建maven项目,导入spring boot的依赖

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.3.8.RELEASE</version>
</parent>

 

导入thymeleaf starter pom依赖

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

 

在src/main/resources下新建static目录(存放js、css、图片等静态资源)和templates目录(存放展示模板,如html等),将bootstrap相关的js、css放入到static下

个人博客

新建Person类,作为数据载体

package com.spring.boot.web.model;

public class Person {
	
	private String name;
	
	private int age;
	
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

新建WebController类,指定入口方法,向模板填充数据

package com.spring.boot.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.spring.boot.web.model.Person;

@Controller
public class WebController {
	
	@RequestMapping("/")
	public String index(Model model){
		Person onePerson = new Person("微儿博客", 18);
		
		List<Person> list = new ArrayList<Person>();
		Person p1 = new Person("张三", 18);
		Person p2 = new Person("李四", 19);
		Person p3 = new Person("王五", 20);
		list.add(p1);
		list.add(p2);
		list.add(p3);
		
		model.addAttribute("oneperson", onePerson);//向模板传数据
		model.addAttribute("people", list);
		return "index";//找到名为index.*的模板
	}
}

 

在src/main/resources/templates下新建index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>test</title>
<link th:href="@{css/bootstrap.min.css}" rel="stylesheet"/>
</head>
<body>
	<div class="panel panel-primary">
		<div class="panel-heading">
			<h3 class="panel-title">访问model</h3>
		</div>
		<div class="panel-body">
			<span th:text="${oneperson.name}"></span>
		</div>
	</div>
	<div th:if="${not #lists.isEmpty(people)}">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h3 class="panel-title">列表</h3>
			</div>
			<div class="panel-body">
				<ul class="list-group">
					<li class="list-group-item" th:each="person:${people}">
						<span th:text="${person.name}"></span>
						<span th:text="${person.age}"></span>
						<button class="btn" th:onclick="'getName(\''+${person.name}+'\');'">获取名字</button>
					</li>
				</ul>
			</div>
		</div>	
	</div>
	<script type="text/javascript" th:src="@{js/jquery-1.12.3.min.js}"></script>
	<script type="text/javascript" th:src="@{js/bootstrap.min.js}"></script>
	<script th:inline="javascript">
		function getName(name){
			alert(name);
		}
	</script>
</body>
</html>

 

创建执行类Main

package com.spring.boot.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
	
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}

 

执行,访问localhost:8080

微儿博客

 

原文地址:微儿博客

分享到:
评论

相关推荐

    spring boot+thymeleaf项目实战

    7. **错误处理和模板布局**:学习如何使用Thymeleaf处理Spring MVC的模型错误,并创建可重用的布局模板,以提高代码复用性。 在“favorites-web-master”这个项目中,你可以期待看到一个完整的Web应用结构,包含...

    《Spring Boot 2+Thymeleaf企业应用实战》_杨恩雄.pdf

    《Spring Boot 2+Thymeleaf企业应用实战》是杨恩雄编著的一本面向Java Web开发的技术书籍,这本书籍主要介绍了Spring Boot框架及其与Thymeleaf模板引擎的结合使用。内容覆盖了从Spring Boot和Spring MVC基础,到...

    权限管理—spring boot+thymeleaf

    其次,**Thymeleaf** 是一个现代服务器端JavaScript模板引擎,常用于Spring Boot应用的视图层。它支持HTML5,使得开发者能在浏览器中直接预览页面,同时在服务器端渲染时保持相同的行为。在权限管理上下文中,...

    spring boot+thymeleaf+bootstrap 简单实现后台管理系统界面

    在文件结构上,通常会有一个`src/main/resources/templates`目录存放Thymeleaf模板文件,每个模板对应一个或多个后台接口。例如,`dashboard.html`可能对应着后台的主界面,`users.html`可能是用户管理页面,它们会...

    Spring Boot + thymeleaf 实现文件上传下载功能

    本文详细介绍了如何使用 Spring Boot 和 Thymeleaf 实现文件上传下载功能,包括项目初始化、依赖项配置、控制器编写、模板渲染和文件上传下载逻辑等内容。 知识点: * Spring Boot 项目初始化 * Thymeleaf 模板...

    Spring Boot集成Thymeleaf模板引擎的完整步骤

    本文将为大家介绍Spring Boot集成Thymeleaf模板引擎的完整步骤,从基本概念到实际配置,详细地介绍了Thymeleaf模板引擎的使用方法。 一、Thymeleaf模板引擎概述 Thymeleaf是一种模板引擎框架,支持HTML5标准,可以...

    spring boot使用thymeleaf模板的方法详解

    Spring Boot使用Thymeleaf模板引擎的详细指南 Thymeleaf是一个强大的、现代的模板引擎,被广泛用于Spring Boot项目中,特别是在Spring 4.0及更高版本中。它以其独特的特性和易于使用性而受到开发者的青睐。在本文中...

    spring boot+bootstrap+thymeleaf

    在"bootdemo"这个项目中,很可能是包含了一个使用Spring Boot、Bootstrap和Thymeleaf开发的基础模板。这个模板可能包含了基本的路由配置、数据访问对象(DAO)、服务层(Service)以及控制器(Controller)的实现,...

    理财管理-使用spring boot+thymeleaf

    开发者可以利用Spring Boot的RESTful API接口,通过HTTP请求从客户端获取或提交数据,Thymeleaf模板根据接收到的数据动态生成页面。通过这样的组合,可以构建出高效、可维护的理财管理应用。 至于压缩包中的文件...

    spring boot+thymeleaf+bootstrap 简单实现后台管理系统界面(看评论酌情下载)

    在后端,我们使用Spring Boot 的控制器(Controller)来处理HTTP请求,通过`@GetMapping`和`@PostMapping`等注解定义路由,返回Thymeleaf 模板或者模型数据。 例如,创建一个简单的登录页面,可以在`login.html`中...

    spring boot+thymeleaf+mybatis+mysql

    然后,创建一个`application.properties`或`application.yml`文件,配置Spring Boot的属性,包括服务器端口、Thymeleaf模板路径、MyBatis的配置文件路径、数据库连接信息等。 2. 配置Thymeleaf:Thymeleaf模板文件...

    spring boot 连接thymeleaf的很简单的Demo

    接下来,创建一个简单的Thymeleaf模板。在`src/main/resources/templates`目录下,创建一个名为`index.html`的文件,内容可能如下: ```html &lt;!DOCTYPE html&gt; &lt;html xmlns:th="http://www.thymeleaf.org"&gt; ...

    毕业设计基于spring boot+thymeleaf理财管理系统设计与实现【源码+lw+部署+讲解】

    内容概要:这个毕业设计项目基于Spring Boot框架和Thymeleaf模板引擎,旨在设计和实现一个理财管理系统。该系统允许用户注册账号、记录个人收支、制定预算计划等功能,帮助用户更好地管理个人财务。源码包括了后端的...

    毕设绝技-理财管理(spring boot+thymeleaf)

    毕设绝技-理财管理(Spring Boot+Thymeleaf)介绍 理财管理系统基于Spring Boot和Thymeleaf技术栈进行设计和实现,旨在为用户提供一个便捷、安全、高效的理财平台,帮助用户更好地管理自己的资金和投资。 Spring ...

    Spring MVC / Spring Boot中使用Thymeleaf模板引擎

    例如,一个简单的Thymeleaf模板可能如下所示: ```html &lt;!DOCTYPE html&gt; &lt;html xmlns:th="http://www.thymeleaf.org"&gt; , Thymeleaf!"&gt;Default Title ${greeting}"&gt;Hello, World! ${message}"&gt;Default Message...

    Spring boot + thymeleaf 后端直接给onclick函数赋值的实现代码

    "Spring Boot + Thymeleaf 实现后端直接给 onclick 函数...同时,本篇文章还介绍了 Thymeleaf 模板引擎、Spring Boot 中的 Thymeleaf 配置、使用 Thymeleaf 生成动态 HTML 内容、onclick 事件处理和 JS 函数等知识点。

    spring-boot html thymeleaf

    Spring Boot 结合 Thymeleaf 的动态模板功能,可以实现以下功能: 1. **模板引擎集成**:Spring Boot 默认已经集成了 Thymeleaf,只需在配置文件中声明视图解析器即可。 2. **视图解析**:视图解析器会根据逻辑...

    spring boot +spring security+thymeleaf实现权限

    在本文中,我们将深入探讨如何使用Spring Boot、Spring Security和Thymeleaf这三个强大的Java技术栈组件来实现一个全面的权限管理系统,同时涵盖Remember-Me功能。Spring Boot简化了Spring应用的开发,Spring ...

    11、理财管理(spring boot+thymeleaf).zip

    随着科技的发展,越来越多的理财管理系统开始采用先进的技术框架,如Spring Boot和Thymeleaf,以提供更加便捷、高效和安全的服务。 Spring Boot是一个由Pivotal团队提供的全新框架,旨在简化新Spring应用的初始搭建...

    spring-boot idea Thymeleaf模板引擎 增删改查

    在这个主题中,我们将深入探讨如何使用Spring Boot与Thymeleaf模板引擎进行Web开发中的增删改查操作。 Thymeleaf是一个现代的服务器端HTML模板引擎,它支持HTML5, XML, SVG 和 JavaScript。它的一大特点是在静态...

Global site tag (gtag.js) - Google Analytics