springboot配置html页面

最近写了一下springboot , 碰到了一个配置 html 的问题 , 专门 记录一下

首先 说明 , 有两种 访问html 的方式

1.通过后台跳转到 html 页面

现在比较流行的开发模式就是 前后端分离, 在分离的情况下 , 就无法直接访问到 html , 需要通过 后端来跳转

(1.) 添加maven

<!-- 动态页面 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

(2.) 配置 application.yml

spring:
thymeleaf:
prefix:
classpath: /templates # 访问template下的html文件需要配置模板,映射
cache: false # 开发时关闭缓存,不然没法看到实时页面

(3) controller

@Controller
@RequestMapping("/delete/")
public class deleteController {

    @RequestMapping("wrong")
    public String index() {
        return "wrong";
    }
}

注意 :

访问方法跳转页面 方法请求加/  返回到某一个页面不用.后缀名 并且类上的注解改为@controller 不是@rest Controller

通过访问 localhost:8080/delete/wrong 就可以访问到 templates下的wrong.html页面.

2.直接访问 html 页面

配置applicaiton.yml

不用通过方法访问页面
spring:
  resources:
    static-locations: classpath:/static/, classpath:/templates/

就可以通过 localhost:8080/wrong.html 访问

感谢 知知.