Browse Source

Spring中的异常处理机制

zhangxiaoyu 2 years ago
parent
commit
0a3d3e42dc

+ 5 - 0
springMvc/src/main/java/com/controller/DataResponseController.java

@@ -1,5 +1,7 @@
1
 package com.controller;
1
 package com.controller;
2
 
2
 
3
+import com.service.UserService;
4
+import org.springframework.beans.factory.annotation.Autowired;
3
 import org.springframework.stereotype.Controller;
5
 import org.springframework.stereotype.Controller;
4
 import org.springframework.ui.Model;
6
 import org.springframework.ui.Model;
5
 import org.springframework.web.bind.annotation.RequestMapping;
7
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -15,6 +17,8 @@ import java.io.IOException;
15
 
17
 
16
 @Controller
18
 @Controller
17
 public class DataResponseController {
19
 public class DataResponseController {
20
+    @Autowired
21
+    UserService userService;
18
     // 返回 modelAndView
22
     // 返回 modelAndView
19
     @RequestMapping("/modelView1")
23
     @RequestMapping("/modelView1")
20
     public ModelAndView modelView1() {
24
     public ModelAndView modelView1() {
@@ -37,6 +41,7 @@ public class DataResponseController {
37
     // 可以单独设置 Model
41
     // 可以单独设置 Model
38
     @RequestMapping("/modelView3")
42
     @RequestMapping("/modelView3")
39
     public String modelView3(Model model) {
43
     public String modelView3(Model model) {
44
+        userService.saveUser();
40
         model.addAttribute("name","modelName");
45
         model.addAttribute("name","modelName");
41
         return "template/index.jsp";
46
         return "template/index.jsp";
42
     }
47
     }

+ 14 - 0
springMvc/src/main/java/com/controller/ExceptionTestController.java

@@ -1,7 +1,21 @@
1
 package com.controller;
1
 package com.controller;
2
 
2
 
3
+import com.exception.MyException;
3
 import org.springframework.stereotype.Controller;
4
 import org.springframework.stereotype.Controller;
5
+import org.springframework.web.bind.annotation.RequestMapping;
6
+import org.springframework.web.bind.annotation.ResponseBody;
7
+
8
+import java.util.HashMap;
9
+import java.util.Map;
4
 
10
 
5
 @Controller
11
 @Controller
6
 public class ExceptionTestController {
12
 public class ExceptionTestController {
13
+    @RequestMapping("/exception1")
14
+    @ResponseBody // 异常测试
15
+    public Map exception1() {
16
+        HashMap stringStringHashMap = new HashMap<String, String>();
17
+        stringStringHashMap.put("1","2");
18
+        throw new MyException();
19
+//        return stringStringHashMap;
20
+    }
7
 }
21
 }

+ 1 - 1
springMvc/src/main/java/com/controller/UserController.java

@@ -59,7 +59,7 @@ public class UserController {
59
     public Map save4() {
59
     public Map save4() {
60
         HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
60
         HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
61
         stringStringHashMap.put("1","2");
61
         stringStringHashMap.put("1","2");
62
-
62
+        int  a = 1/0;
63
         return stringStringHashMap;
63
         return stringStringHashMap;
64
     }
64
     }
65
 
65
 

+ 5 - 0
springMvc/src/main/java/com/exception/MyException.java

@@ -0,0 +1,5 @@
1
+package com.exception;
2
+
3
+public class MyException extends RuntimeException{
4
+
5
+}

+ 20 - 0
springMvc/src/main/java/com/exception_resolver/MyExceptionResolver.java

@@ -0,0 +1,20 @@
1
+package com.exception_resolver;
2
+
3
+import com.exception.MyException;
4
+import org.springframework.stereotype.Component;
5
+import org.springframework.web.servlet.HandlerExceptionResolver;
6
+import org.springframework.web.servlet.ModelAndView;
7
+
8
+import javax.servlet.http.HttpServletRequest;
9
+import javax.servlet.http.HttpServletResponse;
10
+@Component
11
+public class MyExceptionResolver implements HandlerExceptionResolver {
12
+    @Override
13
+    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
14
+        ModelAndView modelAndView = new ModelAndView();
15
+        String message = e instanceof MyException ? "自定义异常":"非自定义的异常";
16
+        modelAndView.addObject("message",message);
17
+        modelAndView.setViewName("my_error");
18
+        return modelAndView;
19
+    }
20
+}

+ 9 - 10
springMvc/src/main/resources/Spring-mvc.xml

@@ -53,14 +53,13 @@
53
             <property name="defaultEncoding" value="UTF-8"></property>
53
             <property name="defaultEncoding" value="UTF-8"></property>
54
         </bean>
54
         </bean>
55
 <!--        配置简单映射异常处理器器-->
55
 <!--        配置简单映射异常处理器器-->
56
-        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
57
-            <property name="defaultErrorView" value="error"></property>
58
-            <property name="exceptionMappings">
59
-                <map>
60
-<!--                    <entry key="">-->
61
-
62
-<!--                    </entry>-->
63
-                </map>
64
-            </property>
65
-        </bean>
56
+<!--        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">-->
57
+<!--&lt;!&ndash;            <property name="defaultErrorView" value="error"></property>&ndash;&gt;-->
58
+<!--            <property name="exceptionMappings">-->
59
+<!--                <map>-->
60
+<!--                    <entry key="java.lang.RuntimeException" value="runtime_error"></entry>-->
61
+<!--                    <entry key="com.exception.MyException" value="my_error"></entry>-->
62
+<!--                </map>-->
63
+<!--            </property>-->
64
+<!--        </bean>-->
66
 </beans>
65
 </beans>

+ 16 - 0
springMvc/src/main/webapp/template/my_error.jsp

@@ -0,0 +1,16 @@
1
+<%--
2
+  Created by IntelliJ IDEA.
3
+  User: 77179
4
+  Date: 2022/5/25
5
+  Time: 20:31
6
+  To change this template use File | Settings | File Templates.
7
+--%>
8
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9
+<html>
10
+<head>
11
+    <title>Title</title>
12
+</head>
13
+<body>
14
+<h1>${message}</h1>
15
+</body>
16
+</html>

+ 16 - 0
springMvc/src/main/webapp/template/runtime_error.jsp

@@ -0,0 +1,16 @@
1
+<%--
2
+  Created by IntelliJ IDEA.
3
+  User: 77179
4
+  Date: 2022/5/25
5
+  Time: 20:31
6
+  To change this template use File | Settings | File Templates.
7
+--%>
8
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9
+<html>
10
+<head>
11
+    <title>Title</title>
12
+</head>
13
+<body>
14
+<h1>runtime_error</h1>
15
+</body>
16
+</html>