notas de estudio de springboot (10)

Springboot y recursos dinámicos

Springboot no es compatible con jsp de forma predeterminada.

Se recomienda utilizar el motor de plantillas (thymeleaf) para el montaje:

 Página web = plantilla + datos

El motor de plantillas que usamos aquí es thymeleaf

Ejemplo:

Introduce la dependencia de la hoja de tomillo:

<!-- 引入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>

Introducción simple a la hoja de tomillo

1. ¿Dónde está escrito el código?

Según el código fuente, solo necesita poner el archivo thymeleaf en "classpath: / templates /" y el sufijo del archivo es ".html".

 2. Ejemplo:

Primero defina un controlador:

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";
	}
}

 Cree una nueva página html en las plantillas de la ruta interna

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

prueba

para resumir:

th: reemplaza el valor html original

Si no se obtiene $ {welcome}, se muestra thymeleaf

De lo contrario, se muestra el valor obtenido.

Introducción a Thymeleaf

Los documentos oficiales se pueden descargar desde el sitio web oficial. https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf

th: problema de valor (capítulo diez)

th: xx

th: text obtiene el valor del texto, escape

th: utext obtiene el valor del texto sin escapar

Ejemplo:

<h1> hola </h1>

th: el texto muestra el efecto de renderizar hola como h1

th: utext muestra <h1> hola </h1> sin renderizar

Problema de símbolos (Capítulo 4)

Problema transversal:

<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>

Inconscientemente, ¡los conceptos básicos de springboot están casi terminados! Espero usarlo lo antes posible, jajaja, ¡feliz!

Supongo que te gusta

Origin blog.csdn.net/dongjinkun/article/details/82980399
Recomendado
Clasificación