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}]

    的数据。
  • 相关阅读:
    mysql 存储结构
    Mysql 创建表
    java 定时任务
    SpringBoot
    Spring : Spring初识(二)
    hadoop
    JAVA学习路线图
    redis缓存和cookie实现Session共享
    说说 JAVA 代理模式
    Spring 学习教程(五):快速入门
  • 原文地址:https://www.cnblogs.com/shenming/p/3808038.html
Copyright © 2011-2022 走看看