Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604989
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-07-24 09:41:42

1. 添加config 配置类

点击(此处)折叠或打开

  1. package org.fh.config;


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

  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.HandlerExceptionResolver;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

  8. /**
  9.  * 说明:错误异常拦截处理
  10.  * 作者:FH Admin
  11.  * from fhadmin.cn
  12.  */
  13. @Configuration
  14. public class ExceptionConfiguration implements HandlerExceptionResolver {

  15.     @Override
  16.     public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
  17.             Exception ex) {
  18.         ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());    //返回json
  19.         
  20.         String exInfo = ex.toString().replaceAll("\n", "
    "
    );
  21.         
  22.         boolean status = exInfo.contains("Subject does not have permission");
  23.         
  24.         if(status){
  25.             exInfo = "[没有此页面的访问权限]" + exInfo;
  26.         }else {
  27.             System.out.println("==============异常开始=============");
  28.             ex.printStackTrace();
  29.             System.out.println("==============异常结束=============");
  30.         }
  31.         mv.addObject("exception", exInfo);
  32.         mv.addObject("result", "exception");
  33.         
  34.         return mv;
  35.     }
  36.     
  37. }

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

点击(此处)折叠或打开

  1. /**删除
  2.      * @param out
  3.      * @throws Exception
  4.      */
  5.     @RequestMapping(value="/delete")
  6.     @RequiresPermissions("autograph:del")
  7.     @ResponseBody
  8.     public Object delete() throws Exception{
  9.         Map<String,String> map = new HashMap<String,String>();
  10.         String errInfo = "success";
  11.         //xxxx
  12.         map.put("result", errInfo);                //返回结果
  13.         return map;
  14.     }

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

点击(此处)折叠或打开

  1. //发送 post 请求提交保存
  2.             $.ajax({
  3.          xhrFields: {
  4.      withCredentials: true
  5.      },
  6.                     type: "POST",
  7.                     url: httpurl+'xxxx/delete',
  8.                  data: {tm:new Date().getTime()},
  9.                     dataType:"json",
  10.                     success: function(data){
  11.                         if("success" == data.result){
  12.                             
  13.                         }else if ("exception" == data.result){
  14.                             alert("模块异常"+data.exception);//显示异常
  15.                             
  16.                         }
  17.                     }
  18.                 })


阅读(1063) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~