Springboot 集成 Thymeleaf

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

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

1

2

3

4

<!-- thymeleaf 模板引用  -->

<dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-thymeleaf</artifactId>  </dependency>

  

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

测试类@Controller

1

2

3

4

5

6

7

8

9

10

11

/**

 * @author pillarzhang

 * @date 2019-06-03

 */

@Controller

public class loginController {

    @RequestMapping("/index")

    public String index(){

        return "index";

    }

}

  

Index,html 页面如下

1

2

3

4

5

6

7

8

9

10

<!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 设置相应的属性

1

2

3

4

5

6

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, 返回一串字符串同上

一般来说springboot默认静态资源就是resources-》static目录下,但是总有各种各样的原因,所以还是直接配置

一下比较好。

一般来说被thymeleaf修饰的页面一般是thymeleaf默认在resources-》templates目录下作为前缀,表示被thymeleaf修饰的页面在这里作为根目录。(注意:不是静态资源)

但是还是希望大家能加上对应的配置,避免各种各样的尴尬。

application.yml我的配置如下:

spring:
    mvc:
          static-path-pattern: /**
    resources:
          static-locations: classpath:/static/
    thymeleaf:
          prefix: classpath:/templates/
          suffix: .html
          cache: false #关闭缓存

 

如果你的是application.properties那么对应的就是

spring.mvc.static-path-pattern=/**表示所有的访问都经过静态资源路径;

spring.resources.static-locations=classpath:/static/在这里表示配置静态资源根路径

Thymeleaf.prefix=classpath:/templates/ 在这里作为前缀,表示被thymeleaf修饰的页面在这里作为根目录。(注意:不是静态资源)

Thymeleaf.suffix=.html 在这里表示以.html作为后缀。

请根据你的需要进行配置。

 

给大家参考一下页面中导入各种各样的外部资源应该怎么操作。

我的目录结构因为是这样:

所以导入如下:

导入css:

<link rel="stylesheet" th:href="@{/index/css/owl.theme.css}">

 

导入js:

<script th:src="@{/index/js/vendor/modernizr-2.8.3-respond-1.4.2.min.js}"></script>

 

导入图片:

<img th:src="@{/index/img/logo.png}" alt="Logo">

 

链接网页:

除非你把网页放到静态资源中去,不然还是要通过controller进行跳转。

<li><a th:href="@{/system/login}">- 个人主页</a></li>

 

 

有一些地方需要注意一下:

@{}表示的是调用静态资源那一块,也就是你设置的spring.resources.static-locations=classpath:/static/这里面的内容

不是这里面的内容是获取不到的。

 

@{/index/img/slider/image_1.jpg}

说明一下,@{/.....}里面的/的作用是从你设置的application.yml中

resources:
      static-locations: classpath:/templates/ (意思是http://localhost:8080从这里开始)

开始的,如:

 http://localhost:8080/index/img/slider/image_1.jpg(正确)

如果不加上就是从你调用的controller层类上面那个映射开始,如

@RequestMapping("/system")
public class SystemController {

........

}

system开始.如:

 http://localhost:8080/system/index/img/slider/image_1.jpg(错误)

所以根据你项目结构而加以区别。

一般来说请你加上/

 

 

最重要的一点,请不要页面上有th:href=”” 或者th:src=””等带有th:标签然后后面不接任何东西的代码。

这样编辑时不会报错,但是运行之后会报错,而且看了你自己都蒙。找问题根源浪费你大量时间。错误如下:

16:07:39.100 [http-nio-8080-exec-1] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8080-exec-1] Exception processing template "index/index": An error happened during template parsing (template: "class path resource [templates/index/index.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/index/index.html]")

 

https://www.cnblogs.com/zhang-dongliang/p/10968878.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值