@ExceptionHandler

@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。就这么简单。 

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

@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";  
    }  
}  

 

posted @ 2016-09-08 11:12  limeOracle  阅读(169)  评论(0编辑  收藏  举报