zoukankan      html  css  js  c++  java
  • SpringMVC(十一)系统异常处理器提升版

    现在我想根据用户名和年龄的输入格式是否正确让它调到我自定义不同的页面

    定义一个用户姓名异常和一个年龄异常

    姓名异常

    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>

    最后进行测试就可以了

  • 相关阅读:
    intellij idea 修改web端口号
    intellij idea有时候有时候服务器报错500
    由于没有更新主分支的代码,总是报警
    intellij idea 快捷键
    intellij idea 修改文件名失败
    [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟
    [Introduction to programming in Java 笔记] 1.3.7 Converting to binary 十进制到二进制的转换
    C++学习笔记-2-构造函数和析构函数
    python学习笔记--随时更新
    C++学习笔记-1-自增和自减运算符
  • 原文地址:https://www.cnblogs.com/my-123/p/8681088.html
Copyright © 2011-2022 走看看