mvc基础配置

全局异常捕获

 "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

    //匹配不到的异常  默认跳转的页面  value是默认的跳转页面
        "defaultErrorView" value="defaulterror" />  

    //页面取异常信息的时候  给exception起别名
        "exceptionAttribute"  value="ex"/>


    //匹配常见的异常
        "exceptionMappings">
             //匹配常见的异常几种样式
                "java.lang.ArithmeticException">error
                "java.lang.RuntimeException">error
            
        
     

局部异常处理

    @Controller
public class TestController {
    @RequestMapping("/test1")
    public String test1(){
        String s = null ;
        s.length() ;
        return "index" ;
    }
    @RequestMapping("/test2")
    public String tet2(){
        int i = 10 / 0 ;
        return "index" ;
@ExceptionHandler(value = NullPointerException.class)
    public String handlerException(RuntimeException e, HttpServletRequest req){
        req.setAttribute("e", e);
        return "error";
    }

    @ExceptionHandler(value =  ArithmeticException.class)
    public String handlerException1(RuntimeException e, HttpServletRequest req){
        req.setAttribute("e", e);
        return "error";
      }
  }

视图解析器,只处理转发,不处理重定向

 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/" />
         <property name="suffix" value=".jsp" />
     

你可能感兴趣的:(mvc基础配置)