Demand:
Passing array parameters to backend in JS
Analysis:
JS in the array is weakly typed can put any type (object, basic type), but if the array is put in the object type, passed to the backend is displayed only object string - [object Object], the reason is as follows:
In the background to receive, can only use request to receive, () method returns a String [], so it should be in the foreground when the transmission of the object called toString () method, then if you still want to pass the object how to do? Cool!
But you can use JSON strings to achieve this, parsing JSON strings into JAVA objects in the background.
Perhaps, you want to say what if it's a composite object, such as the following:
Copy Code The code is as follows.
public class Person {
private String username;
private String password;
private Address addr;
}
The Person object has an address attribute of type addr. It doesn't matter, the value of the attribute that any object will eventually use is a basic data type, and can be parsed using the corresponding wrapper type parseInt, or parseXXX.
Realization:
OK, the principle is such a. First look at how to write JS:
Copy Code The code is as follows.
var as = [];
var temp = [];
for ( var int = 0; int < 5; int++) {
('{"k":');
(int);
(',"v":');
(int);
('}');
((""));
}
//Methods in Jquery, see the Jquery API for details.
$.post(
"servlet/AjaxServlet?m=putWarningRule",{"aa":as}
);
The final string will look like this (for example only)
Copy Code The code is as follows.
{"k":0,"v":0}
Backend reception, do not discuss any framework, just HttpServletRequest
Copy Code The code is as follows.
String[] jsonArr = ("aa[]");
One thing to note, in the js when passing parameters named "aa", but in the background when receiving is "aa[]", here should be Jquery to do the conversion, so the best way is to change in the JS to "aa[]", the reason why this is not written here "[]" is to illustrate the problem. You can use the following way to print all the parameters in the request
Copy Code The code is as follows.
Enumeration<String> names = ();
while (()) {
String string = (String) ();
(string);
}
OK, so far, has been received, the rest is how to turn a JSON string into a POJO. I use jsontools-core-1., this jar package relies on antlr-2.7., download from the code base, download, import classpath, write a simple tool class, there are mainly so 2 methods:
Copy Code The code is as follows.
/**
* :: Converting objects to JSON-formatted strings
* @param obj
* @return Returns a JSON string.
*/
public static String toJSONAsString(Object obj){
try {
return (obj).render(false);
} catch (MapperException e) {
();
}
return null;
}
@SuppressWarnings("unchecked")
public static <T> T jsonToObject(String jsonStr, Class<T> targetClass) throws TokenStreamException, RecognitionException, MapperException{
JSONValue jv = new JSONParser(new StringReader(jsonStr)).nextValue();
return (T) (jv,targetClass);
}
//test
public static void main(String[] args) throws Exception {
Person p = new Person();
("a");
("v");
String json = toJSONAsString(p);
Person np = jsonToObject(json,);
(()+"=="+());
}
After request fetches the value, iterates through the array, converting it one by one.
Copy Code The code is as follows.
Person p = (jsonArr[0], );
The Person class is as follows:
Copy Code The code is as follows.
public class Person {
private String k;
private String v;
public String getK() {
return k;
}
public void setK(String k) {
= k;
}
public String getV() {
return v;
}
public void setV(String v) {
= v;
}
}