zoukankan      html  css  js  c++  java
  • 一个简单json数据提交实例

    1.客户端编程:jsp页面
    <%@ page language="java" contentType="text/html; charset=UTF-8"
     

       pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="js/json2.js"></script>
    <body>
    <form action="">
    <table>
    <tr>
    <td>名字</td>
    <td><input type="text" id="name" name="name"/></td>
    </tr>
    <tr>
    <td>年龄</td>
    <td><input type="text" id="age" name="age"/></td>
    </tr>
    <tr>
    <td><input type="button" value="提交" onclick="commit();"/> </td>
    </tr>
    </table>
    <table id="ulist" border="2">
    </table>
    </form>
    </body>
    <script type="text/javascript">
    function commit(){
      
     $.ajax(

                {type : "post",
                 data:{name: $ ('#name').val(),   
                          age: $ ('#age').val()},
                 url : "testJson_testJson.action",
                 dataType : "JSON",
                 success : callback
                    }
                );

    }
    function callback(data){
        var json =  JSON.parse(data);
        alert("fdf");

        var ulist =    $("#ulist");   

           $.each(json, function(i,item){

             ulist.append(
            "<tr><td>"+item.name+"</td><td>"+item.age+"</td></tr>"
                     );
            })
    }
    </script>
    </html>
    2.服务端编程:用到sturst2

    public class Person {
        private String name;
        private String age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAge() {
            return age;
        }
        public void setAge(String age) {
            this.age = age;
        }

    }

    public class TestJsonAction {

        private static final long serialVersionUID = -3571998877536556903L;

        public String testJson() throws Exception {
            Person p1 = new Person();
            p1.setName("nn");
            p1.setAge("11");
            Person p2 = new Person();
            p2.setName("gg");
            p2.setAge("12");
            Person p3 = new Person();
            p3.setName("rr");
            p3.setAge("24");
            List<Person> ulist = new ArrayList<Person>();
            ulist.add(p1);
            ulist.add(p2);
            ulist.add(p3);
            String name =ServletActionContext.getRequest().getParameter("name");
            String age = ServletActionContext.getRequest().getParameter("age");
            Person p4 = new Person();
            p4.setName(name);
            p4.setAge(age);
            ulist.add(p4);
            JSONArray json = JSONArray.fromObject(ulist);
            ServletActionContext.getResponse().getWriter().print(json);
            return null;
        }

    }
  • 相关阅读:
    汽车发动机参数指标含义
    谷歌浏览器Google Chrome和Adobe Flash Plugins插件安装问题
    今天研究成功ASP动态管理数据表及字段
    漂亮的弹出对话框!
    Opera Dragonfly 提供下载了
    javascript客户端验证函数大全
    C# Regex类用法
    只能输入数字的TextBox
    c#,winform,treeview,选中节点,选中相应的全部子节点,取消节点,取消父节点,小技巧
    WinForm中如何判断关闭事件来源于用户点击右上角的“关闭”按钮
  • 原文地址:https://www.cnblogs.com/felix-/p/4329823.html
Copyright © 2011-2022 走看看