spring boot Web开发

模板引擎:


语法规则:


方法使用:

1.引入thymeleaf

Spring Boot默认就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依赖即可:

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

2.编写模板文件在src/main/resouces/templates目录下添加html文件

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello.v.2</h1>
<p th:text="${hello}"></p>
</body>
</html>
3.编写controller 测试
package cn.vp.Controller;

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

import java.util.Map;

@Controller
public class TemplateController {
    @RequestMapping("/helloHtml")
    public String helloHtml(Map<String,Object> map){
String a="from TemplateController.helloHtml";
        map.put("hello",a);
        return "/helloHtml";
    }
}

基本操作:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello World!</title>
</head>
<body>
<h1 th:text="${person.name}">Hello.v.2</h1>
<div th:text="${person.age}"></div>
<div th:text="${book.person.name}"></div>
<hr/>
<div th:text="${session.book.name}"></div>
<hr/>
<div th:text="${authors[1].age}"></div>
<div th:text="${maps.get('无敌强').name}"></div>
<br/>
<h4>基本选择结构的使用</h4>
<span th:if="${person.age} <=20">你老了</span>
<span  th:unless="${person.age} <=20">你还是小鲜肉</span>
<br/>
<h4>switch选择结构的使用</h4>
<div th:switch="${person.age}">
    <span th:case="10">今年10岁</span>
    <span th:case="9999">今年9999岁</span>
    <span th:case="*">年龄无限大</span>
</div>
<br/>
<h4>循环的使用</h4>
<select >
    <option  th:each="author, authorstart :${authors}" th:value="${authorstart.index}" th:text="${author.name}"></option>
</select>
<br/>
<h4>单选按钮</h4>
<input   th:checked="${book.person.age==1}" type="radio" name="love">莉莉 <input th:checked="${book.person.age==2}" type="radio" name="love">姐姐
</body>
</html>

Thymeleaf语法规则

网址:https://www.cnblogs.com/nuoyiamy/p/5591559.html 


猜你喜欢

转载自blog.csdn.net/qq_40781110/article/details/80074647