SpringMvc annotation-based exception handling

SpringMvc's annotation-based exception handling does not need to configure anything. As long as the annotation scanner is started, its priority is higher than the global exception handling. After this processing, the global exception handling will not be processed.

 

 

1. Define a parent class abstract Controller, which writes an annotated exception handling method, which is equivalent to a public exception capture handler

 

2. Other controllers can inherit, throw the exception specified by the parent class in the corresponding controller method, and then handle the exception catcher of the parent class

 

http://blog.csdn.net/chonggaoing/article/details/42803299

 

 

@Controller

public class OpmRoleController extends BaseController {

 

 

@RequestMapping("/system/opmRole/opmdel")

public ModelAndView opmdel(@RequestParam(value = "ids", required = false) String ids,OpmUserVo opmUser,HttpServletRequest request,Model modle) throws Exception {

throw new ServiceException("Deletion failed");

}

}

 

 

public abstract class BaseController {

@Autowired

private HttpServletRequest request;

 

@Autowired

private ResourceBundleMessageSource _res;

 

 

@ExceptionHandler(Exception.class)

public ModelAndView exception(Exception e, HttpServletRequest request) {

e.printStackTrace ();

 

request.setAttribute("exception", e);

 

if (ServerInfo.isAjax(request) || request.getParameter("ajax") != null) {

}

 

ModelAndView mav = new ModelAndView("404");

mav.addObject("statusCode", 300);

mav.addObject("message", e.getMessage());

mav.addObject("callbackType", "closeCurrent");

//mav.addObject("navTabId", navtab); The following is useless when this is omitted

//mav.addObject("forwardUrl", "404");

 

return mav;

}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326520705&siteId=291194637