zoukankan      html  css  js  c++  java
  • springmvc 之 ajax

    1. 在springmvc 中依然可以使用 servletapi 来实现ajax的操作。 和 servlet 的时候一致。

    @RequestMapping("/ajax")
    public void ajax(HttpServletResponse resp)throws IOException{
           resp.getWriter().print("springmvc ajax");      
    }

    2. 使用springmvc进行json数据的传递工作。

      a) 需要导入jackson 的 jar 包

      b) 编写处理器

    @RequestMapping("/json")
        @ResponseBody//将相应主体返回
        public List<User> json(){
            List<User> list = new ArrayList<User>();
            list.add(new User(1,"张三",22));
            list.add(new User(2,"李四",42));
            list.add(new User(3,"王五",32));
            return list;
        }

      c) 配置文件中使用:

    <mvc:annotation-driven/>

      d) 页面

    <script type="text/javascript">
          $(function(){
              $('#btn').click(function(){
                  $.post("json",function(data){
                      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].age+"</td></tr>";
                      }
                      $("#content").html(html);
                  });
              });
          });
      </script>
      </head>
      
      <body>
        <button id="btn">获取数据</button>
        <table width="80%" align="center">
            <tr>
                <td>编号</td>
                <td>姓名</td>
                <td>年龄</td>
            </tr>
            <tbody id="content">
            
            </tbody>
        </table>
      </body>
    </html>
  • 相关阅读:
    结对开发地铁
    学习进度04
    构建之法阅读笔记02
    学习进度03
    构建之法阅读笔记01
    Golang开发工具LiteIDE使用方法整理
    package httputil
    package net
    package json
    package encoding
  • 原文地址:https://www.cnblogs.com/forever2h/p/6805442.html
Copyright © 2011-2022 走看看