Javaweb front end sends objects to server side

Source: Internet
Author: User

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160613/bdcd67d3f36842edbdfb604425468b0f_th.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

  recent projects need to do a batch deletion of the function, delete conditions have multiple, need to pass from the page to the background server program, a single delete, you can stitch parameters to the URL, the server side to get parameters after the delete operation can be done. But the bulk of the deletion, parameters will be many, the delivery of some trouble. Of course there is a more common solution, is the use of commas, underscores, or semi-colon segmentation, and then to the background of the split before the operation, this method requires the foreground splicing, background parsing, slightly more complicated and error prone. For the Java program, I do not seem very object-oriented. So it's natural to think of the use of JSON string to pass data, the following steps:1, the first to pass the parameters to be encapsulated into a JS object, the code sample is as follows:

    1. var params = [];

    2. var param = {};

    3. Param["AAA"] = "a";

    4. param["BBB"] = true;

    5. param["CCC"] = 2;

    6. Params.push (param);

  2, next to the JS object into a JSON string, the more common practice is to use JSON.STRINGFY (), after trying to find that IE8 browser does not support (this method for Google, Firefox, and IE9 browser version above the perfect support), and our current products only support IE browser, Turn to help Google Baidu, find a more general JS method, the code is as follows:

  1. function Obj2str (obj) {

  2. Switch (typeof (obj)) {

  3. Case ' object ':

  4. var ret = [];

  5. if (obj instanceof Array) {

  6. for (var i = 0, len = obj.length; i < Len; i++)

  7. {

  8. Ret.push (Obj2str (obj[i));

  9. }

  10. Return ' [' + ret.join (', ') + '] ';

  11. }

  12. else if (obj instanceof RegExp)

  13. {

  14. return obj.tostring ();

  15. }

  16. Else

  17. {

  18. For (var a in obj)

  19. {

  20. Ret.push (A + ': ' + obj2str (Obj[a]));

  21. }

  22. Return ' {' + ret.join (', ') + '} ';

  23. }

  24. Case ' function ':

  25. Return ' function () {} ';

  26. Case ' number ':

  27. return obj.tostring ();

  28. Case ' string ':

  29. Return ' "' + obj.replace (/(\\|\")/g, ' \\$1 '). Replace (/\n|\r|\t/g, function (a)

  30. {

  31. return (' \ n ' = = a)? ' \\n ': (' \ r ' = = a)? ' \\r ': (' \ t ' = = a)? ' \\t ': ';

  32. }) + ' "';

  33. Case ' Boolean ':

  34. return obj.tostring ();

  35. Default

  36. return obj.tostring ();

  37. }

  38. }

  3, the front-end package, the use of Ajax request to the background, Java parsing JSON string method, you can find a good way to write on the internet, Of course, you can also use Json-lib-xxx.jar, the jar package is widely used in the project, LZ, a company used it to do a lot of JSON conversion work, using the following methods:

    1. Jsonarray Jsonarray = Jsonarray.fromobject (JSONSTR);

    2. List<map> cmdlist = (list<map>) jsonarray.tocollection (Jsonarray, Map.class);

  4, through the built-in method into a collection, and then passed to Ibatis or their own use of the database persistence layer processing can be.


This article is from the "11247808" blog, please be sure to keep this source http://11257808.blog.51cto.com/11247808/1791158

Javaweb front end sends objects to server side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.