• SpringBoot2-[模板引擎-Thymeleaf]


    在这里插入图片描述

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


    文章目录

    模板引擎-Thymeleaf

    • SpringBoot不支持JSP

    • Thymeleaf:现代化、服务端Java模板引擎

    • 引入Thymeleaf支持

      
                  org.springframework.boot
                  spring-boot-starter-thymeleaf
      
      
      • 1
      • 2
      • 3
      • 4
    • 基本语法:

      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、特殊操作

      无操作: _

    • 视图解析

      • 返回值以 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})
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
      • 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";
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
      • 代码实现:

        success.html

        
        
        
            
            Welcome Page!
        
        
        
        占位符1
        进入百度! 进入百度!
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12

        ViewController.java

        @Controller
        public class ViweController {
            /**
             *
        将参数写入请求域中,发给模版引擎
        * @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
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
      • 公共代码块抽取-三种方式

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

          抽取:

          © 2011 The Good Thymes Virtual Grocery
          • 1
          • 2
          • 3

          选择器抽取

          • 1
          • 2
        2. 选择公共代码块使用的方式

          - 将footer以及其内部内容放到div内部,也就是当前的div也包括
          - 将footer以及其内部内容放到div所在的位置,div将没有
          - 将footer内容放到当前的div中,包括div不包括footer
          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
    • 拦截器

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

        @Slf4j
        public class LoginInterceptor implements HandlerInterceptor {

      2. 配置拦截器

        @Configuration
        public class WebConfig implements WebMvcConfigurer {
        /**

      拦截器原理
      在这里插入图片描述

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    java计算机毕业设计师大家教中心管理系统源程序+mysql+系统+lw文档+远程调试
    【C++】LeetCode 160 相交链表
    [环境配置][原创]matconv在windows上GPU编译成功的环境
    Matlab:有关字符串数组的常见问题解答
    Spring编程常见错误50例-Spring Bean定义常见错误
    Linux下利用Docker快速部署Kafka
    旅游景区一体化污水处理设备产品特点
    好心情患者故事:节食暴食反复横跳,我确诊了重度抑郁
    (学习日记)2022.8.11
    《数据结构》——时间和空间复杂度
  • 原文地址:https://blog.csdn.net/m0_67393619/article/details/126114355