Spring Boot集成Thymeleaf简单实例

1、定义

Thymeleaf是一种用于Web和独立环境的现代服务器端的Java模板引擎。

2、简单实例

(1)目录结构

Spring Boot集成Thymeleaf简单实例_第1张图片

(2)MySpringBootApplication.java

package cn.hwd.thymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringBootApplication {

	public static void main(String[] args) {
		SpringApplication.run(MySpringBootApplication.class, args);
	}

}

(3)HelloController.java

package cn.hwd.thymeleaf.controller;

import javax.servlet.http.HttpServletRequest;

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

@Controller
@RequestMapping(value = "/hello")
public class HelloController {

	@RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(HttpServletRequest request) {
		request.setAttribute("message", "Hello world.");
		return "index";
    }
	
}

(4)index.html






Insert title here


	

(5)default.css

.hello {color: red;}

(6)application.properties

spring.thymeleaf.cache = false

(7)pom.xml




  4.0.0

  cn.hwd
  thymeleaf01
  0.0.1-SNAPSHOT
  jar

  thymeleaf01
  http://www.example.com

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

(8)运行结果

Spring Boot集成Thymeleaf简单实例_第2张图片

你可能感兴趣的:(Spring,Boot)