关耳er  

SpringMVC异常处理

 1、异常解决方法1

  (1ErrorController

@Controller
@RequestMapping("/error")
public class ErrorController {
    @RequestMapping("/firstRequest")
    public String firstRequest(){
        //模拟异常
        int result=5/0;
        return "index";
    }

}
代码实现

  (2spring-mvc.xml

<!--系统异常处理器-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="defaultErrorView" value="error"></property>
    <property name="exceptionAttribute" value="ex"></property>
</bean>
代码实现

  (3)效果展示

  

 2 、异常解决方法2

  (当空指针错误走error,名称错误走nameError,年龄错误走ageError

  (1login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>登陆页面</title>
</head>
<body>
<form action="/error/secondRequest" method="post">
    姓名:<input type="text" name="userName"/>
    年龄:<input type="text" name="userAge"/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
代码实现

  (2)nameError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>姓名错误</title>
</head>
<body>
    <h1>您输入的信息:${ex.message}</h1>
</body>
</html>
代码实现

  (3)ageError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>年龄错误</title>
</head>
<body>
    <h1>您输入的信息:${ex.message}</h1>
</body>
</html>
代码实现

  (4)ErrorController

@RequestMapping("/secondRequest")
public String secondRequest(String userName,Integer userAge) throws NameException, AgeException {
    if (!userName.equals("admin")){
        throw new NameException("用户名错误!");
    }
    if (userAge>99){
        throw new AgeException("年龄不符合规定!");
    }
    return "success";
}
代码实现

  (5)spring-mvc.xml

<!--系统异常处理器-->
<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.springmvc.exception.NameException">nameError</prop>
            <prop key="com.springmvc.exception.AgeException">ageError</prop>
        </props>
    </property>
</bean>
代码实现

  (6)效果展示

  

 3、自定义异常解决方法

  (1NameException

package com.springmvc.exception;

public class NameException extends Exception{
    public NameException() {
    }

    public NameException(String message) {
        super(message);
    }
}
代码实现

  (2AgeException

package com.springmvc.exception;

public class AgeException extends Exception{
    public AgeException() {
    }

    public AgeException(String message) {
        super(message);
    }
}
代码实现

  (3login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>登陆页面</title>
</head>
<body>
<form action="/error/secondRequest" method="post">
    姓名:<input type="text" name="userName"/>
    年龄:<input type="text" name="userAge"/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
代码实现

  (4nameError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>姓名错误</title>
</head>
<body>
    <h1>您输入的信息:${ex.message}</h1>
</body>
</html>
代码实现

  (5ageError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>年龄错误</title>
</head>
<body>
    <h1>您输入的信息:${ex.message}</h1>
</body>
</html>
代码实现

  (6MyHandlerException

/**
 * 自定义异常处理器
 */
public class MyHandlerException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
       ModelAndView modelAndView=new ModelAndView();
       //如果发生异常,给一个默认异常处理页面
       modelAndView.setViewName("error");
       modelAndView.addObject("ex",ex);

       //如果发生Name异常,则跳转到Name异常页面
        if (ex instanceof NameException){
            modelAndView.setViewName("nameError");
        }
        //如果发生age异常,则跳转到age异常页面
        if (ex instanceof AgeException){
            modelAndView.setViewName("ageError");
        }
        return modelAndView;
    }
}
代码实现

  (7spring-mvc.xml

<!--将自定义异常处理器注册到Spring容器-->
<bean class="com.springmvc.exception.MyHandlerException"/>
代码实现 

  (8)效果展示

  

 

 

 4、局部异常解决方法

  (1NameException

package com.springmvc.exception;

public class NameException extends Exception{
    public NameException() {
    }

    public NameException(String message) {
        super(message);
    }
}
代码实现

  (2AgeException

package com.springmvc.exception;

public class AgeException extends Exception{
    public AgeException() {
    }

    public AgeException(String message) {
        super(message);
    }
}
代码实现

  (3login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>登陆页面</title>
</head>
<body>
<form action="/error/secondRequest" method="post">
    姓名:<input type="text" name="userName"/>
    年龄:<input type="text" name="userAge"/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
代码实现

  (4nameError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>姓名错误</title>
</head>
<body>
    <h1>您输入的信息:${ex.message}</h1>
</body>
</html>
代码实现

  (5ageError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>年龄错误</title>
</head>
<body>
    <h1>您输入的信息:${ex.message}</h1>
</body>
</html>
代码实现

  (6ErrorController

@ExceptionHandler
public ModelAndView exceptionHandler(Exception ex){
    ModelAndView modelAndView=new ModelAndView();
    //如果发生异常,给一个默认异常处理页面
    modelAndView.setViewName("error");
    modelAndView.addObject("ex",ex);

    //如果发生Name异常,则跳转到Name异常页面
    if (ex instanceof NameException){
        modelAndView.setViewName("nameError");
    }
    //如果发生age异常,则跳转到age异常页面
    if (ex instanceof AgeException){
        modelAndView.setViewName("ageError");
    }
    return modelAndView;
}
代码实现

  (7)效果展示

    

 

 

 5、全局异常解决方法

  (1NameException

package com.springmvc.exception;

public class NameException extends Exception{
    public NameException() {
    }

    public NameException(String message) {
        super(message);
    }
}
代码实现

  (2AgeException

package com.springmvc.exception;

public class AgeException extends Exception{
    public AgeException() {
    }

    public AgeException(String message) {
        super(message);
    }
}
代码实现

  (3login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>登陆页面</title>
</head>
<body>
<form action="/error/secondRequest" method="post">
    姓名:<input type="text" name="userName"/>
    年龄:<input type="text" name="userAge"/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
代码实现

  (4nameError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>姓名错误</title>
</head>
<body>
    <h1>您输入的信息:${ex.message}</h1>
</body>
</html>
代码实现

  (5ageError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>年龄错误</title>
</head>
<body>
    <h1>您输入的信息:${ex.message}</h1>
</body>
</html>
代码实现

  (6ExceptionControllerAdvice

/**
 * 全局注解处理
 */
@ControllerAdvice
public class ExceptionControllerAdvice {
    @ExceptionHandler
    public ModelAndView exceptionHandler(Exception ex){
        ModelAndView modelAndView=new ModelAndView();
        //如果发生异常,给一个默认异常处理页面
        modelAndView.setViewName("error");
        modelAndView.addObject("ex",ex);

        //如果发生Name异常,则跳转到Name异常页面
        if (ex instanceof NameException){
            modelAndView.setViewName("nameError");
        }
        //如果发生age异常,则跳转到age异常页面
        if (ex instanceof AgeException){
            modelAndView.setViewName("ageError");
        }
        return modelAndView;
    }
}
代码实现

  (7)效果展示

    

 

 

   

 

posted on 2019-11-11 15:19  关耳er  阅读(155)  评论(0编辑  收藏  举报