Springboot 自定义多个404页面
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);
}
}
}
}
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);
}
}
}
}
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();
}
}
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();
}
}
Springboot 自定义多个404页面的更多相关文章
- Django如何自定义漂亮的404页面
目录 在templates 中添加404.html 修改settings.py 在templates 中添加404.html <!DOCTYPE html PUBLIC "-//W3C ...
- NGINX下如何自定义404页面
什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...
- SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面
SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...
- springboot自定义错误页面
springboot自定义错误页面 1.加入配置: @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { re ...
- Springboot - 自定义错误页面
Springboot 没找到页面或内部错误时,会访问默认错误页面.这节我们来自定义错误页面. 自定义错误页面 1.在resources 目录下面再建一个 resources 文件夹,里面建一个 err ...
- JavaWeb 自定义404页面
本来,Tomcat中自定义404页面不过是在web.xml文件中写4行代码的事情. 直接引用 Tomcat官方FAQ 怎样自定义404页面? 编辑web.xml <error-page> ...
- springboot使用之四:错误页面404处理建议
每个项目可能都会遇到404,403,500等错误代码,如没有错误页面,则会给用户一个很不友好的界面,springboot项目同样也存在这个问题. 但在官方文档并没有相关配置信息,这就要求我们自己来实现 ...
- 通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面
最近在学习使用Apache服务器的配置,做一个记录. Apache下有个.htaccess文件,是Apache的一个特殊的配置文件.这个配置文件默认是没有的,要手动在各自的项目的根目录编写才行. 要实 ...
- Web---演示Servlet的相关类、下载技术、线程问题、自定义404页面
Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代表用户的 ...
随机推荐
- CEF 框架使用集锦
CEF 框架使用集锦: 参考:〓https://github.com/NetDimension/NanUI/wiki/%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8NanUI ...
- 实现一个vue的图片预览插件
vue-image-swipe 基于photoswipe实现的vue图片预览组件 安装 1 第一步 npm install vue-image-swipe -D 2 第二步 vue 入口文件引入 im ...
- $.inArray()方法
$.inArray() 函数用于在数组中查找指定值,并返回它的索引值(如果没有找到,则返回-1) 提示:源数组不会受到影响,过滤结果只反映在返回的结果数组中. 语法 $.inArray( value, ...
- php框架tp3.2.3和js写的微信分享功能心得,分享的标题内容图片自定义
https://blog.csdn.net/weixin_42231483/article/details/81585322 最近用PHP的tp3.2.3框架和js写的微信分享功能心得,分享的标题内容 ...
- Directx11教程(19) 画一个简单的地形
原文:Directx11教程(19) 画一个简单的地形 通常我们在xz平面定义一个二维的网格,然后y的值根据一定的函数计算得到,比如正弦.余弦函数的组合等等,可以得到一个看似不错的地形或者 ...
- 【To Read】LeetCode | Jump Game II(转载)
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 自己动手打造基于 WKWebView 的混合开发框架(一)WKWebView 上手
http://www.cocoachina.com/ios/20150911/13301.html 代码示例:https://github.com/johnlui/Swift-On-iOS/tree/ ...
- iOS 多个精致动画
iOS 多个精致动画 http://www.cocoachina.com/bbs/read.php?tid=301262 iOS 零碎小知识 http://www.cocoachina.co ...
- VirtualBox使用随笔
1.virtualbox配置Android手机USB热点 host:Windows10;guest:windows XP/10 右击我的电脑 - 管理 - 设备管理器 - 网卡适配器,若手机正确vb连 ...
- 初始化Redis密码
在配置文件/etc/redis/redis.conf中有个参数: requirepass 这个就是配置redis访问密码的参数: 比如 requirepass test123: (需重启Redis才能 ...