@Controller  
public class AccessController {  
  
    /** 
     * 异常页面控制 
     *  
     * @param runtimeException 
     * @return 
     */  
    @ExceptionHandler(RuntimeException.class)  
    public @ResponseBody  
    Map<String,Object> runtimeExceptionHandler(RuntimeException runtimeException) {  
        logger.error(runtimeException.getLocalizedMessage());  
  
        Map model = new TreeMap();  
        model.put("status", false);  
        return model;  
    }  
  
}  

当这个Controller中任何一个方法发生异常,一定会被这个方法拦截到。然后,输出日志。封装Map并返回,页面上得到status为false。就这么简单。@ExceptionHandler 

或者这个有些有些复杂,来个简单易懂的,上代码: 

@Controller  
public class AccessController {  
    /** 
     * 异常页面控制 
     *  
     * @param runtimeException 
     * @return 
     */  
    @ExceptionHandler(RuntimeException.class)  
    public String runtimeExceptionHandler(RuntimeException runtimeException,  
            ModelMap modelMap) {  
        logger.error(runtimeException.getLocalizedMessage());  
  
        modelMap.put("status", IntegralConstant.FAIL_STATUS);  
        return "exception";  
    }  
}  

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-30
  • 2021-06-10
  • 2021-08-21
  • 2022-01-02
  • 2021-12-29
猜你喜欢
  • 2021-06-28
  • 2021-10-08
  • 2021-09-14
  • 2021-11-14
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案