在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. Direct2D 第1篇 最简单的D2D程序

    原文:Direct2D 第1篇 最简单的D2D程序 编译之前,得先安装DirectX SDK #include <windows.h> #include <d2d1.h> #i ...

  2. VisualVM介绍使用

    1    打开VisualVM(这个工具放在JDK安装目录的bin目录下,双击jvisualvm.exe即可打开),如下图所示 以VisualVM自身为例,VisualVM本身也是一个java程序,当 ...

  3. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...

  4. JS实现AES加密并与PHP互通的方法分析

    <script type="text/javascript" src="/CryptoJS/aes.js"></script><s ...

  5. LDAP Authentication Handler

    Including the Handler In the pom.xml file for your CAS Maven2 WAR Overlay, add the following depende ...

  6. 使用php封装APP接口

    php封装APP接口 我们先来介绍Json的封装方法 json_encode函数传递中文的话,输出后是乱码的,针对这个问题我觉得有必要做一个解释: 其实json_encode对中文那不是乱码,只是js ...

  7. 【JZOJ4792】【NOIP2016提高A组模拟9.21】整除

    题目描述 麦克雷有一个1~n的排列,他想知道对于一些区间,有多少对区间内的数(x,y),满足x能被y整除. 输入 第一行包含2个正整数n,m.表示有n个数,m个询问. 接下来一行包含n个正整数,表示麦 ...

  8. elastic search book [ ElasticSearch book es book]

    谁在使用ELK 维基百科, github都使用 ELK (ElasticSearch es book) ElasticSearch入门 Elasticsearch入门,这一篇就够了==>http ...

  9. spring-cloud-zuul跨域问题解决

    问题发现 正常情况下,跨域是这样的: 1. 微服务配置跨域+zuul不配置=有跨域问题 2. 微服务配置+zuul配置=有跨域问题 3. 微服务不配置+zuul不配置=有跨域问题 4. 微服务不配置+ ...

  10. Directx教程(26) 简单的光照模型(5)

    原文:Directx教程(26) 简单的光照模型(5)     在前面的工程中,我们都是在vs中实现顶点光照计算,然后再把顶点颜色传到ps中.本章中我们尝试fragment光照(或者说叫ps光照),在 ...