一、自定义错误页面
想必都看过tomcat或spring的默认定义的错误页面,确实有那么点不好看。那么如何自定义呢?其实springboot已经提供了默认的配置路径。
在resources目录下新建public/error目录
建立错误页面
-resources
|-public
|- error
|-400.html
|-404.html
|-500.html一般为了样式好看,会引用css以及图片等,只需要在resources目录下新建static目录(springboot默认配置的资源文件夹),将css,js,图片等文件放入,错误页面会自动引用到。
- 需要注意的是引用地址的写法应为
href="/css/main.css"
。如果写为href="css/main.css"
,那么一旦错误url超过两层,样式就引用不到了。
例如:
访问错误的url: http://localhost:8080/user/asd,那么错误页面的css引用地址就会为:http://localhost:8080/user/asd/css/main.css,就不能正确的显示了。
二、 自定义资源文件夹
appliation.properties
# 默认值为 /**
spring.mvc.static-path-pattern=/resources/**
# 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
spring.resources.static-locations=classpath:/king/static/
上面配置的效果:
访问localhost:8080/resources/hello.json,会去寻找/king/static/hello.json文件。需要注意的是,classpath:/king/static/中的classpath和最后一个/别忘记加了。