RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
SpringBoot怎么引入Thymeleaf方法

这篇文章主要介绍SpringBoot怎么引入Thymeleaf方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联公司2013年成立,是专业互联网技术服务公司,拥有项目成都网站建设、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元益阳做网站,已为上家服务,为益阳各地企业和个人服务,联系电话:18980820575

1、Thymeleaf简介

Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用
Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模,Thymeleaf的可扩展性也非常棒。你可以使用它定义自己的模板属性集合,这样就可以计算自定义表达式并使用自定义逻辑,Thymeleaf还可以作为模板引擎框架。

2、引入Thymeleaf

引入依赖

在maven(pom.xml)中直接引入:


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


    org.springframework.boot
    spring-boot-starter-web

配置Thymeleaf

在application.yml配置Thymeleaf

server:
  port: 8000
spring:
  thymeleaf:
    cache: false # 关闭页面缓存
    encoding: UTF-8 # 模板编码
    prefix: classpath:/templates/  # 页面映射路径
    suffix: .html # 试图后的后缀
    mode: HTML5 # 模板模式

# 其他具体配置可参考org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
# 上面的配置实际上就是注入该类的属性值

demo示例

创建IndexController

@Controller
public class IndexController {
    // 返回视图页面
    @RequestMapping("index")
    public String index(){
        return "index";
    }

}

创建index.html




    
    Title


    Hello Thymeleaf!

创建TestController

@RestController
public class TestController {
    
    // 返回整个页面
    @RequestMapping("/test")
    public ModelAndView test(){
        return new ModelAndView("test");
    }
}

创建test.html




    
    Title


Hello Thymeleaf! 
By: ModelAndView

3、测试结果

SpringBoot怎么引入Thymeleaf方法

4、Thymeleaf基础语法及使用

引入标签 

html标签里引入xmlns:th="http://www.thymeleaf.org"才能使用th:*这样的语法

引入URL 

@{...}

例如:

绝对路径 是访问绝对路径下的URL, 相对路径 是访问相对路径下的URL。
是引入默认的static下的css文件夹下的bootstrap文件,类似的标签有: th:href 和 th:src

3.获取变量 

通过${}取值,对于JavaBean的话,使用变量名.属性名获取

4.字符串替换


或者

注意:|…|中只能包含变量表达式${…},不能包含其他常量、条件表达式等

5.运算符 

在表达式中可以使用各类算术运算符  
例如 (+, -, *, /, %)  
例如:th:with="isEven=(${stat.number} % 1 == 0)"  
逻辑运算符 (>, <, <=,>=,==,!=)  
需要注意的是使用<,>的时候需要转义

th:if="${stat.number} > 1"
th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"

6.条件 
  if/unless   th:if是该标签在满足条件的时候才会显示,unless是不成立时候才显示

Login

switch  thymeleaf支持switch结构,默认属性(default)用*表示

User is an administrator

User is a manager

User is some other thing

7.循环


    Onions
    2.41
    yes

8.Utilities

内置在Context中,可以直接通过#访问
#dates  
#calendars  
#numbers  
#strings  
arrays   
lists  
sets   
maps   
…

以上是SpringBoot怎么引入Thymeleaf方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


当前题目:SpringBoot怎么引入Thymeleaf方法
本文来源:http://sczitong.cn/article/pehgdo.html