3.springBoot整合thymeleaf

创建项目

1.创建maven项目
在这里插入图片描述
在这里插入图片描述
2.修改pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.synda</groupId>
	<artifactId>10-spring-boot-thymeleaf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.5.RELEASE</version>
	</parent>
	<dependencies>
		<!-- spring-boot启动器:支持全栈式的web开发(包括了romcat和MVC等jar -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<version>2.1.5.RELEASE</version>
		</dependency>
		<!-- 引入thymleaf -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
			<version>2.1.5.RELEASE</version>
		</dependency>
	</dependencies>
</project>

3.创建templates目录:该目录是安全的,意味着外界不可以直接访问;
该目录
3.创建Controller

package com.synda.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DemoController {
	
	@RequestMapping("/show1")
	public String getShowInfo(Model model) {
		model.addAttribute("msg", "这是Thymeleaf练习");
		return "index";
	}
}

4.创建启动类

package com.synda;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class application {

	public static void main(String[] args) {
		SpringApplication.run(application.class, args);
		
	}

}

5.在templates目录下创建index.html
(1)、使用th:text对字符串进行操作;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>字符串操作练习th:text</title>
</head>
<body>
	<!-- 获取字符串 -->
	<span th:text="${msg}"></span>
	<hr />
	<!-- 可以设置为input的value值 -->
	<input type="text" name="username" th:value="${msg}">
	<hr />
	<!-- 判断该字符串是否为空,返回布尔值 -->
	<span th:text="${#strings.isEmpty(msg)}"></span>
	<hr />
	<!-- 判断该字符串是否含有指定字符 返回布尔值-->
	<span th:text="${#strings.contains(msg,'T')}"></span>
	<hr />
	<!-- 查看指定字符在该字符串中的位置,返回该字符串的位置,如果没有,则返回-1 -->
	<span th:text="${#strings.indexOf(msg,'T')}"></span>
	<hr />
	<!-- 判断该字符串是否以指定字符开头 -->
	<span th:text="${#strings.startsWith(msg,'T')}"></span>
	<hr />
	<!-- 判断该字符串是否以给定的字符结尾 -->
	<span th:text="${#strings.endsWith(msg,'s')}"></span>
	<hr />
	<!-- 返回该字符串的长度 -->
	<span th:text="${#strings.length(msg)}"></span>
	<hr />
	<!-- 截取字符串 -->
	<span th:text="${#strings.substring(msg,'13')}"></span>
	<span th:text="${#strings.substring(msg,'10','13)}"></span>
	<hr />
	<!-- 大小写转换 -->
	<span th:text="${#strings.toUpperCase(msg)}"></span>
	<span th:text="{#strings.toLowerCase(msg)}"></span>
	
</body>
</html>

启动项目,访问http://localhost:8080/show,输出如下结果
在这里插入图片描述
(2)、Controller中新建一个方法,请求为show2

@RequestMapping("/show2")
	public String getDateInfo(Model model) {
		model.addAttribute("date", new Date());
		return "index1";
	}

(3)、创建index2对日期进行操作;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 传递两个参数,需要操作的日期,日期格式 -->
	<span th:text="${#dates.format(date,'yyyy-mm-dd')}"></span>
	<!-- 如果没有指定格式,则按照浏览器的默认来显示 -->
	<span th:text="${#dates.format(date)}"></span>
	<!-- 单独获取年/月/日 -->
	<span th:text="${#dates.year(date)"></span>
	<span th:text="${#dates.month(date)"></span>
	<span th:text="${#dates.day(date)"></span>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 <p th

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

synda@hzy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值