本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9691614
建立时间:2004年12月20日




[j2ee]spring mvc的异常处理 
原创空间,  软件技术,  电脑与网络

邢红瑞 发表于 2005/8/7 11:16:23

,error.jsp写上![当前位置: 错误提示]对不起,当前页有错。或者java.lang.Exception/app/systemError对于网站是个不错的办法,对于发布的产品,这个出错提示让用户丈二和尚摸不着头脑,应该给出更合理 准确的提示。目前为止,只有spring的mvc做到了这一点。spring的DispatcherServlet没有捕捉所有异常,而是交由HandlerExceptionResolvers 处理,这样一些出错的相应就不会让用户看到了。spring的DispatchServlet代码 /**  * Initialize the HandlerExceptionResolver used by this class.  * If no bean is defined with the given name in the BeanFactory  * for this namespace, we default to no exception resolver.  */ private void initHandlerExceptionResolvers() throws BeansException {  if (this.detectAllHandlerExceptionResolvers) {   // Find all HandlerExceptionResolvers in the ApplicationContext,   // including ancestor contexts.   Map matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(     getWebApplicationContext(), HandlerExceptionResolver.class, true, false);   this.handlerExceptionResolvers = new ArrayList(matchingBeans.values());   // We keep HandlerExceptionResolvers in sorted order.   Collections.sort(this.handlerExceptionResolvers, new OrderComparator());  }  else {   try {    Object her = getWebApplicationContext().getBean(      HANDLER_EXCEPTION_RESOLVER_BEAN_NAME, HandlerExceptionResolver.class);    this.handlerExceptionResolvers = Collections.singletonList(her);   }   catch (NoSuchBeanDefinitionException ex) {    // Ignore, no HandlerExceptionResolver is fine too.    this.handlerExceptionResolvers = getDefaultStrategies(HandlerExceptionResolver.class);   }  } }数据库的出错是经常遇到的,例如链接数据库的网络故障,数据库表被删除等等捕捉DataAccessException是个不错的办法,bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">                           exceptionᠺϮ%@ page language="java" errorPage="/error.jsp" pageEncoding="gb2312" contentType="text/html;charset=gb2312"  %>,error.jsp写上![当前位置: 错误提示]对不起,当前页有错。或者java.lang.Exception/app/systemError对于网站是个不错的办法,对于发布的产品,这个出错提示让用户丈二和尚摸不着头脑,应该给出更合理 准确的提示。目前为止,只有spring的mvc做到了这一点。spring的DispatcherServlet没有捕捉所有异常,而是交由HandlerExceptionResolvers 处理,这样一些出错的相应就不会让用户看到了。spring的DispatchServlet代码 /**  * Initialize the HandlerExceptionResolver used by this class.  * If no bean is defined with the given name in the BeanFactory  * for this namespace, we default to no exception resolver.  */ private void initHandlerExceptionResolvers() throws BeansException {  if (this.detectAllHandlerExceptionResolvers) {   // Find all HandlerExceptionResolvers in the ApplicationContext,   // including ancestor contexts.   Map matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(     getWebApplicationContext(), HandlerExceptionResolver.class, true, false);   this.handlerExceptionResolvers = new ArrayList(matchingBeans.values());   // We keep HandlerExceptionResolvers in sorted order.   Collections.sort(this.handlerExceptionResolvers, new OrderComparator());  }  else {   try {    Object her = getWebApplicationContext().getBean(      HANDLER_EXCEPTION_RESOLVER_BEAN_NAME, HandlerExceptionResolver.class);    this.handlerExceptionResolvers = Collections.singletonList(her);   }   catch (NoSuchBeanDefinitionException ex) {    // Ignore, no HandlerExceptionResolver is fine too.    this.handlerExceptionResolvers = getDefaultStrategies(HandlerExceptionResolver.class);   }  } }数据库的出错是经常遇到的,例如链接数据库的网络故障,数据库表被删除等等捕捉DataAccessException是个不错的办法,bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">                           exception       '> 写java程序不可能不出异常,尤其是web程序,关键是出异常后的错误处理。使用<%@ page language="java" errorPage="/error.jsp" pageEncoding="gb2312" contentType="text/html;charset=gb2312"  %>,error.jsp写上<span class="red_01"><big><big><big>!</big></big></big></span>[当前位置: 错误提示]<span class="style18">对不起,当前页有错。</span>或者<error-page><exception-type>java.lang.Exception</exception-type><!-- Displays a stack trace --><location>/app/systemError</location></error-page>对于网站是个不错的办法,对于发布的产品,这个出错提示让用户丈二和尚摸不着头脑,应该给出更合理 准确的提示。目前为止,只有spring的mvc做到了这一点。spring的DispatcherServlet没有捕捉所有异常,而是交由HandlerExceptionResolvers 处理,这样一些出错的相应就不会让用户看到了。spring的DispatchServlet代码 /**  * Initialize the HandlerExceptionResolver used by this class.  * If no bean is defined with the given name in the BeanFactory  * for this namespace, we default to no exception resolver.  */ private void initHandlerExceptionResolvers() throws BeansException {  if (this.detectAllHandlerExceptionResolvers) {   // Find all HandlerExceptionResolvers in the ApplicationContext,   // including ancestor contexts.   Map matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(     getWebApplicationContext(), HandlerExceptionResolver.class, true, false);   this.handlerExceptionResolvers = new ArrayList(matchingBeans.values());   // We keep HandlerExceptionResolvers in sorted order.   Collections.sort(this.handlerExceptionResolvers, new OrderComparator());  }  else {   try {    Object her = getWebApplicationContext().getBean(      HANDLER_EXCEPTION_RESOLVER_BEAN_NAME, HandlerExceptionResolver.class);    this.handlerExceptionResolvers = Collections.singletonList(her);   }   catch (NoSuchBeanDefinitionException ex) {    // Ignore, no HandlerExceptionResolver is fine too.    this.handlerExceptionResolvers = getDefaultStrategies(HandlerExceptionResolver.class);   }  } }数据库的出错是经常遇到的,例如链接数据库的网络故障,数据库表被删除等等捕捉DataAccessException是个不错的办法,bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">     <property name="exceptionMappings">         <props>             <prop key="java.lang.Exception">exception</prop>             <prop key="org.springframework.dao.DataAccessException">                   redirect:dataAccessException.htm           </prop>         </props>     </property></bean> <bean id="urlMapping" class="org.springframework.web.   servlet.handler.SimpleUrlHandlerMapping">        <property name="mappings">           <props>              <prop key="/admin/index.htm">index</prop>                  <prop key="/admin/dataAccessException.htm">dataAccessException</prop>                  <prop key="/admin/bookRelatedSearch.htm">bookRelatedSearch</prop>            </props>          </property></bean> <bean name="dataAccessException" class="org.springframework.web.servlet.mvc.ParameterizableViewController">     <property name="viewName">          <value>dataAccessException</value>     </property></bean>个人感觉oracle的做法是最好的,它的SQLException中给出详细的错误码,我们可以知道数据库到底出了什么问题,不过也有缺点,有时候提示是错的,耽误不少时间。


阅读全文(6854) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.051 second(s), page refreshed 144768113 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号