springboot的thymeleaf语法

SpringBoot的资源映射

资源映射在5个文件中:

classpath:/META-INF/resources/  
classpath:/resources/  注意,这里是在我们的resources类别的目录中新建的resources文件夹
classpath:/static/    同上
classpath:/public/    同上
/  					 这是项目根路径

注意:有关web模块的映射都在webmvcautoconfiguration类中进行配置。上述的5个路径也是在源码中 发现

欢迎页映射

就是index文件,默认情况下在/**映射,换句话说,就是可以被任何路径做出映射。

注意:还有一个就是自己的网站图标的设置,以xxx.ico文件为准,默认作为访问图标。

springboot的web模式

问题:在springboot中,我们一般使用maven来构建项目,但是一般情况下,我们无法使用jsp页面,因为在sprinboot中,没有wbeapp目录。
所以就引入了 模板引擎,常见的模板引擎有:jsp,velocity,freemarker,thymeleaf.
模板引擎的原理,基本都是一个模板(template)+数据
以JSP举例,我们在使用jsp的时候,可以在servlet中使用其中的域对象存储值,然后在jsp可以写java代码获取值,然后输出到进行输出。

springboot推荐thymeleaf:

引入thymeleaf:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
version我使用的是3.0
引入后导入了两个jar包:
一个是自己的,一个是和spring整合的。
如下图:

在这里插入图片描述

在properties中导入如下配置。
原因为:layout2可以支持thymeleaf3,但是如果使用的thmeleaf2那么也可以使用layout1

<thymeleaf.version>3.0.11</thymeleaf.version>
        <!--布局功能支持的程序 thymeleaf3主程序,layout以上的版本-->
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>

springboot的thymeleaf的用法

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

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

	public static final String DEFAULT_SUFFIX = ".html";
	如果我们将html页面放入templates/ 那么就会被自动渲染。

关于thymeleaf的使用文档,参考下面的链接

thymeleaf的PDF文档
基本的结构如下:
在这里插入图片描述

thymeleaf的例子
Controller层

Controller层的简易说明

视图层(html)

视图层解析

小白学习历程,希望有人给出指正。

Spring Boot Thymeleaf项目是使用Spring Boot框架和Thymeleaf模板引擎来构建的。在配置Spring Boot Thymeleaf项目时,需要进行以下几个步骤: 1. 在pom.xml文件中引入Thymeleaf依赖。可以使用以下代码片段将Thymeleaf依赖添加到pom.xml文件中: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 这将确保项目中包含Thymeleaf所需的所有依赖项。\[2\] 2. 在application.properties或application.yml文件中添加Thymeleaf的配置。可以使用以下代码片段来配置Thymeleaf的相关属性: ``` spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false spring.thymeleaf.suffix=.html spring.thymeleaf.servlet.content-type=text/html ``` 这些配置将指定Thymeleaf模板文件的位置、编码方式、缓存设置等。\[1\] 3. 在项目中使用Thymeleaf模板引擎来创建和渲染页面。可以使用Thymeleaf语法和标签来编写动态的HTML页面。在Spring Boot中,可以将Thymeleaf模板文件放置在src/main/resources/templates目录下,并使用@Controller注解的控制器类来处理请求并返回Thymeleaf模板。\[1\] 通过以上步骤,你就可以开始使用Spring Boot和Thymeleaf来构建动态的Web应用程序了。 #### 引用[.reference_title] - *1* *2* [springBoot整合thymeleaf(超简单)](https://blog.csdn.net/baobei_ll/article/details/124213027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [springboot使用thymeleaf](https://blog.csdn.net/qq_41378597/article/details/85644813)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值