@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/