由于对前端开发并不精通,
因此计划使用一个静态的调用关系来调用HTML文件
1. Controller
package com.ais.devops;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {
@RequestMapping("/index")
public String root() {
return "index";
}
@GetMapping("/index2")
public String root2() {
return "index";
}
}
说明:RequestMapping和GetMapping两种方案均未发现异常。可以正确读取静态资源
说明:class类注解不能使用RestController注解
2.配置thymeleaf
2.1 POM中增加dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.2 在application.properties中增加配置项
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
##开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
2.3 增加纯静态index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<h1>springboot访问第一个html页面</h1>
</body>
</html>
说明:
1.实际index.html放置的路径是bootstrap-start/src/main/resources/templates/index.html
2.template不行,必须使用templates文件夹
3.有一种说法是静态页面应当把prefix指向bootstrap-start/src/main/resources/static/pages/index.html,实测无法调用
4.meta以及其他参数最后都要增加 / 否则thymeleaf无法正确解析
5.代码中不能使用&&和& (原因不明)
即可调用该index.html
参考
https://blog.youkuaiyun.com/wangmx1993328/article/details/81054474