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

    的数据。
  • 相关阅读:
    乱七八杂
    转化跟踪设置说明
    Photoshop图象切片保存为网页HTML(DIV+CSS布局)的方法
    HTTP 错误 403.14
    打开asp出现An error occurred on the server when processing the URL
    ADODB.Connection 错误 '800a0e7a'
    修改客户端连接的服务器IP地址(内部使用)
    关于云计算的10个常见问题解答
    云安全的11个挑战及应对策略
    如何鉴别一个区块链项目的真假?
  • 原文地址:https://www.cnblogs.com/shenming/p/3808038.html
Copyright © 2011-2022 走看看