SpringBoot2 学习5集成Thymeleaf

整个项目文件结构

API 参考地址:https://www.e-learn.cn/thymeleaf/standard-dialects
SpringBoot2 学习5集成Thymeleaf_第1张图片

POM

关键:

org.springframework.boot
spring-boot-starter-thymeleaf


  4.0.0
  
    zz
    SpringBoot2
    0.0.1-SNAPSHOT
  
  SpringBootThymeleaf
  
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
         org.springframework.boot
            spring-boot-devtools
            true
        
        
         
		
		    mysql
		    mysql-connector-java
		    8.0.15
		
		
		
		    org.springframework.boot
		    spring-boot-starter-thymeleaf
		
	
  

        
            alimaven
            aliyun maven
            http://maven.aliyun.com/nexus/content/groups/public/
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

Controller

package com.zz.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.zz.entity.Account;


@Controller
public class IndexController {
	
	@RequestMapping("/account")
	public String index(Model m) {
		List list = new ArrayList();
		list.add(new Account("KangKang", "康康", "e10adc3949ba59abbe56e", "超级管理员", "17777777777"));
		list.add(new Account("Mike", "麦克", "e10adc3949ba59abbe56e", "管理员", "13444444444"));
		list.add(new Account("Jane","简","e10adc3949ba59abbe56e","运维人员","18666666666"));
		list.add(new Account("Maria", "玛利亚", "e10adc3949ba59abbe56e", "清算人员", "19999999999"));
        m.addAttribute("accountList",list);
        return "account";
	}
}

template




    account
    
    


    
no account name password accountType tel

测试效果

SpringBoot2 学习5集成Thymeleaf_第2张图片

语法

SpringBoot2 学习5集成Thymeleaf_第3张图片





demo1



${...} : 变量表达式。 演示

1 取值,直接显示

你好

2 对象,显示

3 List,显示

遍历(迭代)的语法th:each="自定义的元素变量名称 : ${集合变量名称}": 状态变量的使用语法:th:each="自定义的元素变量名称, 自定义的状态变量名称 : ${集合变量名称}": 不管什么时候,Thymeleaf 始终会为每个th:each创建一个状态变量, 默认的状态变量名称就是自定义的元素变量名称后面加Stat字符串组成
名字 电话

controller

package com.zz.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.zz.entity.Account;


@Controller
@RequestMapping("/thymeleaf")
public class DemoController {
	
	@RequestMapping("/demo1")
	public String to_demo1(Model m) {
		m.addAttribute("helloname", "jerry");
		Account account=new Account();
		account.setName("小明");
		m.addAttribute("a", account);
		
		Account account1=new Account();
		account1.setName("小明");
		account1.setTel("23424");
		Account account2=new Account();
		account2.setName("小红");
		account2.setTel("322");
		Account account3=new Account();
		account3.setName("小王");
		account3.setTel("111");
		List clist=new ArrayList();
		clist.add(account1);
		clist.add(account2);
		clist.add(account3);
		m.addAttribute("cs", clist);
		
        return "demo1";
	}
}

你可能感兴趣的:(SpringBoot)