« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 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中给出详细的错误码,我们可以知道数据库到底出了什么问题,不过也有缺点,有时候提示是错的,耽误不少时间。 |
|
|