首页 > 代码库 > SpringMVC学习笔记五:HandlerExceptionResolver异常处理

SpringMVC学习笔记五:HandlerExceptionResolver异常处理

继承HandlerExceptionResolver自定义异常处理器

控制器ExceptionController.java

package com.orange.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.orange.exception.NameException;
import com.orange.exception.PasswordException;

@Controller
@RequestMapping("/exception")
public class ExceptionController {

    @RequestMapping("handlerException")
    public ModelAndView doException2(String name, String password, ModelAndView mav) throws Exception{
        if(! "admin".equals(name)){
            throw new NameException("用户名错误");
        }
        if(! "admin".equals(password)){
            throw new PasswordException("密码错误");
        }
        mav.addObject("name", name);
        mav.addObject("password", password);
        mav.setViewName("/showException.jsp");
        return mav;
    }
}

定义两个异常类型,NameException.Java和PasswordException.java

package com.orange.exception;

public class NameException extends Exception {

    public NameException() {
        super();
    }

    public NameException(String message) {
        super(message);
    }

    
}
package com.orange.exception;

public class PasswordException extends Exception {

    public PasswordException() {
        super();
    }

    public PasswordException(String message) {
        super(message);
    }

    
}

自定义异常处理器MyException,当发生异常时,指定跳转的视图jsp

package com.orange.exception;

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

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

import com.fasterxml.jackson.core.sym.Name;

public class MyExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("ex", ex);
        mav.setViewName("/defaultException.jsp");
        
        if(ex instanceof NameException){
            mav.setViewName("/showException.jsp");
        }
        
        if(ex instanceof PasswordException){
            mav.setViewName("/showException.jsp");
        }
        
        return mav;
    }

}

注册异常处理器

<!-- 注册异常处理器 -->
    <bean class="com.orange.exception.MyExceptionResolver">

当发生异常时,跳转的jsp页面

exception.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>    
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<base href="<%=basePath %>">
<title>Default Page</title>
</head>
<body>
<dir>
    <h3>SimpleMappingExceptionResolver</h3><br>
    <a href="exception/simple">测试</a>
</dir>
<hr>
<div>
    <h3>HandlerExceptionResolver</h3><br>
    <form action="exception/handlerException">
        name: <input type="text" name="name"><br>
        password: <input type="text" name="password"><br>
        <input type="submit" value="submit">
    </form>
</div>
</body>
</html>

showException.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>    
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<base href="<%=basePath %>">
<title>ShowExceptionPage</title>
</head>
<body>
<div>
    <c:out value="${ex.message }"></c:out>
</div>
<div>
    <c:out value="${name }"></c:out><br>
    <c:out value="${password }"></c:out>
</div>
</body>
</html>

 

SpringMVC学习笔记五:HandlerExceptionResolver异常处理