数据传递-------@ResponseBody

 

1、导入jar包

jack-core-asl-1.9.11.jar

jack-mapper-asl-1.9.11.jar

2、配置springmvc-servlet.xml文件

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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 
    <context:component-scan base-package="com.wh"></context:component-scan>
 
    <mvc:annotation-driven ></mvc:annotation-driven>
     
    <!-- 配置解析JSON类型 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>
     
</beans>

3、编写javascript的ajax和后台springmvc的控制器

handler.java

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.wh.handler;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller
public class TestResponseBody {
    /**
     * 返回一个字符串的json格式
     *
     * produces="text/html;charset=UTF-8"
     * 声明返回的类型
     */
    @RequestMapping(value="/dealAjax.action",produces="text/html;charset=UTF-8")
    public @ResponseBody String dealAjax(){
        return "成功";
    }
    /*
    <script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap/js/jquery.js"></script>
    <script>
        $(function() {
            $('button').click(function() {
                $.ajax({
                    "url" : "dealAjax.action",
                    "type" : "post"
                 "data":{"username":"jack"}
                }).done(function(result) {
                    alert(result);
                }).fail(function(e) {
                    alert(e);
                });
            });
        });
    </script>
    */
     
    /**
     * 返回一个对象的json格式
     *
     * 传对象时,不能指定为produces="text/html;charset=UTF-8"
     */
    @RequestMapping(value="/dealAjax2.action")
    public @ResponseBody User dealAjax2(){
        User u=new User("张三",20,"男");
        return u;
    }
/*
<script>
    $(function() {
        $('button').click(function() {
            $.ajax({
                "url" : "${pageContext.request.contextPath}/dealAjax2.action",
                "type" : "post",
                 //注意:这里不能加下面这行,否则数据会传不到后台
                // "contentType": "application/json;charset=utf-8",
                "data":{"username":"jack"}
            }).done(function(data) {
                console.log("success...");
                console.log(data);
                alert(data.uname+"----"+data.uage+"----"+data.usex);
                //alert(data.uname);
            }).fail(function(e) {
                console.log("error...");
                console.log(e.responseText);
                //alert(e);
            });
        });
    });
</script>
*/
 
    /**
     * 返回一个list集合的json格式
     *
     * 传对象时,不能指定为produces="text/html;charset=UTF-8"
     */
    @RequestMapping(value="/dealAjax3.action")
    public @ResponseBody List<User> dealAjax3(){
        List<User> list=new ArrayList<User>();
        User u1=new User("张三",20,"男");
        User u2=new User("张三",20,"男");
        User u3=new User("张三",20,"男");
        list.add(u1);
        list.add(u2);
        list.add(u3);
        return list;
    }
/*
<script>
    $(function() {
        $('button').click(function() {
            $.ajax({
                "url" : "${pageContext.request.contextPath}/dealAjax3.action",
                "type" : "post",
                 //注意:这里不能加下面这行,否则数据会传不到后台
                 //"contentType": "application/json;charset=utf-8",
                "data":{"username":"jack"}
            }).done(function(data) {
                $.each(data,function(i,item){
                    console.log("i="+i);
                    console.log("item"+item);
                    console.log(item.uname+"---"+item.uage);
                });
            }).fail(function(e) {
                console.log("error...");
                console.log(e.responseText);
            });
        });
    });
</script>
 */
     
    /**
     * 返回一个Map集合的json格式,且map中的key值写死了
     */
    @RequestMapping(value="/dealAjax4.action")
    @ResponseBody     //此注解不能省略 否则ajax无法接受返回值
    public Map<String,User> dealAjax4(){
        Map<String,User> map=new HashMap<String,User>();
        map.put("map1",new User("张三",20,"男"));
        return map;
    }
/*
 * <script>
    $(function() {
        $('button').click(function() {
            $.ajax({
                "url" : "${pageContext.request.contextPath}/dealAjax4.action",
                "type" : "post",
                "dataType":"json", 
                "data":{"username":"jack"}
            }).done(function(data) {
                alert(data.map1.uname+"-----"+data.map1.uage);
                console.log(data.map1+data.map1.uname+"-----"+data.map1.uage);
            }).fail(function(e) {
                console.log("error...");
                console.log(e.responseText);
            });
        });
    });
</script>
 */
     
    /**
     * 返回多个Map集合的json格式
     * 并在前台遍历输出
     * for ( var key in data) {
     *  console.log(key+"---"+data[key].uname+"---"+data[key].uage);
     * }
     */
    @RequestMapping(value="/dealAjax5.action")
     
    public @ResponseBody Map<String,User> dealAjax5(){
        Map<String,User> map=new HashMap<String,User>();
        map.put("map1",new User("张三",20,"男"));
        map.put("map2",new User("李四",21,"男"));
        map.put("map3",new User("王武",22,"男"));     
        return map;
    }
/*
<script>
    $(function() {
        $('button').click(function() {
            $.ajax({
                "url" : "${pageContext.request.contextPath}/dealAjax5.action",
                "type" : "post",
                "dataType":"json", 
                "data":{"username":"jack"}
            }).done(function(data) {
                 for ( var key in data) {
                    console.log(key+"---"+data[key].uname+"---"+data[key].uage);
                 }
            }).fail(function(e) {
                console.log("error...");
                console.log(e.responseText);
            });
        });
    });
</script>
*/
}

遍历map集合的参考地址:http://blog.163.com/xueling1231989@126/blog/static/102640807201461744258436/

posted @   奋斗的少年WH  阅读(275)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示