`

spring mvc3中异常处理

阅读更多
在spring mvc3中,处理异常可以这样做,
首先定义一个自定义的异常类:
1
public class GenericException extends RuntimeException{ 

private String customMsg; 

//getter and setter methods 

public GenericException(String customMsg) { 
this.customMsg = customMsg; 
} 

} 

2 web.xml
 
<web-app ...> 

  <error-page> 
<error-code>404</error-code> 
<location>/WEB-INF/pages/404.jsp</location> 
  </error-page> 

  <error-page> 
<exception-type>java.lang.Exception</exception-type> 
<location>/WEB-INF/pages/404.jsp</location> 
  </error-page> 

</web-app> 



3 控制器
   CustomerController.java
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.AbstractController; 
import com.mkyong.common.exception.GenericException; 

public class CustomerController extends AbstractController{ 

@Override 
protected ModelAndView handleRequestInternal(HttpServletRequest request, 
HttpServletResponse response) throws Exception { 

throw new GenericException("Oppss...System error, please visit it later"); 

} 

} 



4
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

  <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> 

<!-- Register the bean --> 
<bean class="com.mkyong.common.controller.CustomerController" /> 

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
   <property name="exceptionMappings"> 
     <props> 
<prop key="com.mkyong.common.exception.GenericException"> 
GenericExceptionPage 
</prop> 
        <prop key="java.lang.Exception">error</prop> 
     </props> 
   </property> 
</bean> 

<bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 
          <property name="prefix"> 
              <value>/WEB-INF/pages/</value> 
           </property> 
          <property name="suffix"> 
             <value>.jsp</value> 
          </property> 
    </bean> 

</beans> 


5 要注意的是,这里用了SimpleMappingExceptionResolver,
当抛出com.mkyong.common.exception.GenericException时,会先于web.xml抛出异常,这时就去到
/WEB-INF/pages/GenericExceptionPage.jsp这个页面处理你的自定义异常了
分享到:
评论

相关推荐

    spring mvc统一处理异常

    spring mvc统一处理异常,通过@ControllerAdvice+@ExceptionHandler

    spring mvc异常处理

    spring mvc异常处理,详细参考http://blog.csdn.net/xiejx618/article/details/41918611

    Spring MVC入门教程

    十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? 十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 ...

    Spring MVC 教程 快速入门 深入分析

    十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? 十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 ...

    Spring MVC+MyBatis开发从入门到项目实战

    第3篇是Spring MVC技术入门,包括Spring MVC的背景介绍、架构整体剖析、环境搭建、处理器与映射器的讲解、前端控制器的源码分析、多种视图解析器的介绍、请求映射与参数绑定的介绍、Validation校验与异常处理和拦截...

    SpringMVCDemo:Spring MVC 框架知识案例

    1.创建第一个 Spring MVC 程序案例 2.Spring MVC @RequestMapping 注解案例 3.Spring MVC 请求参数的获取案例 4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 ...15.Spring MVC 异常处理案例

    spring mvc异常简单处理

    spring mvc让servlet容器直接处理异常.详细看:http://blog.csdn.net/xiejx618/article/details/41695255

    第19章spring-mvc之全局异常处理

    第19章spring-mvc之全局异常处理

    SpringMVC框架架构介绍

    十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? 十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求...

    springmvc 异常统一处理的三种方式详解.docx

    那么,能不能将所有类型的异常处理从各处理过程解耦出来,这样既保证了相关处理过程的功能较单一,也实现了异常信息的统一处理和维护?答案是肯定的。下面将介绍使用Spring MVC统一处理异常的解决和实现过程。

    使用Spring MVC统一异常处理实战

    NULL 博文链接:https://cgs1999.iteye.com/blog/1547197

    Spring MVC--9.自定义拦截器、异常处理

    Spring MVC--9.自定义拦截器、异常处理

    Spring MVC 3.0实战指南.ppt

    《Spring MVC 3.0实战指南》,参考《Spring 3.x企业应用开发实战》。 内容简介: 1、Spring MVC框架简介 2、HTTP请求地址映射 3、HTTP请求数据的绑定 4、数据转换、格式化、校验 5、数据模型控制 6、视图及解析器 7...

    三. spring mvc 异常统一处理

    NULL 博文链接:https://gaojiewyh.iteye.com/blog/1297746

    problem-spring-web, 在 spring Web MVC中,处理问题的库.zip

    problem-spring-web, 在 spring Web MVC中,处理问题的库 Web站点MVC问题 问题 spring 是一个库,它使得从 spring 应用程序中生成 application/problem json 响应变得容易。 工具填补了一个利基,它将问题库和mvc...

    Spring3MVC注解教程.ppt

    《Spring MVC 3.0实战指南》,参考《Spring 3.x企业应用开发实战》。 内容简介: 1、Spring MVC框架简介 2、HTTP请求地址映射 3、HTTP请求数据的绑定 4、数据转换、格式化、校验 5、数据模型控制 6、视图及...

    详解Spring MVC/Boot 统一异常处理最佳实践

    在 Web 开发中, 我们经常会需要处理各种异常,这篇文章主要介绍了详解Spring MVC/Boot 统一异常处理最佳实践,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    spring-mvc-custom-exception:Spring MVC 中的异常处理

    spring-mvc-custom-exception 在 Spring MVC 中自定义现有的 ExceptionHandlerExceptionResolver 类以动态处理异常的示例如何运行示例 git clone ...mvc-custom-exceptionmvn spring-boot:run

    spring mvc 事务配置+异常处理

    NULL 博文链接:https://gary-bu.iteye.com/blog/2025026

Global site tag (gtag.js) - Google Analytics