web開發學習筆記(一)

來源:互聯網
上載者:User

標籤:blog   io   os   使用   ar   java   for   資料   div   

通過JSON字串將前台資料傳遞至後台,且前台資料是動態變化的,當點擊提交時才將資料傳遞至後台:

  JSON字串形式:var json = ‘{"key1":"value1","key2":"value2"}‘

  在js裡使用map存放動態變化的資料:

    在js中使用map:

var map = {};function change(key,value){map[key] = value;}function submit(){var json = JSON.stringify(map)document.getElementById("form").action="<%=path%>/Controller?json="+json;document.getElementById("form").submit();}

  其中,"form"為表單的id屬性,change(key,value)為存放資料的響應函數。

在後台Controller裡接受json:

  

String json = request.getParameter("json");JSONObject js = new JSONObject();js = JSONObject.fromObject(json);Iterator it = js.keys();//遍曆所有的json內容while(it.hasNext()){    String key = it.next().toString();    String value = js.getString(key);}

  

PS:1、需要注意的是,想要使用此種方法,需要引入幾個jar包:commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-    logging-1.1.1.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar,並且要保證這幾個jar包的版本一致。

  2、在WEB-INF下的lib目錄下也要加入這幾個jar包

web開發學習筆記(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.