zoukankan      html  css  js  c++  java
  • spring mvc(三)开发环境搭建和HelloWorld程序

    Spring MVC响应中返回JSON数据的方法:

    配置与以前相同使用<mvc:annotation-driven/>的注解配置,
    但WEB-INF/lib的类路径里面要有jackson-all-1.6.9.jar这个库文件,

    然后在controller里面这样写:


    @Controller
    @RequestMapping("/user" )

    public class UserController {
           
           @RequestMapping("/ajax2" )
           @ResponseBody
           public List<User> ajax2(){
                System. out.println("ajax request2--" );
                List<User> userList= new ArrayList<User>();
                 for(int i=0; i<5; i++){
                      User user= new User();
                      user.setName( "测试"+(i+1));
                      user.setPassword( "1234"+i);
                      user. new Date());
                      userList.add(user);
                }
                
                 return userList;
          }

    加粗的表示这是返回由jackson转换后的json对象,可以是对象、数组、列表等,在地址里

    敲入:http://localhost:8080/spring/user/ajax2 (这里工程context路径是spring),
    会返回

    [{birthday:1339237340265, name:测试1, password:12340}
    , {birthday:1339237340265, name:测试2, password:12341}
    , {birthday:1339237340265, name:测试3, password:12342}
    , {birthday:1339237340265, name:测试4, password:12343}
    , {birthday:1339237340265, name:测试5, password:12344}]

    的数据。
  • 相关阅读:
    hadoop 环境配置
    批量生成不同尺寸的图片
    如何生成publish windows app 用到的 pfx 文件
    MVC项目用Windsor注入
    UWP textbox 只能输入数字
    power shell upload file to azure storage
    Checkbox can't checked
    安装部署 Goaccess
    阿里云OSS的Bucket容量大小采集
    1. Nagios和 NagiosQL安装及配置
  • 原文地址:https://www.cnblogs.com/shenming/p/3808038.html
Copyright © 2011-2022 走看看