SpringMVC異常處理

SpringMVC異常處理

 一、異常解決方法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,年齡錯誤走ageErrorhtml

  (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)效果展現

  

 三、自定義異常解決方法

  (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)效果展現

  

 

 

 四、局部異常解決方法

  (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)效果展現

    

 

 

 五、全局異常解決方法

  (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)效果展現

    

相關文章
相關標籤/搜索