首页 > 代码库 > SpringMVC-处理JSON

SpringMVC-处理JSON

1.添加jakson包支持

<!-- jakson版本号 -->
           <jackson.version>2.7.9</jackson.version>
<!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

2.springmvc.xml中配置bean

<!-- 用于将对象转化为JSON -->
    <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;cahrtset=utf-8</value>
            </list>
        </property>
    </bean>
    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="stringConverter"/>
                <ref bean="jsonConverter"/>
            </list>
        </property>
    </bean>

3.User.java

public class User {
private int id; private String name; private String sex; public User() { } public User(int id, String name, String sex) { super(); this.id = id; this.name = name; this.sex = sex; }  //get/set方法省略
}

4.Controller.java

@Controller
public class JsonController {
    
    @RequestMapping("/json")
    //指示方法返回值的注释应绑定到Web响应正文。即将返回值直接传到页面(xhr类型)
    @ResponseBody
    public List<User> json(){
        List<User> list = new ArrayList<User>();
        User u1 = new User(1, "丽娜", "女");
        User u2 = new User(2, "刘杰", "男");
        User u3 = new User(3, "徐斌", "男");
        
        list.add(u1);
        list.add(u2);
        list.add(u3);
//        System.out.println(list);
        return list;
    }

}

5.json.jsp页面

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
    $(function(){
        $("#btn").click(function(){
            $.post("json.do",function(data){
                //返回的是object对象
                var html = "";
                for(var i=0;i<data.length;i++){
                    //拼接表格
                    html+="<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].sex+"</td></tr>";
                }
                $("#tbody").html(html);
            });
        });
    });
</script>
</head>
<body>
<button id="btn">显示数据</button><br>
<hr>
<table width="80%">
    <tr>
        <td>编号:</td>
        <td>姓名:</td>
        <td>性别:</td>
    </tr>
    <tbody id="tbody"></tbody>
</table>

</body>

 

SpringMVC-处理JSON