最近做一个小功能,一个html单页完成信息展示及操作,前端使用Vue + ElementUI,后端使用springboot提供接口,需要在后端springboot项目中直接访问html页面,这样做比较简单。

1、先写一个springboot基础项目

这里不做介绍,可参考博文:SpringBoot入门:SpringBoot之HelloWorld

2、resources下新增配置文件application.yml

server:
  port: 8080
spring:
  mvc:
    view:
      prefix: /
      suffix: .html

3、resources下新建static文件夹,新增hello.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>这是一个html页面</title>
</head>
<body>
	<h1>springboot访问html页面</h1>
</body>
</html>

4、web层调整

@Controller
public class HelloWorldController {
	
    @RequestMapping("/hello")
    public String index() {
        return "hello";
    }
}

启动springboot, 浏览器访问http://127.0.0.1:8080/hello