<!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";
}
}
http://localhost:8080/index.html 成功返回
http://localhost:8080/map1 成功返回
http://localhost:8080/index2.html 404返回
http://localhost:8080/map1 404返回
[Ref] SpringBoot如何返回页面
如果使用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 成功返回