首页 > 编程语言 > 详细

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

时间:2017-03-25 23:57:52      阅读:357      评论:0      收藏:0      [点我收藏+]

继承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异常处理

原文:http://www.cnblogs.com/djoker/p/6618825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!