zoukankan      html  css  js  c++  java
  • SSM-SpringMVC-26:SpringMVC异常骇级之自定义异常注解版

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

    注解的方法实现异常解析,话不多说,直接搞起,和以前一样的习惯,和上篇博客一样的代码放后面,不一样的在前面

    案例:

      1.自定义处理器和处理方法

    package cn.dawn.day18annotationexception;
    
    import cn.dawn.day18annotationexception.userexception.UserageException;
    import cn.dawn.day18annotationexception.userexception.UsernameException;
    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;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Created by Dawn on 2018/3/28.
     */
    @Controller
    public class ZDYExceptionController {
    
    
        /*作用于这俩个*/
        @ExceptionHandler({UsernameException.class,UserageException.class})
        public ModelAndView resolveException(Exception ex) {
            ModelAndView modelAndView=new ModelAndView();
            /*返回的异常对象*/
            modelAndView.addObject("ex",ex);
            /*判断去那个页面*/
            if(ex instanceof UsernameException){
                modelAndView.setViewName("name");
            }
            if(ex instanceof UserageException){
                modelAndView.setViewName("age");
            }
    
            return modelAndView;
        }
    
    
    
    
        @RequestMapping("/annotationException")
        public String annotationException(String username,Integer userage) throws Exception {
            if(!username.equals("admin")){
                throw new UsernameException("登陆名不正确");
            }
            if(userage<18){
                throw new UserageException("未成年,走开");
            }
            return "success";
        }
    }

      2.自己的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="cn.dawn.day18annotationexception"></context:component-scan>
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/day18/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
    
    </beans>

      3.修改web.xml的中央调度器的上下文配置路径

      4.login.jsp

    <%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>登录</h2>
    <form action="${pageContext.request.contextPath}/annotationException" method="post">
    
        用户名:<input name="username">
        年龄:<input name="userage">
    
        <input type="submit" value="登录"/>
    </form>
    </body>
    </html>

    下面的内容与上篇博客相同

      

     5.UserageException自定义异常

    复制代码
    package cn.dawn.day17selfexceptionresolver.userexception;
    
    /**
     * Created by Dawn on 2018/3/30.
     */
    public class UserageException extends Exception {
        public UserageException() {
            super();
        }
    
        public UserageException(String message) {
            super(message);
        }
    }
    复制代码

      6.UsernameException自定义异常

    复制代码
    package cn.dawn.day17selfexceptionresolver.userexception;
    
    /**
     * Created by Dawn on 2018/3/30.
     */
    public class UsernameException extends Exception {
        public UsernameException() {
            super();
        }
    
        public UsernameException(String message) {
            super(message);
        }
    }
    复制代码

      7.jsp页面

        7.1success.jsp

    复制代码
    <%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
    <html>
    <body>
    <%--<img src="image/1.jpg">--%>
    <h2>Success!</h2>
    </body>
    </html>
    复制代码

        7.2error.jsp

    复制代码
    <%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>ERROR</h2>
    <p>${ex.message}</p>
    服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天
    </body>
    </html>
    复制代码

        7.3age.jsp

    复制代码
    <%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>年龄错误ERROR</h2>
    <p>${ex.message}</p>
    服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天
    </body>
    </html>
    复制代码

        7.4name.jsp

    复制代码
    <%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>名字错误ERROR</h2>
    <p>${ex.message}</p>
    服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天
    </body>
    </html>
    复制代码

      8.启动tomcat,访问login.jsp

  • 相关阅读:
    EL表达式与JSTL
    jsp
    Servlet 会话
    Servlet 常用类
    Servlet
    Java 网络编程
    CentOS系统下安装python3+Django
    转载Alpine Linux常用命令
    转载Alpine基础
    CentOS启动docker1.13失败(Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.)
  • 原文地址:https://www.cnblogs.com/DawnCHENXI/p/8683903.html
Copyright © 2011-2022 走看看