如何將JSON數組傳遞給Spring-MVC?如何將JSON數組傳遞給Spring-MVC?
我試圖找到一種方法來傳遞對象的數組中的JSON和Spring-MVC(JAVA)..
下面是我的javascript是設置兩個數組,並進行呼叫:
function doAjaxPost() {
var inData = {};
inData.name = ['kurt','johnathan'];
inData.education = ['GSM','HardKnocks'];
htmlStr = JSON.stringify(inData);
alert(htmlStr);
$.post(contexPath + "/AddUser.htm", inData, function(outData, outStatus){
alert(outStatus);
});
};
這裏是我的Java(Spring的MVC)控制器:
@RequestMapping(value="/AddUser.htm",method=RequestMethod.POST)
public @ResponseBody JsonResponse addUser(@ModelAttribute(value="user") User user, BindingResult result){
JsonResponse res = new JsonResponse();
ValidationUtils.rejectIfEmpty(result, "name", "Name can not be empty.");
ValidationUtils.rejectIfEmpty(result, "education", "Educatioan not be empty");
if(!result.hasErrors()){
userList.add(user);
res.setStatus("SUCCESS");
res.setResult(userList);
}else{
res.setStatus("FAIL");
res.setResult(result.getAllErrors());
}
return res;
}
這是我使用的bean:
public class User {
private String name = null;
private String education = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
}
請讓我知道該怎麼得到這個工作的權利......這是我得到.. 嚴重的錯誤:Servlet.service()的servlet的Spring MVC的分發程序Servlet拋出異常 org.springframework.beans。 InvalidPropertyException:bean class [com.raistudies.domain.User]的無效屬性'education []':索引屬性路徑'education []'中引用的屬性既不是數組,也不是列表或映射;返回值[Ljava.lang.String; @ 6fef3212]
可能重複[反序列化json數組在Spring MVC控制器](http://stackoverflow.com/questions/27613613/deserializing-json-array-in-spring-mvc-controller) –