springboot访问静态页面

一、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二、静态资源位置以及优先级

1.位置

  • static目录:css、js、图片等
  • templates目录:html页面(templates目录下的html页面不能直接访问,需要通过服务器内部进行访问,可以避免无权限的用户直接访问到隐私页面,造成信息泄露)

2.优先级

spring boot默认将/**静态资源访问映射到以下目录:

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources

这四个目录的访问优先级:META-INF/resources > resources > static > public
访问路径与springboot.mvc.static-path-pattern、templates下文件夹等有关

三、编写Controller

1.使用@Controller,不能使用@RestController

因为@RestController返回的是@ResponseBody的json数据,且不走SpringMVC的视图解析流程,所以跳不到html那里

四、前端打包资源重定向
使用vue等框架打包的单页面资源,可以使用类似{module}-web的访问路径,并且在controller中使用通配路径{modele}-web/**指向index.html。

评论

暂无评论