Просмотр исходного кода

SpringMVC的几种数据响应方式

zhangxiaoyu лет назад: 2
Родитель
Сommit
22a546a0e2

+ 2 - 0
pom.xml

@@ -70,6 +70,8 @@
70 70
             <artifactId>lombok</artifactId>
71 71
             <version>1.18.20</version>
72 72
         </dependency>
73
+
74
+
73 75
     </dependencies>
74 76
 
75 77
 </project>

+ 16 - 0
springMvc/pom.xml

@@ -71,6 +71,22 @@
71 71
       <version>2.3.3</version>
72 72
       <scope>provided</scope>
73 73
     </dependency>
74
+    <dependency>
75
+      <groupId>com.fasterxml.jackson.core</groupId>
76
+      <artifactId>jackson-core</artifactId>
77
+      <version>2.9.8</version>
78
+    </dependency>
79
+    <dependency>
80
+      <groupId>com.fasterxml.jackson.core</groupId>
81
+      <artifactId>jackson-databind</artifactId>
82
+      <version>2.9.8</version>
83
+    </dependency>
84
+    <dependency>
85
+      <groupId>com.fasterxml.jackson.core</groupId>
86
+      <artifactId>jackson-annotations</artifactId>
87
+      <version>2.9.8</version>
88
+    </dependency>
89
+
74 90
   </dependencies>
75 91
 
76 92
 </project>

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

@@ -0,0 +1,46 @@
1
+package com.controller;
2
+
3
+import org.springframework.stereotype.Controller;
4
+import org.springframework.ui.Model;
5
+import org.springframework.web.bind.annotation.RequestMapping;
6
+import org.springframework.web.servlet.ModelAndView;
7
+
8
+import javax.servlet.http.HttpServletRequest;
9
+import javax.servlet.http.HttpServletResponse;
10
+import java.io.IOException;
11
+
12
+/**
13
+ * 数据相应
14
+ */
15
+
16
+@Controller
17
+public class DataResponseController {
18
+    // 返回 modelAndView
19
+    @RequestMapping("/modelView1")
20
+    public ModelAndView modelView1() {
21
+        // 直接返回字符串
22
+        ModelAndView modelAndView = new ModelAndView();
23
+        modelAndView.addObject("name","Mr.zhang");
24
+        modelAndView.setViewName("template/index.jsp");
25
+        return modelAndView;
26
+    }
27
+
28
+    // 返回 modelAndView 参数可以自动注入
29
+    @RequestMapping("/modelView2")
30
+    public ModelAndView modelView2(ModelAndView modelAndView) {
31
+        // 直接返回字符串
32
+        modelAndView.addObject("name","Mr.zhang");
33
+        modelAndView.setViewName("template/index.jsp");
34
+        return modelAndView;
35
+    }
36
+
37
+    // 可以单独设置 Model
38
+    @RequestMapping("/modelView3")
39
+    public String modelView3(Model model) {
40
+        model.addAttribute("name","modelName");
41
+        return "template/index.jsp";
42
+    }
43
+
44
+
45
+
46
+}

+ 30 - 2
springMvc/src/main/java/com/controller/UserController.java

@@ -1,13 +1,18 @@
1 1
 package com.controller;
2 2
 
3
+import com.pojo.Student;
3 4
 import org.aopalliance.intercept.Interceptor;
4 5
 import org.springframework.beans.factory.annotation.Autowired;
5 6
 import org.springframework.stereotype.Controller;
6 7
 import org.springframework.web.bind.annotation.RequestMapping;
8
+import org.springframework.web.bind.annotation.ResponseBody;
9
+import org.springframework.web.servlet.ModelAndView;
7 10
 
8 11
 import javax.servlet.http.HttpServletRequest;
9 12
 import javax.servlet.http.HttpServletResponse;
10 13
 import java.io.IOException;
14
+import java.util.HashMap;
15
+import java.util.Map;
11 16
 
12 17
 @Controller
13 18
 public class UserController {
@@ -20,12 +25,35 @@ public class UserController {
20 25
         // 加了前后缀 完整链接应该是 forward:/template/success.jsp
21 26
         return "redirect:http://baidu.com";
22 27
     }
28
+    // 回写数据 返回字符串
23 29
     @RequestMapping("/save")
24 30
     public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
25 31
         // 直接返回字符串
26
-        System.out.println("1213");
27 32
         response.getWriter().println("save");
28
-//        return "forward:success.jsp";
29 33
 
30 34
     }
35
+    // 回写数据 使用注解进行返回字符串
36
+    @RequestMapping("/save2")
37
+    @ResponseBody // 不进行视图跳转,直接进行数据响应
38
+    public String save2() {
39
+        return "save2";
40
+    }
41
+    // 回写数据 返回对象
42
+    @RequestMapping("/save3")
43
+    @ResponseBody // 不进行视图跳转,直接进行数据响应
44
+    public Student save3() {
45
+        Student student = new Student();
46
+        student.setName("woshinishiws");
47
+        return student;
48
+    }
49
+    // 回写数据 返回对象
50
+    @RequestMapping("/save4")
51
+    @ResponseBody // 不进行视图跳转,直接进行数据响应
52
+    public Map save4() {
53
+        HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
54
+        stringStringHashMap.put("1","2");
55
+        return stringStringHashMap;
56
+    }
57
+
58
+
31 59
 }

+ 13 - 0
springMvc/src/main/java/com/pojo/Student.java

@@ -0,0 +1,13 @@
1
+package com.pojo;
2
+
3
+public class Student {
4
+    public String name;
5
+
6
+    public String getName() {
7
+        return name;
8
+    }
9
+
10
+    public void setName(String name) {
11
+        this.name = name;
12
+    }
13
+}

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

@@ -28,4 +28,13 @@
28 28
 <!--               <bean class="com.interceptor.MyInterceptor2"></bean>-->
29 29
 <!--           </mvc:interceptor>-->
30 30
 <!--       </mvc:interceptors>-->
31
+<!--    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">-->
32
+<!--        <property name="messageConverters">-->
33
+<!--            <list>-->
34
+<!--                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>-->
35
+<!--            </list>-->
36
+<!--        </property>-->
37
+
38
+<!--    </bean>-->
39
+        <mvc:annotation-driven></mvc:annotation-driven>
31 40
 </beans>

+ 1 - 1
springMvc/src/main/webapp/template/index.jsp

@@ -11,6 +11,6 @@
11 11
     <title>Title</title>
12 12
 </head>
13 13
 <body>
14
-<h1>hello world</h1>
14
+<h1>hello world ${name}</h1>
15 15
 </body>
16 16
 </html>