Program Encoding method: Import Javax.servlet.http.httpservletrequest;import Org.apache.logging.log4j.logmanager;import Org.apache.logging.log4j.logger;import Org.springframework.web.bind.annotation.controlleradvice;import Org.springframework.web.bind.annotation.ExceptionHandler, @ControllerAdvicepublic class Exceptioncontrolleradvice {private Logger log = Logmanager.getlogger ("Exceptioncontrolleradvice"); @ExceptionHandler public String Expadvice (httpservletrequest req, Exception ex) {Req.setattribute ("ex", Ex.getme Ssage ()); Log.error (ex); Returns the error prompt page, which can be treated differently depending on the type of exception in the actual development. return "Errors/error"; }}xml configuration mode: <bean id= "Exceptionresolver" class= " Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver "> <property name=" exceptionmappings "> <props> <prop key=" java.lang.Exception ">errors/error</prop> <p ROP key= "java.lang.Throwable" >errors/error</prop> </props> </property> <property name= "Defaulterrorview" value= "Errors/error"/></bean>
Global exception handling of springmvc4.x Learning series