
{"id":2731,"date":"2020-12-16T15:30:45","date_gmt":"2020-12-16T07:30:45","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2731"},"modified":"2021-01-01T14:35:51","modified_gmt":"2021-01-01T06:35:51","slug":"2020java%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%e4%b9%8bspringmvc%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2731","title":{"rendered":"2020java\u6846\u67b6\u6559\u7a0b\u4e4bspringMVC\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<h3>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspringMVC\u5f02\u5e38\u5904\u7406<\/h3>\n<p><strong>\u5b9e\u4f8b1\uff1a\u7b80\u5355\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>1.\u5728\u63a7\u5236\u5c42\u521b\u5efa\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u7684\u65b9\u6cd5\uff0c\u5e76\u4e14\u6dfb\u52a0 @ExceptionHandler \u6ce8\u89e3<\/p>\n<pre>@ExceptionHandler\r\npublic ModelAndView handleException(Exception ex){\r\n    System.out.println(\"\u51fa\u73b0\u5f02\u5e38:\"+ex);\r\n    ModelAndView mav=new ModelAndView();\r\n    mav.setViewName(\"error\");\r\n    mav.addObject(\"exception\",ex);\r\n    return mav;\r\n}<\/pre>\n<p><strong>2.\u521b\u5efa\u9519\u8bef\u9875\u9762<\/p>\n<p><\/strong><!--more--><\/p>\n<pre>&lt;%@ page contentType=\"text\/html;charset=UTF-8\" <span style=\"color: #ff0000;\">isErrorPage=\"true\"<\/span> language=\"java\" %&gt;<\/pre>\n<pre>&lt;body&gt;\r\n<span style=\"color: #ff0000;\">&lt;%=exception.getMessage()%&gt;<\/span>\r\n&lt;\/body&gt;<\/pre>\n<p><strong>\u5b9e\u4f8b2\uff1a\u53ef\u4ee5\u4e3a @ExceptionHandler\u6dfb\u52a0\u4e00\u4e2a\u53c2\u6570\uff1a<\/strong><\/p>\n<p>\u5982\uff1a@ExceptionHandler(NumberFormatException.class)<\/p>\n<p><strong>\u5b9e\u4f8b3\uff1a\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<p><strong>\u5b9e\u4f8b4\uff1a\u5168\u5c40\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff0c\u5e76\u6dfb\u52a0AOP\u7684\u901a\u77e5<\/p>\n<pre><strong><span style=\"color: #ff0000;\">@ControllerAdvice<\/span><\/strong>\r\npublic class MyExceptionHandler {\r\n    @ExceptionHandler(Exception.class)\r\n    public ModelAndView handlerException(Exception ex){\r\n        System.out.println(\"\u51fa\u73b0\u5168\u5c40\u5f02\u5e38\"+ex);\r\n        ModelAndView mav=new ModelAndView();\r\n        mav.setViewName(\"error\");\r\n        mav.addObject(\"exception\",ex);\r\n        return mav;\r\n    }\r\n}<\/pre>\n<p>\u7136\u540e\u5728springMVC-servlet.xml\u914d\u7f6e\u626b\u63cf<\/p>\n<pre>&lt;context:component-scan base-package=\"springmvc.exception\"\/&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspringMVC\u5f02\u5e38\u5904\u7406 \u5b9e\u4f8b1\uff1a\u7b80\u5355\u5f02\u5e38\u5904\u7406 1.\u5728\u63a7\u5236\u5c42\u521b\u5efa\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u7684\u65b9\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2731","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2731"}],"version-history":[{"count":3,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2731\/revisions"}],"predecessor-version":[{"id":2736,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2731\/revisions\/2736"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}