• SpringMVC06Exception 异常处理


      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>
        <filter>
            <filter-name>CharacterEncoding</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <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>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

      2.实体类UserInfo

    public class UserInfo {
        private String name;
        private Integer age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
    }

      3.1:自定义异常类(用户异常)

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

      3.2:自定义异常类(用户名异常)

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

      3.3:自定义异常类(年龄异常)

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

      4.处理器

    import cn.happy.bean.UserInfo;
    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() { int result = 5 / 0; return "/index.jsp"; }

    //对象异常处理
        @RequestMapping("/multiException")
        public String doFirst(UserInfo info) throws UserInfoException {
            if (!info.getName().equals("admin")) {
                throw new NameException("用户名错误");
            } else if (info.getAge() > 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:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context.xsd 
           http://www.springframework.org/schema/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <context:component-scan base-package="cn.happy"/>
        <!--注解驱动-->
        <mvc:annotation-driven/>
        <!--异常处理器-->
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="defaultErrorView" value="/error.jsp"/>
            <property name="exceptionAttribute" value="ex"/>
            <property name="exceptionMappings">
                <props>
                    <prop key="cn.happy.exceptions.NameException">/NameException.jsp</prop>
                    <prop key="cn.happy.exceptions.AgeException">/AgeException.jsp</prop>
                </props>
            </property>
        </bean>
    </beans>

      6.1:视图页面(登录页面)

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

      6.2:视图页面(异常测试页面)

    <%@ 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.3:用户名异常(测试页)

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

      6.4:年龄异常(测试页)

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>年龄错误</h2>
    </body>
    </html>
      6.5:欢迎页面
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>welcome</h2>
    </body>
    </html>

      7.1:测试页面(正常登录)

       7.2:测试用户名错误

      7.3:测试年龄错误

  • 相关阅读:
    一些点子的梳理
    网络安全解决方案
    流量劫持技术
    运营商DNS系统安全解决方案
    安卓工作室 设置每次启动 选择项目,不直接打开项目
    多臂机测试, AB测试
    vue 入门
    web开发 入门
    C# .NET 使用第三方类库DotNetZip解压/压缩Zip文件
    Linq 101 工具和源码
  • 原文地址:https://www.cnblogs.com/Chenghao-He/p/7787646.html
Copyright © 2020-2023  润新知