spring4mvc返回json(bean,list,map)

  因为spring3和spring4的mvc在前端返回json所需要的jar包不一样,所以索性写一篇关于spring4mvc在前端返回json的博文。

首先,新建一个web项目,项目格式如图所示:

 

convertJsonAction:springmvc的action类

inde.jsp:有三个按钮,分别返回bean,list和map对应的json

然后是引入的library:分别是spring-core,spring-mvc,spring-web,spring4mvc-jackson

tips:spring4的mvc引入的jackson跟spring3的mvc引入的jar包不一样!!!不一样!!!!

spring-core:

spring-mvc:

spring-web:

spring4mvc-jackson:

接着是配置web.xml

因为springmvc是servelet,所以在web.xml中配置的就是一个servlet,在web.xml中添加如下代码

 <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
               <param-name>contextConfigLocation</param-name>
               <!-- 将配置对应的springmvc文件设置在src根目录下 -->    
               <param-value>classpath:springmvc.xml</param-value>
       </init-param>
        
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

 

然后在src下面新建一个叫springmvc的xml文件,文件内容如图所示:

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
    
</beans>

 

接着我们在ConvertJSonAction.java类中添加一个方法:

package com.mz.json;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * controller指明这是一个springmvc的action类
 * requestMapping是总的路径,所有访问这个action类中的方法必须通过/convert
 * @author asus
 *
 */
@Controller
@RequestMapping("/convert")
public class ConvertJsonAction {
    //responseBody是指返回的User是一个json对象(也可以是string或者xml对象)
    @RequestMapping("/bean2json")
    public @ResponseBody User bean2json() {
        User user = new User();
        user.setId(1);
        user.setUsername("Irasel");
        return user;
    }
}

 

 这是action的处理类,接着我们在springmvc中配置开启mvc的annotation和扫描标签:

<mvc:annotation-driven />
    <context:component-scan base-package="com.mz.json"></context:component-scan>

 

index.jsp中添加三个按钮,每一个按钮分别对应的返回一个bean对象的json,list对象的json和map对象的json:

 <!-- 点击返回一个bean对象的json -->
    <input type="button" value="bean2json"><br/>
    <!-- 点击返回一个list对象的json -->
    <input type="button" value="list2json"> <br/>
    <!-- 点击返回一个map对象的json -->
    <input type="button" value="map2json"><br/>

 

使用jquery的异步传输:

    $(':button:first').click(function(){
              var url = 'convert/bean2json.action';
              var arg =null;
              $.post(url,arg,function(returnData){
                  console.log(returnData);
              });
          });

 

效果如图所示:

接着是list和map的方法(不再粘贴效果图了,反正都差不多)

    @RequestMapping("/list2json")
    public @ResponseBody List<User> list2Json() {
        List<User> users = new ArrayList<User>();
        User user1 = new User();
        user1.setId(1);
        user1.setUsername("Irasel");
        User user2 = new User();
        user2.setId(2);
        user2.setUsername("路西法");
        User user3 = new User();
        user3.setId(3);
        user3.setUsername("Micheal");
        users.add(user1);
        users.add(user2);
        users.add(user3);
        return users;
    }
    @RequestMapping("/map2json")
    public @ResponseBody Map<String, Object> map2Json() {
        List<User> users = new ArrayList<User>();
        Map<String, Object> map = new HashMap<String, Object>();
        User user1 = new User();
        user1.setId(1);
        user1.setUsername("Irasel");
        User user2 = new User();
        user2.setId(2);
        user2.setUsername("路西法");
        User user3 = new User();
        user3.setId(3);
        user3.setUsername("Micheal");
        users.add(user1);
        users.add(user2);
        users.add(user3);
        map.put("row", users.size());
        map.put("users", users);
        return map;
        
    }

 

posted @ 2016-11-19 13:21  我是一只小哈皮  阅读(6846)  评论(0编辑  收藏  举报