springboot2.0 集成thymeleaf

thymeleaf 是按照严格的html格式来执行的,我们平时用的html不是那么严格。因此需要配置成不是很严格的,

#前缀,也就是模板存放的路径
spring.thymeleaf.prefix=classpath:/web/
#编码格式
spring.thymeleaf.encoding=UTF-8
#是否开启缓存
spring.thymeleaf.cache=false
#后缀
spring.thymeleaf.suffix=.html
#设置不严格的html
spring.thymeleaf.mode=LEGACYHTML5

pom文件



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.1.RELEASE
		 
	
	com.example
	demo
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot

	
		1.8
	

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

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

 

在html页面获取controller 传来的mode;

Controller:

package com.springboot2.thyemleaf.controller;

import com.springboot2.thyemleaf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * Created by  lpw'ASUS on 2018/5/28.
 */
@Controller
@RequestMapping("/thyemleaf")
public class ThymeleafController {

    @RequestMapping("demo")
    public String testthymeleaf(Model model)throws Exception{
        model.addAttribute("str","hello springboot2.0 -thymeleaf");
        User user = new User("张三",23);
        User user1 = new User("王五",46);
        model.addAttribute("user",user);
        List list=new ArrayList();
        list.add(user);
        list.add(user1);
        model.addAttribute("list",list);
        System.out.print(2525);
        System.out.print(5212);

        model.addAttribute("imgsrc","/img/1.jpg");
        if(true){
            throw new Exception("非运行时异常");
        }
        return "template";
    }
}

htmly页面:一般都是在原来的属性前面加th:




    
    Title


    

thymeleaf 模板


name:
age:

循环主题加内容

下拉列表

图片

当html中th:报错或没有提示时,因为没有引入th标签,但不影响使用,只需要在html标签中添加以下内容即可

 

 

 

你可能感兴趣的:(springboot,thymeleaf)