首先在pom.xml文件中加入thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
然后在resource文件夹下面创建2个文件夹
static文件夹存放js,css,image等文件。templates存放视图文件。
Application.java
HelloController.javapackage com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by Administrator on 2017/6/2. */ @SpringBootApplication public class Application { public static void main(String[] args) { // 启动Spring Boot项目的唯一入口 SpringApplication.run(Application.class, args); } }
package com.my.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2017/6/2.
*/
@Controller
@RequestMapping("/hello")
public class HelloController {
//跳转视图
@RequestMapping("/home")
public String home(Model model) {
model.addAttribute("name","gaoxx");
return "index";
}
//接口方法
@RequestMapping("/test")
@ResponseBody
public Map test(){
Map<String,String> map = new HashMap<String,String>();
map.put("code","200");
map.put("message","查询成功");
return map;
}
}
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" />
<title>Title</title>
</head>
<body>
<span th:text="${name}"></span>
</body>
</html>
运行Application.java
整合视图模板还是蛮简单的。