@RestController = @Controller + @ResponseBody
使用@Controller 註解,在對應的方法上,視圖解析器可以解析return 的jsp,html頁面,並且跳轉到相應頁面
若返回json等內容到頁面,則需要加@ResponseBody註解
即 若加的是@ResponseBody註解,則不能返回到html等頁面,返回的是json內容到頁面
@Controller
@EnableAutoConfiguration
public class HelloController {
@RequestMapping("/test")
public String test(){
return "index";
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class);
}
}
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<p>我是INDEX</p>
</body>
</html>
application.yml
spring:
thymeleaf:
prefix: classpath:/templates/