In spring boot after integrated thymeleaf, we know that the default path for the html thymeleaf classpath: / templates is resources / templates, how to access this path that the following static pages? Suppose we want to visit a page for the hello.html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hell spring boot!!</h1>
</body>
</html>
The page is located in the templates, of course, you can also modify the default path application.properties file.
spring.thymeleaf.prefix=classpath:/templates
A method for use directly back to the controller in the page
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(){
//在集成thymeleaf后 会在默认路径下寻找名字为hello的html页面
return "hello";
}
}
2. The method implemented addViewControllers WebMvcConfigure interface mapping a route
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//第一个路径为类似于Controller中的接口的路径 第二个view为要访问的页面
//实现不需要进行数据渲染的页面的路径映射 当然这些页面没有在默认的五个静态页面访问路径下
registry.addViewController("/hellp").setViewName("hello");
//如果需要添加多个页面直接在下面继续添加即可
}
}