1. 程式人生 > >SpringMVC07SelfException 自定義異常

SpringMVC07SelfException 自定義異常

ive sch n) content form utf del beans dispatch

  1.配置web.xml文件

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class
>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

  2.1:用戶異常類

public class UserInfoException extends Exception {
    public UserInfoException() {
    }
    public UserInfoException(String message) {
        super(message);
    }
}

  2.2:用戶名異常

public class NameException extends UserInfoException {
    public NameException() {
    }
    public NameException(String message) {
        
super(message); } }

  2.3:年齡異常

public class AgeException extends UserInfoException {
    public AgeException() {
    }
    public AgeException(String message) {
        super(message);
    }
}

  3.異常解析器

import cn.happy.exceptions.AgeException;
import cn.happy.exceptions.NameException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//處理程序異常解析器
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
    //解析異常
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/error.jsp");           //默認異常頁面
        if (ex instanceof NameException) {
            mv.setViewName("/error/NameException.jsp");
        } else if (ex instanceof AgeException) {
            mv.setViewName("/error/AgeException.jsp");
        }
        return mv;
    }
}

  4.處理器

import cn.happy.exceptions.AgeException;
import cn.happy.exceptions.NameException;
import cn.happy.exceptions.UserInfoException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FirstController {
    @RequestMapping("/first")
    public String doFirst(String name, int age) throws UserInfoException {
        if (!"admin".equals(name)) {
            throw new NameException("用戶名錯誤");
        } else if (age > 60) {
            throw new AgeException("年齡錯誤");
        }
        return "/index.jsp";
    }
}

  5.映射器

<?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.happy"/> <!--註解驅動--> <mvc:annotation-driven/> <!--自定義異常處理器處理--> <bean class="cn.happy.hanlderexception.MyHandlerExceptionResolver"/> </beans>

  6.1:視圖NameException.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>用戶名錯誤</h2>
</body>
</html>

  6.2:視圖AgeException.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>年齡錯誤</h2>
</body>
</html>

  6.3:視圖login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/first" method="post">
    用戶名:<input name="name"/>
    年齡:<input name="age"/>
    <input type="submit" value="登錄"/>
</form>
</body>
</html>

  6.4:視圖error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>ErrorPage出錯了</h2>
${ex.localizedMessage}
</body>
</html>

  6.5:視圖index.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

SpringMVC07SelfException 自定義異常