springboot学习笔记(十)

springboot与动态资源

springboot默认不支持jsp。

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

springboot学习笔记(十)_第1张图片

 网页=模板+数据

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

示例:

引入thymeleaf依赖:


		
			org.springframework.boot
			spring-boot-starter
		
		
			org.thymeleaf
			thymeleaf-spring5
		
		
			org.thymeleaf.extras
			thymeleaf-extras-java8time
		

thymeleaf简单入门

1.代码在哪里写?

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

springboot学习笔记(十)_第2张图片

 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 map) {
		map.put("welcome", "welcome thymeleaf");
		return "result";
	}
}

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





Insert title here


	

thymeleaf

测试

springboot学习笔记(十)_第3张图片

总结:

th: 替换原来html的值

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

反之,显示获取到的值。

Thymeleaf使用介绍

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

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

th:xx

th:text  获取文本值,转义

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

示例:

hello

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

th:utext  显示

hello

,不渲染

符号问题(第四章)

遍历问题:

NAME PRICE IN STOCK
Onions 2.41 yes

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

你可能感兴趣的:(Springboot入门基础)