Spring MVC custom unified exception handling class, and output error logs on the console, mvc Exception Handling
After SimpleMappingExceptionResolver is used for unified Exception Handling (refer to Spring MVC's unified exception handling method), log4j cannot output error logs in the console when exceptions occur. Therefore, you need to customize a RrtongMappingExceptionResolver class that inherits to SimpleMappingExceptionResolver. In RrtongMappingExceptionResolver, log. error (ex. getMessage () is used to output logs to the console. The specific configuration and implementation of RrtongMappingExceptionResolver are as follows.
Configure custom unified exception handling class RrtongMappingExceptionResolver
<Bean name = "exceptionResolver" class = "com. rrtong. frame. exception. RrtongMappingExceptionResolver"> <! -- Defines the variable name used to obtain the exception information on the exception handling page. The default name is exception --> <property name = "exceptionAttribute" value = "ex"> </property> <! -- Define exceptions that require special processing, and use the class name or full path name as the key, the page name is also used as the value --> <property name = "exceptionMappings"> <props> <prop key = "com. rrtong. frame. exception. guideTestException "> .. /.. /exception/error-interface </prop> <! -- <Prop key = "com. rrtong. frame. exception. notLoginException "> login </prop> --> <prop key =" java. lang. exception "> .. /.. /exception/errorPage </prop> </props> </property> <property name = "statusCodes"> <props> <prop key = "errorrs/error"> 500 </ prop> <prop key = "errors/404"> 404 </prop> </props> </property> <! -- Set the log output level. If this parameter is not set, error logs such as warnings are not output by default. --> <property name = "warnLogCategory" value = "DEBUG"/> <! -- Default HTTP status code --> <property name = "defaultStatusCode" value = "500"/> </bean>
RrtongMappingExceptionResolver
/*** @ ClassName: RrtongMappingExceptionResolver * @ description: inherits from SimpleMappingExceptionResolver's custom unified Exception Handling * @ author: administrator * @ date January 12, 2016 */public class RrtongMappingExceptionResolver extends SimpleMappingExceptionResolver {private final static Logger log = LoggerFactory. getLogger (RrtongMappingExceptionResolver. class); @ Overrideprotected ModelAndView doResolveException (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {Map <String, Exception> model = new HashMap <String, Exception> (); model. put ("ex", ex); ModelAndView modelAndView = new ModelAndView (".. /.. /exception/errorPage ", model);/* the error log is output to the console */log. error (ex. getMessage (); return modelAndView ;}}