SpringBoot 设置静态资源文件
时间: 2024-03-21 16:37:55 浏览: 90
在SpringBoot中,可以通过以下步骤来设置静态资源文件:
1. 在src/main/resources目录下创建static文件夹,然后在该文件夹下创建需要的静态资源文件夹,比如css、js和images等。
2. 在application.properties或application.yml文件中进行配置,指定静态资源文件的位置。例如:
application.properties:
```properties
## 静态资源文件路径
spring.resources.static-locations=classpath:/static/
```
application.yml:
```yaml
## 静态资源文件路径
spring:
resources:
static-locations: classpath:/static/
```
3. 在Controller中使用@GetMapping或@RequestMapping注解来映射访问路径,如:
```java
@Controller
public class HomeController {
@GetMapping("/")
public String index() {
return "index";
}
}
```
这里的index即为static文件夹下的index.html文件。
通过上述步骤,就可以在SpringBoot中设置静态资源文件并访问了。
阅读全文
相关推荐
















