Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templates    静态文件css, js 等文件默认路径是src/main/resources/static,所有项目中如果没有这个目录需要手动加上了

首先我们要在pom.xml文件中添加依赖

<!-- thymeleaf 模板引用  -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

  

引用之后我们就来测试一下, 在pom.xml中引入依赖之后。你完全可以不用配置(也秉承了springboot 约定优于配置)当然你如果需要自定义一些属性,你可以在application.properties 中添加配置。

测试类@Controller

/**
* @author pillarzhang
* @date 2019-06-03
*/
@Controller
public class loginController {
@RequestMapping("/index")
public String index(){
return "index";
}
}

  

Index,html 页面如下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<p style="color:red">hello world</p>
</body>
</html>

  

启动项目,输入http://localhost:8081/index 即可看到如下页面

这就成功的集成了Thymeleaf。

注意:前面也说了,如果你不配置任何属性依然可以使用,当然你也可以自己设置,在配置文件中application.properties 设置相应的属性

spring.thymeleaf.prefix=classpath:/templates/  设置thymeleaf路径默认为src/main/resources/templates
spring.thymeleaf.suffix=.html 设置thymeleaf模板后缀
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false 是否开启缓存默认为true
spring.thymeleaf.mode=LEGACYHTML5 设置thymeleaf严格校验
spring.thymeleaf.encoding=UTF-8 设置编码

  

  1. 配置完成之后一定要注意路径地址是否正确,
  2. 一定要用@Controller,如果使用@RestController,有可能返回return中的一串字符
  3. 方法前不要加@ResponseBody,加这个注释相当于@RestController, 返回一串字符串同上
  4. 如果载application.properties重配置属性,一定要注意是否书写有误,不能多空格否则有可能会报如下错误:

  至此,springboot集成thymeleaf 就完成了,虽然中间遇到了一些小问题,还好解决了。

  如有不当和错误之处,请指出,我们一起交流学习,共同进步!谢谢!

Springboot 集成 Thymeleaf 及常见错误的更多相关文章

  1. 九、SpringBoot集成Thymeleaf模板引擎

    Thymeleaf咋读!??? 呵呵,是不是一脸懵逼...哥用我的大学四级英文知识告诉你吧:[θaimlif]. 啥玩意?不会音标?...那你就这样叫它吧:“赛母李府”,大部分中国人是听不出破绽的.. ...

  2. SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法

    楼主今天在学习SpringBoot集成thymelaf的时候报了中文乱码的错误,经过网上的搜索,现在得到解决的办法,分享给大家: package com.imooc.config; import or ...

  3. Springboot集成Thymeleaf

    Thymeleaf 官方解释: Thymeleaf是一个用于web和独立环境的现代服务器端Java模板引擎. Thymeleaf的主要目的是将优雅的自然模板引入到您的开发工作流中——以使HTML可以在 ...

  4. SpringBoot集成Thymeleaf模板引擎

    简单介绍 目前在JavaEE领域有几中比较常用的模板引擎,分别是Jsp.Velocity.Freemarker.Thymeleaf,对Freemark语法不是特别熟悉,不过对于前端页面渲染效率来说,j ...

  5. SpringBoot集成Thymeleaf发送Html邮件报错

    由于业务需求需要使用Thymeleaf作为模板发送Html邮件,开发调试过程中发生以下错误 org.thymeleaf.exceptions.TemplateInputException: Error ...

  6. springboot系列十二、springboot集成RestTemplate及常见用法

    一.背景介绍 在微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.N ...

  7. spring-boot集成thymeleaf。

    thymeleaf是前台页面展示,原来一直是jsp,jsp中包含很多服务器端的逻辑,逐渐淘汰.同样功能的还有freemarker.孰好孰坏不予评价,只做简单实现. 1.基本思路 (1)pom.xml中 ...

  8. springboot集成thymeleaf中遇到的问题

    错误:不能返回页面,只返回字符串. 原因:在controller中使用了注解@RestController 修改:修改注解为@Controller @Controller 分析: RestContro ...

  9. springboot集成thymeleaf中遇到不能反悔页面,只能反悔字符串

    错误:::::不能返回页面,只能返回字符串 原因::::在controller中使用了注解@RestController 修改注解为:@Controller 分析: RestController = ...

随机推荐

  1. Confluence 导出为 PDF 格式 - 导出多个页面或者整个空间

    使用 Confluence 的空间导出功能,你可以将多个页面或者整个 Confluence 站点转换为 PDF 文件. 希望使用空间导出功能,你需要 导出空间(Export Space)权限.请查看 ...

  2. (转)git常见错误

      error: src refspec master does not match any. 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 error: insufficient pe ...

  3. nRF52-PCA10040——Overview

    Overview Zephyr applications use the nrf52_pca10040 board configuration to run on the nRF52 Developm ...

  4. try_except__异常处理

    try...except.raise 一.try...except 有时候我们写程序的时候,会出现一些错误或异常,导致程序终止.例如,做除法时,除数为0,会引起一个ZeroDivisionError ...

  5. 《鸟哥的Linux私房菜》学习笔记(2)——Bash特性

    一.shell的基本概念:                               shell 意思是外壳,它是离用户最近的程序.shell提供用户操作系统的接口,我们通过shell将输入的命令与 ...

  6. ssl 在nginx上的部署示例

    server { listen 80; listen 443 ssl; server_name           [DOMAIN]; ssl on; ssl_certificate /work/ss ...

  7. Flask-用户角色及权限

    app/models.py class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=T ...

  8. luogu2756 飞行员配对方案问题

    匈牙利 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  9. 【bzoj1585】[Usaco2009 Mar]Earthquake Damage 2 地震伤害 网络流最小割

    题目描述 Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着两个牧场Ai和Bi,注意可能有很多条道路连接着相同的Ai和Bi,并且Ai有可能和Bi相等.Farmer J ...

  10. pytorch下可采用visidom作为可视化工具

    2018/9/18更新  感觉tensorboardX插件更好用,已转用https://github.com/lanpa/tensorboardX 更新:新版visdom0.1.7安装方式为:cond ...