• 一个简单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;
        }

    }
  • 相关阅读:
    java+selenium+new——同一个标签窗口里 ,访问多个网页的后退driver.navigate().back()、前进driver.navigate().forward()、刷新driver.navigate().refresh()等功能 。以及获取当前页面的title属性driver.getTitle()和获取当前页面的url地址driver.getCurrentUrl()
    SoapUI接口测试——关联——参数化
    SoapUI接口测试——添加测试套件——new TestSuite——(类似于postman里面的集合)——添加测试步骤——teststeps(测试步骤)
    java+selenium+new——获取网页源代码driver.getPageSource()
    g++命令行详解
    hdoj_1503Advanced Fruits
    指针遍历vector向量
    最长公共子序列
    hdoj_1087Super Jumping! Jumping! Jumping!
    pcc32应用1
  • 原文地址:https://www.cnblogs.com/felix-/p/4329823.html
Copyright © 2020-2023  润新知