前提

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
</head>
<body>
你好!初学者,我是SpringBoot的简单启动页面!
</body>
</html>
 

@Controller
public class HelloController {
    @RequestMapping("/map1")
    public String index() {
        return "index.html";
    }
    @RequestMapping("/map2")
    public String map2() {
        return "index2.html";
    }
}
 

不使用 模板引擎
访问的页面放在resources/static/,就可直接访问这个页面
http://localhost:8080/index.html  成功返回
http://localhost:8080/map1        成功返回
 
访问的页面放在resources/templates/,禁止访问这个页面
http://localhost:8080/index2.html  404返回
http://localhost:8080/map1         404返回
 
[Ref] SpringBoot如何返回页面

使用 模板引擎
使用 springmvc 配置
如果使用spring-boot-starter-parent,就无需引入依赖
spring:
  mvc:
    view:
      suffix: .html
    static-path-pattern: /**
  web:
    resources:
      static-locations: classpath:/templates/,classpath:/static/
 

直接访问资源
http://localhost:8080/index.html    成功返回
http://localhost:8080/index.htm2    成功返回
http://localhost:8080/map1    报404
http://localhost:8080/map2    报404
调用接口需要 redirect
 
@Controller
public class HelloController {
    @RequestMapping("/map1")
    public String index() {
        return "redirect:index.html";
    }
    @RequestMapping("/map2")
    public String map2() {
        return "redirect:index2.html";
    }
}
 
http://localhost:8080/index.html    成功返回
http://localhost:8080/index.htm2    成功返回
http://localhost:8080/map1  成功返回
http://localhost:8080/map2  成功返回
 
使用Thymeleaf模板引擎
成功返回

404返回




















