springboot返回html页面原理,SpringBoot模板引擎及原理

版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/zhengzhaoyang122/article/details/83686750

一、模板引擎的思想:

模板是为了将显示与数据分离,模板技术多种多样,但其本质都是将模板文件和数据通过模板引擎生成最终的HTML代码。

233b42f71917343f9f740bf123036af1.png

二、SpringBoot模板引擎:

SpringBoot推荐的模板引擎是Thymeleaf——>语法简单,功能强大。

1)、引入thymeleaf的starter启动器。

org.springframework.boot

spring-boot-starter-thymeleaf

3.0.2.RELEASE

2.1.1

2)、查看thymeleaf的默认配置:进入ThymeleafAutoConfiguration的ThymeleafProperties配置文件中,如下:@ConfigurationProperties(

prefix = "spring.thymeleaf"

)

public class ThymeleafProperties {

private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");

private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

public static final String DEFAULT_PREFIX = "classpath:/templates/";

public static final String DEFAULT_SUFFIX = ".html";

private boolean checkTemplate = true;

private boolean checkTemplateLocation = true;

//只要我们吧HTML页面放在classpath:/templates/下就能够自动渲染

private String prefix = "classpath:/templates/";

private String suffix = ".html";

3)、测试:创建controller如下:同时在templates文件夹下创建suceess.html与返回值相同。启动后输入:http://localhost:8080/success便可跳转到success.html页面。@Controller

public class success {

@RequestMapping("/success")

public String success(){

return "success";

}

}

三、thymeleaf使用

1)、导入thymeleaf的名称空间:就会具有thymeleaf的语法提示,不导入也可以,只是么有语法提示了。

2)、写一个简单的demo上个手,如下controller层,给返回的页面添加数据,如下:@Controller

public class success {

@RequestMapping("/success")

public String success(Map map){

map.put("hello","你好");

return "success";

}

}

3)、打开我们的静态页面success.html,根据thymeleaf模板引擎语法,获取hello的值,如下:

成功

这是成功页面

4)、需要注意的是:当hello有值时,显示hello获取到的值,如果单独只访问success.html时,只显示前端页面的内容 “这是成功页面” 能够非常友好的结合前后端进行编程。

7a15eb895215aac314aad2f6cb25ee37.png    

3003cdf2cc7ced2264c47528a04c77ae.png

四、thymeleaf语法规则:

1)、th:text:改变当前元素里面的文本内容。语法文档:https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.pdf

th:任意html属性:可以替换原生的HTML的元素。

e2386e8b6389b415573ce5c918fea56f.png

2)、表达式语法:行里表达式:[[xx]]—相当于th:text , [(xx)]—相当于th:utext● Simple expressions:(表达式语法)

○ Variable Expressions: ${...}:获取变量值,底层时OGNL;

1)、获取对象的属性、调用方法;

2)、使用内置的基本对象;#location...

3)、内置的一些工具对象;#strings...

○ Selection Variable Expressions: *{...}:选择表达式,与${}的功能一样,有一个不同,可以参考文档。

○ Message Expressions: #{...}:用来获取国际化信息

○ Link URL Expressions: @{...}:用来定义URL连接

○ Fragment Expressions: ~{...}:片段引入表达式

● Literals(字面量)

○ Text literals: 'one text' , 'Another one!' ,…

○ Number literals: 0 , 34 , 3.0 , 12.3 ,…

○ Boolean literals: true , false

○ Null literal: null

○ Literal tokens: one , sometext , main ,…

● Text operations:(文本操作)

○ String concatenation: +

○ Literal substitutions: |The name is ${name}|

● Arithmetic operations:(数学运算)

○ Binary operators: + , - , * , / , %

○ Minus sign (unary operator): -

● Boolean operations:(布尔运算)

○ Binary operators: and , or

○ Boolean negation (unary operator): ! , not

● Comparisons and equality:(比较运算)

○ Comparators: > , = , <= ( gt , lt , ge , le )

○ Equality operators: == , != ( eq , ne )

● Conditional operators:(条件运算)

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

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

○ Default: (value) ?: (defaultvalue)

○ Special tokens:

○ Page 17 of 106

● No-Operation: _:(特殊操作)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值