一.新建项目:03-spt-thymeleaf
1.1 新建pom文件
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.2 新建controller层
package com.ljf.spring.boot.demo.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @ClassName: LoginController
* @Description: TODO
* @Author: liujianfu
* @Date: 2020/08/06 11:50:46
* @Version: V1.0
**/
@Controller
public class LoginController {
@RequestMapping("/show")
public String showInfo(Model model){
model.addAttribute("msg", "Thymeleaf 第一个案例");
return "index";
}
}
1.3 新建启动类
package com.ljf.spring.boot.demo.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println("整个thymeleaf启动程序完成!!!");
}
}
1.4 新建资源配置类
在resources文件夹下新建templates文件夹,新建index.html,内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thymeleaf入门</title>
</head>
<body>
<span th:text="Hello"></span>
<hr/>
<span th:text="${msg}"></span>
</body>
</html>