0
我有一个视图窗体,其中包含一个带有文本框的表格。 我需要将表格的信息保存在数据库中。作为parameterJSONArray传递给Spring控制器
* javascript函数*
function sendTableToServer()
{
var table;
table = document.getElementById('myTable');
var rowLength = table.rows.length;
var tableArray = [rowLength -1];
var JSONObject;
for (var tblRow = 1; tblRow < rowLength; tblRow++){
console.log("***** ROW CHANGE *****");
JSONObject = {
"rowIndex": "myTableRow" + tblRow,
"partNo": table.rows[tblRow].cells[0].firstChild.value,
"partName":table.rows[tblRow].cells[1].firstChild.value,
};
f24Array[tblRow] = JSONObject;
}
console.log(f24Array[rowLength-1]["rowIndex"]);
console.log(f24Array.length -1);
$.getJSON("f24BatcCreateEAF.do", {
type: "F24",
pRefForm: "DVL",
pF24Values: tableArray
}
, function(ans) {
console.log("The row is" +ans.strVar);
}
);
};
*控制器*
public @ResponseBody
AjaxReturnMessage createEaf(Model model, HttpServletRequest pRequest, @RequestParam String type, @RequestBody String[] pF24Values) throws Exception {
long eafId=0;
AjaxReturnMessage pARM = new AjaxReturnMessage();
log.info("entering creatEaf);
try {
System.out.println("calling gF24BatchService");
eafId = gF24BatchService.createEAFRow(model, pp, type, pRefForm, pDescription);
pARM.setId(eafId);
pARM.setStrVar("f24TableRow1");
pARM.setCode(AjaxReturnMessage.STATUS_ERROR);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return the jsp page only
return pARM;
}
当我触发sendTableToServer功能我得到一个错误 “404”。 但是,如果我从JSON调用中删除pF24Values,并且他的服务没有错误。
如何在不需要创建新的对象类型的情况下“捕捉”pF24Values?这可能吗?
感谢
感谢您的帮助mckdos 因此,使用表单并使用@RequestBody将信息捕获为POST会解决问题吗? 你能给我一个简单的例子,我已经看到很多使用类型为“employee”或“anmal”的对象作为例子的示例,但如果可能的话,它喜欢粘贴一个字符串数组。 – Yan
我在我的手机上,因此我无法检查代码是否对不起,并且不会再在PC附近待一天。但jist是用JSON编码你的表格数据,然后将数据发布到请求主体中。你的控制器应该不需要太多的改变,除非解组JSON(Spring可以自动完成这个操作?)。 – nickdos
您是否尝试过使用POST呢?这个帖子可能会有所帮助http://stackoverflow.com/questions/5908466/jquery-spring-mvc-requestbody-and-json-making-it-work-together – nickdos