在启动springboot加载首页时,出现如下截图的报错:
出现这种报错,是因为静态资源的引入路径不正确,不能写成下面的路径:
<link rel="stylesheet" type="text/css" href="../static/css/bootstrap.min.css">
而应该写成:
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
这是因为springboot对于静态资源的默认访问路径配置决定的,下面是springboot中关于静态资源访问配置的源码:
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
我们可以看到,这些目录都是默认配置的,无需我们在添加。