springboot学习笔记(十)

springboot与动态资源

springboot默认不支持jsp。

推荐使用模板引擎(thymeleaf)进行组装:

 网页=模板+数据

此处我们使用的模板引擎是thymeleaf

示例:

引入thymeleaf依赖:

<!-- 引入thymeleaf依賴 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf-spring5</artifactId>
		</dependency>
		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-java8time</artifactId>
		</dependency>

thymeleaf简单入门

1.代码在哪里写?

通过源码得知,只需要将thymeleaf文件放入"classpath:/templates/",并且文件的后缀是".html"即可。

 2.示例:

首先定义一个控制器:

package com.example.demo.controller;

import java.util.Map;

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

@Controller
public class BootController {
	@RequestMapping("welcome")
	public String welcome(Map<String,Object> map) {
		map.put("welcome", "welcome thymeleaf");
		return "result";
	}
}

 在内路径的templates中新建一个html页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<p th:text = "${welcome}">thymeleaf</p>
</body>
</html>

测试

总结:

th: 替换原来html的值

如果没有获取到${welcome},则显示thymeleaf

反之,显示获取到的值。

Thymeleaf使用介绍

官方文档可以去官网上下载!https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf

th:的取值问题(第十章)

th:xx

th:text  获取文本值,转义

th:utext  获取文本值,不转义

示例:

<h1>hello</h1>

th:text  显示将hello渲染为h1后的效果

th:utext  显示<h1>hello</h1>,不渲染

符号问题(第四章)

遍历问题:

<table>
<tr>
<th>NAME</th>
<th>PRICE</th>
<th>IN STOCK</th>
</tr>
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
</table>

不知不觉,springboot基础已经快学完了!希望能尽快的用到,哈哈哈,高兴!

猜你喜欢

转载自blog.csdn.net/dongjinkun/article/details/82980399
今日推荐