SpringMVC(十三)异常注解

使用异常注解更方便

异常处理类

package demo15AnnotationException;

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 mycom on 2018/3/30.
 */
@Controller
public class ExceptionController {

    /**
     * 这个标签就代表有异常的话就走这个方法
     * @ExceptionHandler()
     * 这个括号中可以指定你想要的特定的异常类型,不写的话就是顶级异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ModelAndView handlerException(Exception ex) {
        ModelAndView mv=new ModelAndView();
        mv.addObject("ex",ex);//保存的数据,在页面上用EL表达式展示错误信息
        //默认情况,下面两个条件都不满足走这个,
        mv.setViewName("error");
        //判断异常类型
        if(ex instanceof NameException){
            mv.setViewName("nameException");
        }
        if(ex instanceof AgeException){
            mv.setViewName("ageException");
        }
        return mv;
    }



    @RequestMapping("/first")
    public String doFirst(String name,int age) throws Exception {
        //根据异常的不同返回不同的页面
        if(!name.equals("admin")){
            throw new NameException("用户名异常");
        }
        if(age>60){
            throw new AgeException("年龄不符合");
        }
        return "success";
    }
}

两种异常类型:用户名异常和年龄异常

package demo15AnnotationException;

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

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

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

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

配置文件中

<?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="demo15AnnotationException"></context:component-scan>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/error/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>




</beans>

页面还是使用的上一篇博客的页面,可以自己定义页面

这种方式只能在本类中使用,在其他类中不能使用,所以我们可以吧处理异常的那个方法提到一个类中,其它类要使用的话就继承这个类,但是这样有一个弊端,在Java中只支持单继承,所以这个类就不能继承其他类了

posted @ 2018-03-31 10:17  明渃筱曦  阅读(182)  评论(0编辑  收藏  举报