zoukankan      html  css  js  c++  java
  • springboot 全局异常拦截器,友好异常提示

    1. 添加config 配置类

    package org.fh.config;
    
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
    
    /**
     * 说明:错误异常拦截处理
     * 作者:FH Admin
     * from fhadmin.cn
     */
    @Configuration
    public class ExceptionConfiguration implements HandlerExceptionResolver {
    
        @Override
        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
                Exception ex) {
            ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());    //返回json
            
            String exInfo = ex.toString().replaceAll("\n", "<br/>");
            
            boolean status = exInfo.contains("Subject does not have permission");
            
            if(status){
                exInfo = "[没有此页面的访问权限]" + exInfo;
            }else {
                System.out.println("==============异常开始=============");
                ex.printStackTrace();
                System.out.println("==============异常结束=============");
            }
            mv.addObject("exception", exInfo);
            mv.addObject("result", "exception");
            
            return mv;
        }
        
    }

    2.  在逻辑类的方法上抛出异常 throws Exception,比如  

        /**删除
         * @param out
         * @throws Exception
         */
        @RequestMapping(value="/delete")
        @RequiresPermissions("autograph:del")
        @ResponseBody
        public Object delete() throws Exception{
            Map<String,String> map = new HashMap<String,String>();
            String errInfo = "success";
            //xxxx
            map.put("result", errInfo);                //返回结果
            return map;
        }

    3. 前端页面接收异常结果

                //发送 post 请求提交保存
                $.ajax({
                        xhrFields: {
                            withCredentials: true
                        },
                        type: "POST",
                        url: httpurl+'xxxx/delete',
                        data: {tm:new Date().getTime()},
                        dataType:"json",
                        success: function(data){
                            if("success" == data.result){
                                
                            }else if ("exception" == data.result){
                                alert("模块异常"+data.exception);//显示异常
                                
                            }
                        }
                    });          

     自定义表单 www.fhadmin.cn
    28. 定义模版:拖拽左侧表单元素到右侧区域,编辑表单元素,保存表单模版
    29. 表单模版:编辑维护表单模版,复制表单模版,修改模版类型,预览表单模版
    30. 我的表单:选择表单模版,编辑表单规则,是否上传图片、附件、开启富文本、挂靠流程开关等
    31. 表单数据:从我的表单进去可增删改查表单数据,修改表单规则
    32. 挂靠记录:记录表单数据和流程实例ID关联记录,可删除

  • 相关阅读:
    开放GIS标准OGC之路(4)之 解密Filter(摘抄)
    在WCF中使用async/await 关键字的简化的异步编程模型(译)
    WinCE仿真器设置
    oracle PL/SQL编程详解
    oracle 日期格式
    钢笔工具使用教程
    使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明
    利用bitmap将图片部分颜色透明
    PS圆角图片并保留透明背景
    Script Debugger的使用
  • 原文地址:https://www.cnblogs.com/teacher11/p/15054517.html
Copyright © 2011-2022 走看看