springmvc by the HandlerExceptionResolver (an interface is, in the spring-webmvc dependent) exception handler, comprising a processor exception, binding data of abnormal abnormality occurs, and executed by a processor. HandlerExceptionResolver only one interface method , as follows
When an exception occurs, SpringMVC calls resolverException () method, and the corresponding ModelAndView to view, as an exception handling report page feedback to the user.
Local Exception Handling
Exception handling local controller can process only specified abnormality, using @ExceptionHandler annotation (in spring-web-dependent) implementation, @ ExceptionHandler can specify multiple exceptions , as follows
@RequestMapping(value = "exlogin.html", method = RequestMethod.GET) public String exLogin(@RequestParam String userCode, @RequestParam String userPassword){ User the User = userService.selectUserByUserCodeAndUserPassword (userCode, the userPassword); IF ( null == User) { the throw new new a RuntimeException ( "user name or password is incorrect!" ); } return "redirect:/user/main.html"; } @ExceptionHandler({RuntimeException.class}) public String handlerException(RuntimeException e, HttpServletRequest request){ request.setAttribute("e", e); return "error"; }
In the exception handling handlerException (), the presentation information into the abnormality HttpServletRequest object
Display page output abnormality information corresponding to $ {e.message}, the output of the abnormality information to customize
Global exception handler
SimpleMappingExceptionResolver global exception handler may be used to achieve. It exception classes mapped to view names, using the corresponding view report exceptions i.e. exception occurs. Then configure the global exception of springmvc-servlet.xml.
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.RuntimeException">error</prop>
</props>
</property>
</bean>
When the controller specifies a RuntimeException occurs using error view displays the abnormality information. Of course, a plurality of abnormality can be customized within the <props> tag.
error.jsp message display page, to be modified to $ {exception.message} to throw an exception information.