SpringBoot教程(3):整合Thymeleaf(中)

一、Thymeleaf 有哪些优点

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

  2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

  3.Thymeleaf提供spring标准方言和一个与SpringMVC完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能

二、Thymeleaf 静态页面

美工可以直接运行thymeleaf的html文件,可以本地浏览器打开,也可以在IDEA中直接打开。

index.html 文件代码:

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p th:text="${message}">静态message</p>
<p th:text="${display}">静态display</p>
</body>
</html>

我本地有Chrome 浏览器,所以美工可以直接点击Chrome浏览器的快捷图标进行查看静态index.html的运行效果,

这就是美工在浏览器中查看页面的静态效果,此时index.html就可以交给后台开发人员动态绑定值。

三、Thymeleaf 动态页面

修改IndexController代码:

package com.demo.web.controller;

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

@Controller
public class IndexController {

    //@RequestMapping("/index") //单个路由匹配
    @RequestMapping(value = {"","/","/index"}) //多个路由匹配
    public String Index(ModelMap mmap) {
        mmap.addAttribute("message","Hello world");
        return "index";
    }
}

在Index方法中,只向页面传递message,其值为:Hello world。返回字符串“index”,是为了让Thymeleaf的试图解析器寻找到index.html文件。

此时再启动Spring Boot项目,使用浏览器查看效果:

因为没有传递display,页面就没有显示第二个标签,此时查看以下页面源码:

第二个p标签还在,只是没有值。

下一章将介绍Thymeleaf的内置对象。

原文地址:https://www.cnblogs.com/yansg/p/12268922.html

时间: 2024-08-01 19:50:50

SpringBoot教程(3):整合Thymeleaf(中)的相关文章

SpringBoot: 9.整合thymeleaf(转)

1.创建maven项目,添加项目所需依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <de

Spring Boot2 系列教程 (十二) | 整合 thymeleaf

前言 如题,今天介绍 Thymeleaf ,并整合 Thymeleaf 开发一个简陋版的学生信息管理系统. SpringBoot 提供了大量模板引擎,包含 Freemarker.Groovy.Thymeleaf.Velocity 以及 Mustache,SpringBoot 中推荐使用 Thymeleaf 作为模板引擎,因为 Thymeleaf 提供了完美的 SpringMVC 支持.Thymeleaf 是新一代 Java 模板引擎,在 Spring 4 后推荐使用. 什么是模板引擎? Thym

Spring Boot入门系列六( SpringBoot 整合thymeleaf)

SpringBoot 整合thymeleaf 一.什么是Thymeleaf模板 Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎.与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用.它的功能特性如下: @Controller中的方法可以直接返回模板名称,接下来Thyme

三、SpringBoot整合Thymeleaf

三.SpringBoot整合Thymeleaf As well as REST web services, you can also use Spring MVC to serve dynamic HTML content. Spring MVC supports a variety of templating technologies, including Thymeleaf, FreeMarker, and JSPs. Also, many other templating engines

SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: Thymeleaf FreeMarker Velocity Groovy Mustache Spring Boot 推荐使用这些模板引擎来代替 Jsp,Thymeleaf 只是其中一种,下面我们来简单聊聊Thymeleaf及实践一下如何整合Spring Boot和Thymeleaf. 1.Thyme

SpringBoot整合Thymeleaf(三)

? Thymeleaf是SpringBoot官方推荐的用来渲染页面的一个模板,如果你对模板和模板引擎没什么概念的话,可以简单理解为Thymeleaf是一个高级简洁的JSP.如果学过MVC设计模式,那么Thymeleaf就是视图层(view)的主要核心内容. 为什么要整合Thymeleaf SpringBoot在内置Tomcat服务器,并且以Jar包方式运行,传统JSP页面不在适合这种模式开发 使用Thymeleaf有助于前后端协作,因为它在无网络环境下也可以运行,前端开发者便于在静态页面查看页面

springboot整合Thymeleaf模板引擎

引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>nekohtml</groupId&g

springBoot和MyBatis整合中出现SpringBoot无法启动时处理方式

在springBoot和Myatis   整合中出现springBoot无法启动   并且报以下错误 Description: Field userMapper in cn.lijun.controller.UserController required a bean of type 'cn.lijun.mapper.UserMapper' that could not be found. Action: Consider defining a bean of type 'cn.lijun.ma

SpringSecurity解决跨域问题,在SpringBoot整合SprinSecurity中如何用前后端分离Ajax登录,Ajax登录返回状态200还是近error

先说说SpringSecurity如何实现前后端分离Ajax登录? 今天使用SpringBoot整合SpringSecurity中想使用Ajax替代SpringSecurit的Form表单提交,在这里我们的提交方式还是使用表单提交 http.formLogin().loginProcessingUrl("/authentication/form") loginProcessingUrl方法表示你登录请求的地址,在这里SpringSecurity默认登录页面地址是/login ,填写了u

Springboot集成Thymeleaf中遇到的问题------不能返回页面,只返回字符串

springboot集成thymeleaf中遇到的问题: 不能返回页面,只返回字符串 原因:在controller中使用了注解@RestController 或者注解@ResponseBody 解决方法:用 @Controller 代替 @RestController: 不使用@ResponseBody注解. 原文地址:https://www.cnblogs.com/BenNiaoXianFei/p/12672384.html