SpringBoot2-[模板引擎-Thymeleaf]

简介: SpringBoot2-[模板引擎-Thymeleaf]

在这里插入图片描述

👨🏻‍🎓博主介绍:大家好,我是芝士味的椒盐,一名在校大学生,热爱分享知识,很高兴在这里认识大家🌟
🌈擅长领域:Java、大数据、运维、电子
🙏🏻如果本文章各位小伙伴们有帮助的话,🍭关注+👍🏻点赞+🗣评论+📦收藏,相应的有空了我也会回访,互助!!!
🤝另本人水平有限,旨在创作简单易懂的文章,在文章描述时如有错,恳请各位大佬指正,在此感谢!!!

@[TOC]

模板引擎-Thymeleaf

  • SpringBoot不支持JSP
  • Thymeleaf:现代化、服务端Java模板引擎
  • 引入Thymeleaf支持

    image.png

  • 基本语法:

    ### 1、表达式

    ### 2、字面量

    文本值: 'one text' , 'Another one!' ,…数字: 0 , 34 , 3.0 , 12.3 ,…布尔值: true , false

    空值: null

    变量: one,two,.... 变量不能有空格

    ### 3、文本操作

    字符串拼接: +

    变量替换: |The name is ${name}|

    ### 4、数学运算

    运算符: + , - , * , / , %

    ### 5、布尔运算

    运算符: and , or

    一元运算: ! , not

    ### 6、比较运算

    比较: > , < , >= , <= ( gt , lt , ge , le )等式: == , != ( eq , ne )

    ### 7、条件运算

    If-then: (if) ? (then)

    If-then-else: (if) ? (then) : (else)

    Default: (value) ?: (defaultvalue)

    ### 8、特殊操作

    无操作: _

    • ⚠️ 注意:当文本不在一个标签中的时候·,需要使用行内写法,比如

    image.png
    行内写法

    image.png

  • 视图解析

    • 返回值以 forward: 开始: new InternalResourceView(forwardUrl); --> 转发request.getRequestDispatcher(path).forward(request, response);
    • 返回值以 redirect: 开始: new RedirectView() --》 render就是重定向
    • 返回值是普通字符串: new ThymeleafView()--->
  • Thymeleaf使用

    • SpringBoot已经配置好了基本的Thymeleaf的属性

      
      
      @Configuration(
          proxyBeanMethods = false
      )
      @EnableConfigurationProperties({ThymeleafProperties.class})
      @ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
      @AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
      
      
    • ThymeleafProperties.java

      
      
      public class ThymeleafProperties {
          private static final Charset DEFAULT_ENCODING;
          public static final String DEFAULT_PREFIX = "classpath:/templates/";
          public static final String DEFAULT_SUFFIX = ".html";
          private boolean checkTemplate = true;
          private boolean checkTemplateLocation = true;
          private String prefix = "classpath:/templates/";
          private String suffix = ".html";
          private String mode = "HTML";
      
      
    • 代码实现:

      success.html
      image.png

      ViewController.java

      
      
      @Controller
      public class ViweController {
          /**
           *<div>将参数写入请求域中,发给模版引擎</div>
           * @param model
           * @return 页面字符串和thymeleaf的前缀/template 以及后缀.html进行拼接
           */
          @GetMapping("/aitu")
          public String success(Model model){
              model.addAttribute("msg","你好世界!");
              model.addAttribute("link","https://www.baidu.com");
              return "success";
          }
      }
      
      
    • 公共代码块抽取-三种方式

      1. 首先选择使用footer标签抽取,还是使用选择器抽取

        抽取:

        image.png

        选择器抽取

        image.png

        2.选择公共代码块使用的方式
        image.png

  • 拦截器

    1. 定制拦截器需要实现HandlerInterceptor 接口

      @Slf4j
      public class LoginInterceptor implements HandlerInterceptor {
    2. 配置拦截器

      @Configuration
      public class WebConfig implements WebMvcConfigurer {
          /**

    拦截器原理
    image.png

相关文章
|
4月前
|
JSON Java 数据格式
SpringBoot下thymeleaf使用UEditor
以前传统web工程下使用UEditor是继承ActionEnter实现自己的MyActionEnter来实现自定义文件上传路径的
35 0
|
6月前
|
Java
15 SpringBoot模板引擎
15 SpringBoot模板引擎
29 0
|
8月前
|
移动开发 缓存 前端开发
|
8月前
|
存储 前端开发 安全
SpringBoot项目中MVC使用--【JSB系列之010】
SpringBoot项目中MVC使用--【JSB系列之010】
|
缓存 Java
springboot整合thymeleaf
springboot整合thymeleaf
179 0
SpringBoot整合thymeleaf
SpringBoot整合thymeleaf
|
XML 移动开发 前端开发
Springboot整合Thymeleaf
Springboot整合Thymeleaf
137 0
|
前端开发 Java API
SpringBoot学习---thymeleaf模板引擎
SpringBoot学习---thymeleaf模板引擎
SpringBoot学习---thymeleaf模板引擎
|
Java Android开发
SpringBoot入门:SpringBoot整合Freemarker和Thymeleaf模板
关于springboot项目的创建可以看下面这篇文章,这里不进行叙述,可以参考之前的文章SpringBoot入门:使用IDEA和Eclipse构建第一个SpringBoot项目。
142 0
SpringBoot入门:SpringBoot整合Freemarker和Thymeleaf模板
|
Java Spring
SpringBoot 整合 Thymeleaf
jsp输出页面是通过 response.getWriter().write 去一点一点写会给浏览器,效率低,我们也发现了这个问题,于是,在 SpringBoot 中提倡使用一种新的视图解析,他就是 Thymeleaf,他是把整个 HTML 页面直接返回,效率高,使用方式就是先在 pom.xml 中引入 spring-boot-starter-thymeleaf 坐标,然后配置下视图解析器,和 jsp 的视图解析器类似,之后就可以使用了,非常简单
114 0
SpringBoot 整合 Thymeleaf