1. 程式人生 > >SpringMVC(十一)系統異常處理器提升版

SpringMVC(十一)系統異常處理器提升版

pin java emp turn UC res con simple 自定義

現在我想根據用戶名和年齡的輸入格式是否正確讓它調到我自定義不同的頁面

定義一個用戶姓名異常和一個年齡異常

姓名異常

package demo13ExceptionHigh;

/**
 * Created by mycom on 2018/3/30.
 */
public class NameException extends Exception {
    //在這個類中要繼承Exception類,在實現該類中的兩個方法
    public NameException() {
        super();
    }

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

年齡異常

package demo13ExceptionHigh;

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

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

定義一個異常控制器

package demo13ExceptionHigh;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by mycom on 2018/3/30.
*/
@Controller
public class ExceptionController {

@RequestMapping("/first")
public String doFirst(String name,int age) throws Exception {
//根據異常的不同返回不同的頁面
if(!name.equals("admin")){
throw new NameException("用戶名異常");
}
//年齡大於60的就走這個異常
if(age>60){
throw new AgeException("年齡不符合");
}
return "success";
}
}

springmvc.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="demo13ExceptionHigh"></context:component-scan> <!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/error/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--異常處理器--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"></property> <property name="exceptionAttribute" value="ex"></property> <!--異常提升版 添加的東西--> <property name="exceptionMappings"> <props> <!--配置異常類型 key是異常類型 value是要跳轉的頁面名稱--> <prop key="NameException">nameException</prop> <prop key="AgeException">ageException</prop> </props> </property> </bean> <!--註解驅動--> <mvc:annotation-driven/> </beans>

兩個頁面

nameException.jsp

<%--
  Created by IntelliJ IDEA.
  User: mycom
  Date: 2018/3/30
  Time: 12:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
姓名不符合 ${ex.message}
</body>
</html>

ageException.jsp

<%--
  Created by IntelliJ IDEA.
  User: mycom
  Date: 2018/3/30
  Time: 12:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
年齡不符合 ${ex.message}
</body>
</html>

最後進行測試就可以了

SpringMVC(十一)系統異常處理器提升版