博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转载 Spring MVC Exception Handling Example
阅读量:4947 次
发布时间:2019-06-11

本文共 5109 字,大约阅读时间需要 17 分钟。

In J2EE / servlet web application, you can map error page to specify exception like this :

web.xml
404
/WEB-INF/pages/404.jsp
 
com.mkyong.web.exception.CustomException
/WEB-INF/pages/error/custom_error.jsp
 
java.lang.Exception
/WEB-INF/pages/generic_error.jsp

The above code should be self-exploratory. If the exception handling function exists in the servlet container, why we still need to use the Spring to handle the exception?

Generally, there are two reasons :

  1. Customize Error Page – The servlet container will render the error page directly; While the Spring allows you to populate model or data to the error page, so that you can customize a more user friendly error page.
  2. Business logic – Spring allow you to apply extra business logic before rendering the error page, like logging, auditing and etc.

In this tutorial, we will show you two examples to handle the exception in Spring.

  1. For Spring 2.x, we use SimpleMappingExceptionResolver in the XML file.
  2. For Spring 3.x, we can simplify the XML configuration via @ExceptionHandler annotation.

1. SimpleMappingExceptionResolver Example

Review the directory structure.

spring-mvc-exception-example-directory

A custom exception.

CustomGenericException.java
package com.mkyong.web.exception; public class CustomGenericException extends RuntimeException {
  private static final long serialVersionUID = 1L;  private String errCode; private String errMsg;  //getter and setter methods  public CustomGenericException(String errCode, String errMsg) {
this.errCode = errCode; this.errMsg = errMsg; } }

This controller class, just throw a CustomGenericException with custom error code and error description.

CustomerController.java
package com.mkyong.web.controller; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;import com.mkyong.web.exception.CustomGenericException; public class CustomerController extends AbstractController {
  @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
  throw new CustomGenericException("E888", "This is custom message - ABC");  } }

Review the SimpleMappingExceptionResolver below :

mvc-dispatcher-servlet.xml
 
 
 
 
error/generic_error
error/exception_error
 
/WEB-INF/pages/
.jsp
 
 

In above, when

  1. CustomGenericException is thrown, it will map to the view name “error/generic_error”.
  2. Any other Exceptions is thrown, it will map to the view name “error/exception_error”.

In the JSP page, you can access the exception instance via ${exception}.

pages/error/generic_error.jsp.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 	

${exception.errCode} : System Errors

 

System Errors

 

${exception.errMsg}

 

Demo – http://localhost:8080/SpringMvcExample/customer

spring-mvc-exception-handle-1
Download it –   (13KB)
 

2. @ExceptionHandler Example

Since Spring 3.0, there is a new annotation @ExceptionHandler to simplify the XML configuration. Below is the equivalent version using @ExceptionHandler.

CustomerController.java
package com.mkyong.web.controller; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import com.mkyong.web.exception.CustomGenericException; @Controllerpublic class CustomerController {
  @RequestMapping(value = "/customer", method = RequestMethod.GET) public ModelAndView getPages() throws Exception {
  throw new CustomGenericException("E888", "This is custom message X");  }  @ExceptionHandler(CustomGenericException.class) public ModelAndView handleCustomException(CustomGenericException ex) {
  ModelAndView model = new ModelAndView("error/generic_error"); model.addObject("exception", ex); return model;  }  @ExceptionHandler(Exception.class) public ModelAndView handleAllException(Exception ex) {
  ModelAndView model = new ModelAndView("error/exception_error"); return model;  } }

Nothing to declare in Spring XML file.

mvc-dispatcher-servlet.xml
 
 
/WEB-INF/pages/
.jsp
 
 
Download it –   (15KB)
Note
You may interest at this 

转载于:https://www.cnblogs.com/xiari/p/3421029.html

你可能感兴趣的文章
SDOI10 古代猪文题解
查看>>
Codeforces Round #517(Div2) A.Golden Plate
查看>>
JAVA中Date类的使用
查看>>
JS 获取各个偶数之和!!
查看>>
Android 调用堆栈跟踪
查看>>
Windows命令行使用FTP
查看>>
POJ1045 Bode Plot
查看>>
MSMQ(消息队列)
查看>>
文明-墓-太阳墓:太阳墓
查看>>
云:VMware
查看>>
建模:数据建模
查看>>
Shell
查看>>
[loj 2478][luogu P4843]「九省联考 2018」林克卡特树
查看>>
电脑插上耳机没声音
查看>>
pyqt5的使用目录
查看>>
UVA 1395 Slim Span 最小生成树
查看>>
Bug管理工具(TCE)之缺陷流程定制
查看>>
srv.exe蠕虫病毒~
查看>>
hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系...
查看>>
2.Flask jinjia2模板
查看>>