在Springboot中,可以通过修改配置、或者在static文件夹下添加error文件夹引入个性化的404模版。但是如果需要针对不同url地址规则,返回不同样式的404页面,则难以实现了。针对这个问题,可以参考如下内容。

例如有两种类型的url:
/admin开头的是后台管理,其他url为常规访问,不考虑安全性的情况下,想返回两种样式的404页面。

Springboot中的错误页面均是由BasicErrorController控制,继承BasicErrorController,重写其中方法即可实现自定义错误页面。
package com.haramasu.daomin2.error;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Map; /**
* @Auther: DingShuo
* @Date: 2018/7/23 12:25
* @Description:
*/
@Controller
@RequestMapping(value = "/error")
public class MyBasicErrorController extends BasicErrorController {
Logger logger =LoggerFactory.getLogger(MyBasicErrorController.class); public MyBasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
super(errorAttributes, errorProperties);
} @Override
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
if(modelAndView!=null){
return modelAndView;
}else {
String path=model.get("path").toString();
logger.debug("该地址无法访问:"+path);
if(path.startsWith("/admin/")){
return new ModelAndView("error/adminError", model);
}else {
return new ModelAndView("error/homeError", model);
} }
} }
55
 
1
package com.haramasu.daomin2.error;
2

3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5
import org.springframework.boot.autoconfigure.web.ErrorProperties;
6
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
7
import org.springframework.boot.web.servlet.error.ErrorAttributes;
8
import org.springframework.http.HttpStatus;
9
import org.springframework.http.MediaType;
10
import org.springframework.stereotype.Controller;
11
import org.springframework.web.bind.annotation.RequestMapping;
12
import org.springframework.web.servlet.ModelAndView;
13

14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
16
import java.util.Collections;
17
import java.util.Map;
18

19
/**
20
 * @Auther: DingShuo
21
 * @Date: 2018/7/23 12:25
22
 * @Description:
23
 */
24
@Controller
25
@RequestMapping(value = "/error")
26
public class MyBasicErrorController extends BasicErrorController {
27
    Logger logger =LoggerFactory.getLogger(MyBasicErrorController.class);
28

29
    public MyBasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
30
        super(errorAttributes, errorProperties);
31
    }
32

33
    @Override
34
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
35
        HttpStatus status = getStatus(request);
36
        Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
37
                request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
38
        response.setStatus(status.value());
39
        ModelAndView modelAndView = resolveErrorView(request, response, status, model);
40
        if(modelAndView!=null){
41
            return modelAndView;
42
        }else {
43
            String path=model.get("path").toString();
44
            logger.debug("该地址无法访问:"+path);
45
            if(path.startsWith("/admin/")){
46
                return new ModelAndView("error/adminError", model);
47
            }else {
48
                return new ModelAndView("error/homeError", model);
49
            }
50

51
        }
52
    }
53
    
54
}
55

需要注意,继承BasicErrorController后的构造函数,会提示errorProperties的bean未被初始化。
可以在@Configration注解的类中初始化bean
package com.haramasu.daomin2.conf;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @Auther: DingShuo
* @Date: 2018/7/23 12:08
* @Description:
*/
@Configuration
public class BeanConfig { @Bean
public ErrorProperties errorProperties(){
return new ErrorProperties();
}
}
1
20
 
1
package com.haramasu.daomin2.conf;
2

3
import org.springframework.boot.autoconfigure.web.ErrorProperties;
4
import org.springframework.context.annotation.Bean;
5
import org.springframework.context.annotation.Configuration;
6

7
/**
8
 * @Auther: DingShuo
9
 * @Date: 2018/7/23 12:08
10
 * @Description:
11
 */
12
@Configuration
13
public class BeanConfig  {
14

15
    @Bean
16
    public ErrorProperties errorProperties(){
17
        return new ErrorProperties();
18
    }
19
}
20


Springboot 自定义多个404页面的更多相关文章

  1. Django如何自定义漂亮的404页面

    目录 在templates 中添加404.html 修改settings.py 在templates 中添加404.html <!DOCTYPE html PUBLIC "-//W3C ...

  2. NGINX下如何自定义404页面

    什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...

  3. SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面

    SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...

  4. springboot自定义错误页面

    springboot自定义错误页面 1.加入配置: @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { re ...

  5. Springboot - 自定义错误页面

    Springboot 没找到页面或内部错误时,会访问默认错误页面.这节我们来自定义错误页面. 自定义错误页面 1.在resources 目录下面再建一个 resources 文件夹,里面建一个 err ...

  6. JavaWeb 自定义404页面

    本来,Tomcat中自定义404页面不过是在web.xml文件中写4行代码的事情. 直接引用 Tomcat官方FAQ 怎样自定义404页面? 编辑web.xml <error-page> ...

  7. springboot使用之四:错误页面404处理建议

    每个项目可能都会遇到404,403,500等错误代码,如没有错误页面,则会给用户一个很不友好的界面,springboot项目同样也存在这个问题. 但在官方文档并没有相关配置信息,这就要求我们自己来实现 ...

  8. 通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面

    最近在学习使用Apache服务器的配置,做一个记录. Apache下有个.htaccess文件,是Apache的一个特殊的配置文件.这个配置文件默认是没有的,要手动在各自的项目的根目录编写才行. 要实 ...

  9. Web---演示Servlet的相关类、下载技术、线程问题、自定义404页面

    Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代表用户的 ...

随机推荐

  1. CEF 框架使用集锦

    CEF 框架使用集锦: 参考:〓https://github.com/NetDimension/NanUI/wiki/%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8NanUI ...

  2. 实现一个vue的图片预览插件

    vue-image-swipe 基于photoswipe实现的vue图片预览组件 安装 1 第一步 npm install vue-image-swipe -D 2 第二步 vue 入口文件引入 im ...

  3. $.inArray()方法

    $.inArray() 函数用于在数组中查找指定值,并返回它的索引值(如果没有找到,则返回-1) 提示:源数组不会受到影响,过滤结果只反映在返回的结果数组中. 语法 $.inArray( value, ...

  4. php框架tp3.2.3和js写的微信分享功能心得,分享的标题内容图片自定义

    https://blog.csdn.net/weixin_42231483/article/details/81585322 最近用PHP的tp3.2.3框架和js写的微信分享功能心得,分享的标题内容 ...

  5. Directx11教程(19) 画一个简单的地形

    原文:Directx11教程(19) 画一个简单的地形       通常我们在xz平面定义一个二维的网格,然后y的值根据一定的函数计算得到,比如正弦.余弦函数的组合等等,可以得到一个看似不错的地形或者 ...

  6. 【To Read】LeetCode | Jump Game II(转载)

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  7. 自己动手打造基于 WKWebView 的混合开发框架(一)WKWebView 上手

    http://www.cocoachina.com/ios/20150911/13301.html 代码示例:https://github.com/johnlui/Swift-On-iOS/tree/ ...

  8. iOS 多个精致动画

    iOS 多个精致动画  http://www.cocoachina.com/bbs/read.php?tid=301262 iOS 零碎小知识     http://www.cocoachina.co ...

  9. VirtualBox使用随笔

    1.virtualbox配置Android手机USB热点 host:Windows10;guest:windows XP/10 右击我的电脑 - 管理 - 设备管理器 - 网卡适配器,若手机正确vb连 ...

  10. 初始化Redis密码

    在配置文件/etc/redis/redis.conf中有个参数: requirepass 这个就是配置redis访问密码的参数: 比如 requirepass test123: (需重启Redis才能 ...