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