a做爰视频免费观费网站,网站建设万首先金手指13,上海网络推广外包公司,wordpress升级php7这篇文章描述了在Spring MVC 3中执行错误处理的不同技术。该代码在GitHub上的Spring-MVC-Error-Handling目录中可用。 它基于带有注释的Spring MVC示例。 在Spring 3之前处理异常 在Spring 3之前#xff0c;使用HandlerExceptionResolvers处理异常。 此接口定义一个方法使用HandlerExceptionResolvers处理异常。 此接口定义一个方法 ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response,Object handler,Exception ex) 注意它返回一个ModelAndView对象。 因此遇到错误意味着将被转发到特殊页面。 但是此方法不适用于REST Ajax对JSON的调用例如。 在这种情况下我们不想返回页面我们可能想返回特定的HTTP状态代码。 提供了进一步描述的解决方案。 为了本示例的缘故已经创建了两个假的CustomizedException1和CustomizedException2异常。 要将自定义的异常映射到视图可以并且仍然可以使用impleMappingExceptionResolver SimpleMappingExceptionResolver getSimpleMappingExceptionResolver() {SimpleMappingExceptionResolver result new SimpleMappingExceptionResolver();// Setting customized exception mappingsProperties p new Properties();p.put(CustomizedException1.class.getName(), Errors/Exception1);result.setExceptionMappings(p);// Unmapped exceptions will be directed thereresult.setDefaultErrorView(Errors/Default);// Setting a default HTTP status coderesult.setDefaultStatusCode(HttpStatus.BAD_REQUEST.value());return result;} 我们将CustomizedException1映射到Errors / Exception1 JSP页面视图。 我们还为未映射的异常设置了默认错误视图在此示例中为CustomizedException2。 我们还设置了默认的HTTP状态代码。 这是Exception1 JSP页面默认页面与此类似 %page contentTypetext/html pageEncodingUTF-8%
% taglib prefixc urihttp://java.sun.com/jsp/jstl/core %
!doctype html
html langen
headmeta http-equivContent-Type contenttext/html; charsetUTF-8titleWelcome To Exception I !!!/title
/head
bodyh1Welcome To Exception I !!!/h1Exception special message:${exception.specialMsg}a hrefc:url value//Home/a
/body
/html 我们还创建了一个虚拟错误控制器来帮助触发这些异常 Controller
public class TriggeringErrorsController {RequestMapping(value /throwCustomizedException1)public ModelAndView throwCustomizedException1(HttpServletRequest request,HttpServletResponse response)throws CustomizedException1 {throw new CustomizedException1(Houston, we have a problem!);}RequestMapping(value /throwCustomizedException2)public ModelAndView throwCustomizedException2(HttpServletRequest request,HttpServletResponse response)throws CustomizedException2 {throw new CustomizedException2(Something happened on the way to heaven!);}...} 在Spring 3之前将在Web.xml中将SimpleMappingExceptionResolver声明为Bean。 但是我们将使用HandlerExceptionResolverComposite稍后将对其进行描述。 我们还在web.xml中为HTTP状态代码配置目标页面这是处理问题的另一种方法 error-pageerror-code404/error-codelocation/WEB-INF/pages/Errors/My404.jsp/location
/error-page 从Spring 3.X开始有什么新功能 ResponseStatus批注是在调用方法时设置Http状态代码的新方法。 这些由ResponseStatusExceptionResolver处理。 ExceptionHandler注释有助于在Spring中处理异常。 此类注释由AnnotationMethodHandlerExceptionResolver处理。 下面说明了如何在触发我们的自定义异常时使用这些注释将HTTP状态代码设置为响应。 该消息在响应的正文中返回 Controller
public class TriggeringErrorsController {...ExceptionHandler(Customized4ExceptionHandler.class)ResponseStatus(valueHttpStatus.BAD_REQUEST)ResponseBodypublic String handleCustomized4Exception(Customized4ExceptionHandler ex) {return ex.getSpecialMsg();}RequestMapping(value /throwCustomized4ExceptionHandler)public ModelAndView throwCustomized4ExceptionHandler(HttpServletRequest request,HttpServletResponse response)throws Customized4ExceptionHandler {throw new Customized4ExceptionHandler(S.O.S !!!!);}} 在用户端如果使用Ajax调用则可以使用以下方法我们正在使用JQuery来检索错误 $.ajax({type: GET,url: prefix /throwCustomized4ExceptionHandler,async: true,success: function(result) {alert(Unexpected success !!!);},error: function(jqXHR, textStatus, errorThrown) {alert(jqXHR.status jqXHR.responseText);}
}); 一些使用Ajax的人喜欢返回带有错误代码的JSON和一些用于处理异常的消息。 我觉得这太过分了。 一个简单的错误号和一条消息使其保持简单。 由于我们使用了多个解析器因此我们需要一个复合解析器如前所述 Configuration
public class ErrorHandling {...BeanHandlerExceptionResolverComposite getHandlerExceptionResolverComposite() {HandlerExceptionResolverComposite result new HandlerExceptionResolverComposite();ListHandlerExceptionResolver l new ArrayListHandlerExceptionResolver();l.add(new AnnotationMethodHandlerExceptionResolver());l.add(new ResponseStatusExceptionResolver());l.add(getSimpleMappingExceptionResolver());l.add(new DefaultHandlerExceptionResolver());result.setExceptionResolvers(l);return result;} DefaultHandlerExceptionResolver解析标准的Spring异常并将其转换为相应的HTTP状态代码。 运行示例 编译后可以使用mvn tomcatrun运行该示例。 然后浏览 http// localhost8585 / spring-mvc-error-handling / 主页将如下所示 如果单击“例外1”链接将显示以下页面 如果单击“例外2”链接将显示以下页面 如果单击“异常处理程序”按钮将显示一个弹出窗口 这些技术足以涵盖Spring中的错误处理。 更多春天相关的帖子在这里 。 参考 技术说明博客上来自JCG合作伙伴 Jerome Versrynge的Spring MVC错误处理 。 翻译自: https://www.javacodegeeks.com/2012/11/spring-mvc-error-handling-example.html