zoukankan      html  css  js  c++  java
  • SpringMVC08AnnotationException 注解异常

      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>
            <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) {
            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.exceptions.AgeException;
    import cn.happy.exceptions.NameException;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.servlet.ModelAndView;
    public class BaseController {
        //解析异常
        @ExceptionHandler
        public ModelAndView resolveException(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;
        }
    }

      5.处理器

    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.ExceptionHandler;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    @Controller
    public class FirstController extends BaseController {
        @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";
        }
    }

      6.映射器

    <?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"
           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">
    
        <context:component-scan base-package="cn.happy"/>
    </beans>

      7.1:视图(年龄异常页面)

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

      7.2.视图(用户名异常页面)

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

      7.3视图(登录页面)

    <%@ 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>

      7.4:视图(异常页面)

    <%@ 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>

      7.5:视图(登录成功页面)

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

      

      测试页面:测试登录成功

      测试页面:测试用户名异常

      测试页面:测试年龄异常

  • 相关阅读:
    angular转场动画
    css常用单位
    css3美化滚动条样式
    grid布局
    angular使用material组件库和tailwindcss样式
    使用 BaGet 搭建 nuget 仓库
    net core api 文件下载,断点续传
    netcore 使用中间件响应文件下载请求
    angular通知组件--angular2-toaster
    angular图标组件--@visurel/iconify-angular
  • 原文地址:https://www.cnblogs.com/Chenghao-He/p/7788701.html
Copyright © 2011-2022 走看看