SpringMVC (five): springMVC Exception Handling

After the browser to access the server, the server processes the request an exception, if thrown directly displayed on the browser page, the customer experience is not very good, so we can be when an exception occurs, custom exception page so the effect of improving the user experience. In springmvc in and give us three to handle exceptions.

1, the configuration is simple manner exception handler

1.1, write a simple exception page error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>异常页面</title>
</head>
<body style="font-size:30px;">
	系统繁忙,稍后再试!
</body>
</html>

1.2, exception handling configuration

	<!--配置简单异常处理器  -->
	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">error</prop>
			</props>
		</property>
	</bean>

2, exception processing method of adding the way

2.1, page write exception handling error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>异常页面</title>
</head>
<body style="font-size:30px;">
	系统繁忙,稍后再试!
</body>
</html>

2.2, write exception handling

	/**
	 * 异常处理方法,e:是其他方法所抛出的异常
	 * @description TODO
	 * @param e
	 * @param request
	 * @return
	 */
	@ExceptionHandler
	public String handleEx(Exception e,HttpServletRequest request) {
		return "error/error";
	}

3, custom exception processing method

3.1, custom exception class

package com.wedu.springmvc.exception;

/**
 * 自定义异常类
 */
public class SysException extends Exception{

    // 存储提示信息的
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public SysException(String message) {
        this.message = message;
    }

}

3.2, custom exception handler

package com.wedu.springmvc.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 异常处理器
 */
public class SysExceptionResolver implements HandlerExceptionResolver{

    /**
     * 处理异常业务逻辑
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @return
     */
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        // 获取到异常对象
        SysException e = null;
        if(ex instanceof SysException){
            e = (SysException)ex;
        }else{
            e = new SysException("系统正在维护....");
        }
        // 创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        mv.addObject("errorMsg",e.getMessage());
        mv.setViewName("error");
        return mv;
    }

}

3.3, the exception handler configuration

    <!-- 配置异常处理器 -->
    <bean id="sysExceptionResolver" class="com.wedu.springmvc.exception.SysExceptionResolver"/>

 

Published 134 original articles · won praise 10 · views 7341

Guess you like

Origin blog.csdn.net/yu1755128147/article/details/103906586