Complex JSON submitted to Struts2 normal request.getparameter () is not available

Source: Internet
Author: User

Today, when coding a strange problem encountered in the front-end client segment to submit JSON data to STRUTS2, the corresponding JSON data through Request.getparameter () is null

The front desk jquery is as follows:

$.post ("Test.do", {"ReportID": "2c90f1884cc110e6014cc117de000002", "FileFormat": "Docx", "ReName": "Text", "columns": "Name, State", "isindent": "Y"});

The above is the most common kind of ordinary JSON, single key-value form, this can be obtained by Request.getparameter ("name") the corresponding value

But if that's the case, it's hard to say.

$.post ("Test.do", {"ReportID": "2c90f1884cc110e6014cc117de000002", "FileFormat": "Docx", "ReName": "Text", "columns": "Name, State", "isindent": "Y", "Conditiondtos": [{"KeyName": "Namelike", "KeyCode": "Like", "keyValue": "% will%"}]});

The above is a special case, in JSON is not a simple form of key-value, but contains a special case of Key-[key-value], that is, he is a complex JSON object, If JSON is submitted in a similar manner. The action in the early struts2 is not the entire content, note that I am talking about the whole content, why do you say so?
The reason is very simple, in this case, the previous definition of the key-value can be obtained, and the complex key-[key-value] is not get, since Struts2 so, ordinary servlect is so, a bit basic understand, here does not detail

So the problem comes, I want to get key-[key-value] this complex json,request.getparameter () can not solve the problem, how to do?
This leads us to the following:
Request.getreader ();
Yes, to solve everything in a flow.
The specific code is as follows (Struts2 for example):
Public String Exportreportbyjson () throws IOException {HttpServletRequest request = Servletactioncontext.getrequest (); String line =null; StringBuffer json = new StringBuffer (); BufferedReader reader = Null;reader = Request.getreader ();        while (line = Reader.readline ())! = null) {            json.append (line);        }        Reader.close ();        System.out.println (Json.tostring ()); return null;}

To get all the data submitted by the client in a stream, it is important to note that
The three methods of Request.getparameter (), Request.getinputstream (), Request.getreader () are conflicting because the stream can only be read once.
So, if the stream is called after it gets finished, theRequest.getparameter ()It will be an error, please note that OH

Well, the above is a more special server to get complex JSON solution, will be useful to the time, please collect OH

Complex JSON is submitted to Struts2 normal request.getparameter () is not available

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.