首页>代码>springboot仿百度网盘开发的在线云盘项目>/CJPan/src/main/java/com/whut/pan/config/ExceptionHandler.java
package com.whut.pan.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zc on 2018/10/20.
 */
@ControllerAdvice
public class ExceptionHandler {

    private static final String ERROR_VIEW = "errorPage";

    /**
     * 判断是否是Adjx请求
     *
     * @param httpRequest
     * @return
     */
    private static boolean isAjax(HttpServletRequest httpRequest) {
        return httpRequest.getHeader("X-Requested-With") != null && "XMLHttpRequest".equals(
            httpRequest.getHeader("X-Requested-With"));
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest reqest, HttpServletResponse response, Exception e) {
        if (isAjax(reqest)) {
            return e.getMessage();
        } else {
            ModelAndView mav = new ModelAndView();
            mav.addObject("exception", e);
            mav.addObject("url", reqest.getRequestURL());
            mav.setViewName(ERROR_VIEW);
            return mav;
        }
    }
}
最近下载更多
francochan  LV6 2024年5月4日
kkkllll  LV2 2024年1月26日
shuangfu  LV25 2023年10月19日
molu123456 2023年10月10日
暂无贡献等级
thsgli  LV9 2023年3月14日
MAOMAO999999  LV4 2023年3月4日
xxn1234567  LV1 2023年2月1日
cxz2132132  LV11 2022年11月18日
qlpqlp  LV7 2022年11月14日
huyuxin  LV3 2022年11月9日
最近浏览更多
yimaoermao  LV1 2024年11月28日
Boss绝  LV9 2024年11月12日
charleswang  LV7 2024年10月21日
grant1123  LV2 2024年6月22日
TY0165  LV20 2024年6月21日
17380184110 2024年6月19日
暂无贡献等级
来一杯西瓜冰咩  LV6 2024年5月13日
xiaozhi丶  LV15 2024年3月24日
暂无贡献等级
wyyz5211314  LV4 2024年1月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友