1. 程式人生 > >SSM-SpringMVC-25:SpringMVC異常頂級之自定義異常解析器

SSM-SpringMVC-25:SpringMVC異常頂級之自定義異常解析器

post 程序猿 org instance value username 掃描 位置 服務器

------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------

上篇博客相信大家也看到了,自定義異常,用了SimpleMappingExceptionResolver這個解析器,本次要講的是自定義異常解析器:

自己定義的異常解析器,實現了HandlerExceptionResolver,一會再在xml中配置一道,我把需要改或者新建的三處放在前面,下面的與上篇博客的一樣(不過我也放上來),看著來把,活學活用

案例

  1,自定義異常解析器MyHandlerExceptionResolver:

package cn.dawn.day17selfexceptionresolver.resolver;

import cn.dawn.day17selfexceptionresolver.userexception.UserageException;
import cn.dawn.day17selfexceptionresolver.userexception.UsernameException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

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

/** * Created by Dawn on 2018/3/30. */ public class MyHandlerExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView modelAndView=new ModelAndView();
/*返回的異常對象*/ modelAndView.addObject("ex",ex); /*判斷去那個頁面*/ if(ex instanceof UsernameException){ modelAndView.setViewName("name"); } if(ex instanceof UserageException){ modelAndView.setViewName("age"); } return modelAndView; } }

  2.自己的xml大配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

    <!--包掃描器-->
    <context:component-scan base-package="cn.dawn.day17selfexceptionresolver"></context:component-scan>
    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/day17/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--自己的異常解析器-->
    <bean class="cn.dawn.day17selfexceptionresolver.resolver.MyHandlerExceptionResolver"></bean>

</beans>

  3.講web.xml中央調度器的上下文配置位置改為你現在這個配置xml

  下面的上篇博客就有,不過我也再放一份,ok

  4.UserageException自定義異常

package cn.dawn.day17selfexceptionresolver.userexception;

/**
 * Created by Dawn on 2018/3/30.
 */
public class UserageException extends Exception {
    public UserageException() {
        super();
    }

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

  5.UsernameException自定義異常

package cn.dawn.day17selfexceptionresolver.userexception;

/**
 * Created by Dawn on 2018/3/30.
 */
public class UsernameException extends Exception {
    public UsernameException() {
        super();
    }

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

  6.自定義的處理器和處理方法

package cn.dawn.day17selfexceptionresolver;

import cn.dawn.day17selfexceptionresolver.userexception.UserageException;
import cn.dawn.day17selfexceptionresolver.userexception.UsernameException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Dawn on 2018/3/28.
 */
@Controller
public class ZDYExceptionController {
    @RequestMapping("/zidingyiException")
    public String zidingyiException(String username,Integer userage) throws Exception {
        if(!username.equals("admin")){
            throw new UsernameException("登陸名不正確");
        }
        if(userage<18){
            throw new UserageException("未成年,走開");
        }
        return "success";
    }
}

  7.jsp頁面

    7.1login.jsp

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>登錄</h2>
<form action="${pageContext.request.contextPath}/zidingyiException" method="post">

    用戶名:<input name="username">
    年齡:<input name="userage">

    <input type="submit" value="登錄"/>
</form>
</body>
</html>

    7.2success.jsp

<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
<html>
<body>
<%--<img src="image/1.jpg">--%>
<h2>Success!</h2>
</body>
</html>

    7.3error.jsp

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>ERROR</h2>
<p>${ex.message}</p>
服務器被猴子砍了,攻城獅在搶修中,還殺了個程序猿祭天
</body>
</html>

    7.4age.jsp

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>年齡錯誤ERROR</h2>
<p>${ex.message}</p>
服務器被猴子砍了,攻城獅在搶修中,還殺了個程序猿祭天
</body>
</html>

    7.5name.jsp

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>名字錯誤ERROR</h2>
<p>${ex.message}</p>
服務器被猴子砍了,攻城獅在搶修中,還殺了個程序猿祭天
</body>
</html>

  8.啟動tomcat,訪問login.jsp

SSM-SpringMVC-25:SpringMVC異常頂級之自定義異常解析器