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

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-04-18 08:41:27


点击(此处)折叠或打开


  1. 1. 添加config 配置类

  2. package org.fh.config;
  3.  
  4.  
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7.  
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.web.servlet.HandlerExceptionResolver;
  10. import org.springframework.web.servlet.ModelAndView;
  11. import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
  12.  
  13. /**
  14.  * 说明:错误异常拦截处理
  15.  * 作者:FH Admin
  16.  * from fhadmin.cn
  17.  */
  18. @Configuration
  19. public class ExceptionConfiguration implements HandlerExceptionResolver {
  20.  
  21.     @Override
  22.     public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
  23.             Exception ex) {
  24.         ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());    //返回json
  25.         
  26.         String exInfo = ex.toString().replaceAll("\n", "
    "
    );
  27.         
  28.         boolean status = exInfo.contains("Subject does not have permission");
  29.         
  30.         if(status){
  31.             exInfo = "[没有此页面的访问权限]" + exInfo;
  32.         }else {
  33.             System.out.println("==============异常开始=============");
  34.             ex.printStackTrace();
  35.             System.out.println("==============异常结束=============");
  36.         }
  37.         mv.addObject("exception", exInfo);
  38.         mv.addObject("result", "exception");
  39.         
  40.         return mv;
  41.     }
  42.     
  43. }

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

  45.     /**删除
  46.      * @param out
  47.      * @throws Exception
  48.      */
  49.     @RequestMapping(value="/delete")
  50.     @RequiresPermissions("autograph:del")
  51.     @ResponseBody
  52.     public Object delete() throws Exception{
  53.         Map<String,String> map = new HashMap<String,String>();
  54.         String errInfo = "success";
  55.         //xxxx
  56.         map.put("result", errInfo);                //返回结果
  57.         return map;
  58.     }

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

  60.             //发送 post 请求提交保存
  61.             $.ajax({
  62.          xhrFields: {
  63.      withCredentials: true
  64.      },
  65.                     type: "POST",
  66.                     url: httpurl+'xxxx/delete',
  67.                  data: {tm:new Date().getTime()},
  68.                     dataType:"json",
  69.                     success: function(data){
  70.                         if("success" == data.result){
  71.                             
  72.                         }else if ("exception" == data.result){
  73.                             alert("模块异常"+data.exception);//显示异常
  74.                             
  75.                         }
  76.                     }
  77.                 });


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