spring boot访问静态页面
时间: 2023-07-28 15:10:57 浏览: 55
在 Spring Boot 中访问静态页面非常简单,只需要将静态页面放置在指定的目录下即可。默认情况下,Spring Boot 将静态资源放置在`/static`、`/public`、`/resources`或`/META-INF/resources`目录下。
例如,假设您有一个名为`index.html`的静态页面,您可以将其放置在`/src/main/resources/static`目录下,然后通过以下方式访问:
```
http://localhost:8080/index.html
```
如果您的静态页面放置在其他目录下,可以通过在`application.properties`文件中设置`spring.resources.static-locations`属性来指定静态资源的路径,例如:
```
spring.resources.static-locations=classpath:/static/,classpath:/public/,file:/path/to/your/static/files/
```
其中,`classpath:/static/`和`classpath:/public/`是默认的静态资源路径,`file:/path/to/your/static/files/`为您自定义的静态资源路径。
需要注意的是,在访问静态资源时,Spring Boot 会根据静态资源的文件扩展名自动推断其`Content-Type`,例如,`.html`扩展名的文件会被推断为`text/html`类型。如果您的静态资源不是按照扩展名来命名的,需要在`application.properties`文件中配置`spring.mvc.static-path-pattern`属性来指定静态资源的URL。例如,如果您希望访问`.txt`扩展名的静态资源,可以将`spring.mvc.static-path-pattern`设置为`/resources/**`,然后将`.txt`文件放置在`/src/main/resources/resources`目录下,通过以下方式访问:
```
http://localhost:8080/resources/yourfile.txt
```
希望以上内容能够帮助到您。