After using Simplemappingexceptionresolver for uniform exception handling (refer to Spring MVC's exception-unification approach), when an exception is found, log4j cannot output the error log at the console. Therefore, you need to customize a Rrtongmappingexceptionresolver class that inherits to Simplemappingexceptionresolver, which is passed in Rrtongmappingexceptionresolver Log.error (Ex.getmessage ()) to output the log to the console. The following is the implementation of the specific configuration and rrtongmappingexceptionresolver.
Configure a custom unified exception handling class Rrtongmappingexceptionresolver
<bean name= "Exceptionresolver" class= "Com.rrtong.frame.exception.RrtongMappingExceptionResolver" > <!--definition The exception handling page is used to get the variable name of the exception information, the default name is exception---<property name= "Exceptionattribute" value= "ex" ></property> <!--define exceptions that require special handling, with the class name or full pathname as key, and the page name 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= "Errors/error" >500</prop> <prop key= "E rrors/404 ">404</prop> </props> </property> <!--set the log output level, do not define the default output alarm Error log information--><property name= "WarnloGcategory "value=" DEBUG "/><!--default HTTP status code--><property name=" Defaultstatuscode "value=" Bean>
Implement unified exception Handling class Rrtongmappingexceptionresolver
/** * @className: Rrtongmappingexceptionresolver * @description: Custom unified exception handling inherited to Simplemappingexceptionresolver * @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);/* error log output to console */log.error (Ex.getmessage ()); return Modelandview;}}
Spring MVC Custom Unified exception handling class and output error log in console