处理局部异常

  控制器:

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

    @ExceptionHandler
    public ModelAndView exceptionHandler(Exception ex){
        ModelAndView mv=new ModelAndView();
        //保存异常变量
        mv.addObject("ex",ex);
        //添加错误页面
        mv.setViewName("error");
        System.out.println("出现了异常!");
        return mv;
    }
View Code
@RequestMapping("/hello")       //设置处理器方法与用户请求的url之间的映射关系@WebServlet
public String toIndex() throws Exception {
    if (1==1){
        throw new Exception("我是异常哈哈哈哈");
    }
    return "index";
}
View Code

页面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--<c:forEach items="${error}" var="item">
    <span>${item.defaultMessage}</span>
</c:forEach>--%>
${error.defaultMessage}

</body>
</html>
View Code

  结果

 

处理全局异常

  ExceptionHandler类:

/*全局异常处理*/
@ControllerAdvice
public class ExceptionHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler
    public ModelAndView except(Exception e){
        String message = e.getMessage();    //获取异常信息
        ModelAndView mv=new ModelAndView("404");
        mv.addObject("message",message);
        return mv;
    }
}
View Code

  控制器:

@RequestMapping("/hello")       //设置处理器方法与用户请求的url之间的映射关系@WebServlet
public String toIndex() throws Exception {
    if (1==1){
        throw new Exception("我是异常哈哈哈哈");
    }
    return "index";
}
View Code

自定义异常

  spring-mvc文件配置:

<!--异常处理-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="defaultErrorView" value="error"></property>
    <property name="exceptionAttribute" value="ex"></property>
</bean>
View Code

  控制器:

@RequestMapping("/getex")
@ResponseBody
public String getex(){
    int num=5/0;
    return "index";
}
View Code

  结果:

自定义异常类

  spring-mvc文件配置:

<!--异常处理-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="defaultErrorView" value="error"></property>
    <property name="exceptionAttribute" value="ex"></property>
    <property name="exceptionMappings">
        <props>
            <prop key="com.cmy.exception.UserName">UserName</prop>
            <prop key="com.cmy.exception.Password">Password</prop>
        </props>
    </property>
</bean>
View Code

  控制器:

@RequestMapping("/getex01")
public String getex01(String userName,String password) throws Exception {
    if (!userName.equals("admin")){
        throw new Exception("用戶民不正確!!!");
    }
    if (!password.equals("admin")){
        throw  new Exception("密碼不正確!!!");
    }
    return "index";
}
View Code

  UserName.class异常类:

package com.cmy.exception;

public class UserName extends Exception {

    public UserName() {
    }

    public UserName(String message) {
        super(message);
    }
}
View Code

  Password.class异常类:

package com.cmy.exception;

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

    public Password() {
    }

    public Password(String message) {
        super(message);
    }
}
View Code

  页面:

  结果:

 

 

 

自定义异常类实现(HandelExceptionResolver接口)

  spring-mvc.xml文件配置:

<bean class="com.cmy.exception.HandelException">
View Code

  HandelException.class异常类:

public class HandelException implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView mv=new ModelAndView();
        mv.addObject("ex",e);
        mv.setViewName("error");
        if (e instanceof  UserName){
            mv.setViewName("UserName");
        }
        if (e instanceof  Password){
            mv.setViewName("Password");
        }

        return mv;
    }
}
View Code

  控制器:

@RequestMapping("/getex01")
public String getex01(String userName,String password) throws Exception {
    if (!userName.equals("admin")){
        throw new Exception("用戶民不正確!!!");
    }
    if (!password.equals("admin")){
        throw  new Exception("密碼不正確!!!");
    }
    return "index";
}
View Code
 posted on 2019-11-11 13:31  wnwn  阅读(244)  评论(0编辑  收藏  举报