栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot集成Thymeleaf模板引擎

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot集成Thymeleaf模板引擎

简单介绍
目前在JavaEE领域有几中比较常用的模板引擎,分别是Jsp、Velocity、Freemarker、Thymeleaf,对Freemark语法不是特别熟悉,不过对于前端页面渲染效率来说,jsp其实还是最快的,Velocity次之。Thymeleaf虽然渲染效率不是很快,但是语法方面是比较轻巧的,Thymeleaf语法比Velocity轻巧,但是渲染效率不如Velocity

maven配置
因为引入了SpringBoot的parent工程,所以不需要写版本号

    
      org.springframework.boot
      spring-boot-starter-thymeleaf
    

application.yml配置

#添加Thymeleaf配置
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    content-type: text/html

application.yml:

server:
  port: 8081#logging:#  config: classpath:logback_spring.xml.bat#  level:#    com.muses.taoshop: debug#  path: /data/logsspring:
  datasource:    # 主数据源
    shop:
      url: jdbc:mysql://127.0.0.1:3306/taoshop?autoReconnect=true&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
      username: root
      password: root

    driver-class-name: com.mysql.jdbc.Driver    type: com.alibaba.druid.pool.DruidDataSource    # 连接池设置
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20      # 配置获取连接等待超时的时间
      max-wait: 60000      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000      # Oracle请使用select 1 from dual
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,slf4j      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000      # 合并多个DruidDataSource的监控数据
      use-global-data-source-stat: true#  jpa:#    database: mysql#    hibernate:#      show_sql: true#      format_sql: true#      ddl-auto: none#      naming:#        physical-strategy: org.hibernate.boot.entity.naming.PhysicalNamingStrategyStandardImpl#  mvc:#    view:#      prefix: /WEB-INF/jsp/#      suffix: .jsp

  #添加Thymeleaf配置
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    content-type: text/html  #Jedis配置#  jedis :#    pool :#      host : 127.0.0.1#      port : 6379#      password : redispassword#      timeout : 0#      config :#        maxTotal : 100#        maxIdle : 10#        maxWaitMillis : 100000

添加html文件
在resources资源文件夹下面新建一个templates文件夹,所有的html文件都丢在这里,静态资源文件也丢在resources资源文件夹下面

新建一个html文件,然后注意加上

注意Thymeleaf语法要求比较严格 ,不如这样写是不可以的,必须加上斜杠的,

Thymeleaf简单例子

遍历后台数据

        
            
                 最新上架 每天都有上新,每天都有惊喜 
                
                    
                        
                            
                                  

                                

                                

                                

                                                                                                                          

引入文件
Thymeleaf引入另外一个html文件可以使用th:replace或者th:include,

    
    
    
    
    
    
    
    
    

Img便签src

 链接便签

{item.skuId}}">


静态资源使用


    
    
    
    

给出一个html页面例子:


    
    电商门户网站
    
    
    
    
    
    
        .item-list { display: none; position: absolute; width: 705px; min-height: 200px; _height: 200px; background: #FFF; left: 198px; box-shadow: 0px 0px 10px #DDDDDD; border: 1px solid #DDD; top: 3px; z-index: 1000000; }
        
        .clear { display: block; height: 0; overflow: hidden; clear: both; }
        .clearfix:after { content: '20'; display: block; height: 0; clear: both; }
        .clearfix { *zoom:1;
        }
        .hover { position: relative; z-index: 1000000000; background: #FFF; border-color: #DDD; border-width: 1px 0px; border-style: solid; }    
    
    
    
    
    
    
    
    
    
    
    
        
        
            
                
                        
  •                     
  •                     
  •                 
                                                           
  •                 
  •                                                                                                    话费充值                                                                                

                                手机号:                                                      

                            

                                充值方式:                                                                                   

                             立即充值                                                                                                                                                       最新上架 每天都有上新,每天都有惊喜                                                                                                                                

                                    

                                    

                                    

                                                                                                                                   



    作者:smileNicky
    链接:https://www.jianshu.com/p/3abe63de4cbf


    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/235162.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号