springboot默认错误页面及静态资源

本文介绍如何在SpringBoot中自定义错误页面,并提供美观的样式。通过在resources/public/error目录下放置不同状态码对应的HTML文件,即可实现个性化错误提示。同时介绍了如何配置自定义资源文件夹。

一、自定义错误页面

想必都看过tomcat或spring的默认定义的错误页面,确实有那么点不好看。那么如何自定义呢?其实springboot已经提供了默认的配置路径。

  1. 在resources目录下新建public/error目录

  2. 建立错误页面
     -resources
      |-public
        |- error
         |-400.html
         |-404.html
         |-500.html

  3. 一般为了样式好看,会引用css以及图片等,只需要在resources目录下新建static目录(springboot默认配置的资源文件夹),将css,js,图片等文件放入,错误页面会自动引用到。

  4. 需要注意的是引用地址的写法应为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和最后一个/别忘记加了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值