springboot- thymeleaf

thymeleaf

依赖引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

配置文件

##前缀
spring.thymeleaf.prefix=classpath:/templates/suibian/ 
//后缀
spring.thymeleaf.suffix=.html 
#配置视图模板类型,如果视图模板使用的是 html5 需要配置 
spring.thymeleaf.mode=HTML 
##默认视图编码格式
spring.thymeleaf.encoding=utf-8 
 #响应类型 
spring.thymeleaf.servlet.content-type=text/html
#配置页面缓存 
spring.thymeleaf.cache=false

页面语法

th:id 替换id <input th:id="${user.id}"/>
th:text 文本替换 <p text:="${user.name}">bigsai</p>
th:utext 支持html的文本替换 <p utext:="${htmlcontent}">content</p>
th:object 替换对象 <div th:object="${user}"></div>
th:value 替换值 <input th:value="${user.name}" >
th:each 迭代 <tr th:each="student:${user}" >
th:href 替换超链接 <a th:href="@{index.html}">超链接</a>
th:src 替换资源 <script type="text/javascript" th:src="@{index.js}"></script>

对象可以使用${对象名.对象属性}或者${对象名['对象属性']}来取值
Map取值你可以${Map名['key']}来进行取值。也可以通过${Map名.key}取值,当然你也可以使用${map.get('key')}(java语法)来取值

首先在templates目录下建立home.properties中写入以下内容:

bigsai.nane=bigsai
bigsai.age=22
province=Jiang Su

在application.properties中加入以下内容:
spring.messages.basename=templates/home

<h2>消息表达</h2>
<table bgcolor="#ffe4c4" border="1">
    <tr>
    <td>name</td>
    <td th:text="#{bigsai.name}"></td>
    </tr>
    <tr>
    <td>年龄</td>
    <td th:text="#{bigsai.age}"></td>
    </tr>
    <tr>
    <td>province</td>
    <td th:text="#{province}"></td>
    </tr>
</table>

判断是不是为空:null:

<span th:if="${name} != null">不为空</span> 
<span th:if="${name1} == null">为空</span>

判断是不是为空字符串: “”

<span th:if="${#strings.isEmpty(name1)}">空的</span>

判断是否相同:

<span th:if="${name} eq 'jack'">相同于jack,</span> 
<span th:if="${name} eq 'ywj'">相同于ywj,</span> 
<span th:if="${name} ne 'jack'">不相同于jack,</span>

不存在设置默认值:

<span th:text="${name2} ?: '默认值'"></span>

是否包含(分大小写):

<span th:if="${#strings.contains(name,'ez')}">包ez</span> 
<span th:if="${#strings.contains(name,'y')}">包j</span>

是否包含(不分大小写)

<span th:if="${#strings.containsIgnoreCase(name,'y')}">包j</span>

下面的和JAVA的String基本一样

${#strings.startsWith(name,'o')} 
${#strings.endsWith(name, 'o')} 
${#strings.indexOf(name,frag)}// 下标 
${#strings.substring(name,3,5)}// 截取
${#strings.substringAfter(name,prefix)}// 从 prefix之后的一位开始截取到最后,比如 (ywj,y) = wj, 如果是(abccdefg,c) = cdefg//里面有2个c,取的是第一个c 
${#strings.substringBefore(name,suffix)}// 同上,不过是往前截取 
${#strings.replace(name,'las','ler')}// 替换 
${#strings.prepend(str,prefix)}// 拼字字符串在str前面 
${#strings.append(str,suffix)}// 和上面相反,接在后面 
${#strings.toUpperCase(name)} 
${#strings.toLowerCase(name)} 
${#strings.trim(str)} 
${#strings.length(str)} 
${#strings.abbreviate(str,10)}// 我的理解是 str截取0-10位,后面的全部用…这个点代替,注意,最小是3位
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot集成Thymeleaf可以通过以下步骤完成: 1. 在项目的pom.xml文件中添加Thymeleaf的依赖项。可以使用以下代码片段添加Thymeleaf starter依赖: ``` <!-- thymeleaf 相关依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 这将自动导入Thymeleaf的相关依赖项。 2. 在application.properties文件中添加Thymeleaf的相关配置。可以使用以下代码片段添加配置: ``` # 关闭Thymeleaf的缓存,方便开发过程中无需重启 spring.thymeleaf.cache=false # 设置Thymeleaf页面的编码 spring.thymeleaf.encoding=UTF-8 # 设置Thymeleaf页面的后缀 spring.thymeleaf.suffix=.html # 设置Thymeleaf页面的存储路径 spring.thymeleaf.prefix=classpath:/templates/ ``` 这些配置将确保Thymeleaf与Spring Boot的自动配置完美整合。 3. 在项目中创建Thymeleaf模板文件。可以将模板文件存储在src/main/resources/templates目录下。使用Thymeleaf的语法编写模板文件,可以在模板中使用动态数据和表达式。 通过以上步骤,你就可以在Spring Boot项目中成功集成Thymeleaf,并使用Thymeleaf作为模板引擎来开发你的项目了。 #### 引用[.reference_title] - *1* *3* [SpringBoot 整合Thymeleaf教程及使用](https://blog.csdn.net/yangyifan0/article/details/128069902)[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^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot集成Thymeleaf](https://blog.csdn.net/yinyin_xiao/article/details/125072715)[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^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值