将serialize数组传递给struts2动作 [英] Passing serialize array to struts2 action
本文介绍了将serialize数组传递给struts2动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我计划将整个表单数据以JSON格式发布到Struts2 Action。以下是我的代码片段。纠正我出错的地方或帮助我,以便我可以正确地获取Action文件中的所有值。我在Action文件中的所有SOP都显示为 null
I am planning to post entire form data in JSON format to Struts2 Action. Below are my code snippets. Correct me where I am going wrong or Help me so that I can get all values in the Action file correctly. All of my SOPs in Action file is displayed as null
var MyForm = $("#companyform").serializeArray();
var data = JSON.stringify(MyForm);
$.ajax({
type: 'POST',
url:'createcompany.action?jsonRequestdata='+data,
dataType: 'json',
success: function(data){
console.log(stringify(data));
}});
我的表格数据变成 [{name:tan 值: RRR},{ 名称: 锅, 值: ADF},{ 名称: TOD, 值: 1}]
Struts2行动档案:
String jsonRequestdata;
public String execute() throws Exception {
JSONArray jsonArr = (JSONArray) new JSONParser().parse(jsonRequestdata);
JSONObject json = (JSONObject) jsonArr.get(0);
System.out.println("TAN=" + json.get("tan"));
System.out.println("PAN=" + json.get("pan"));
System.out.println("TOD=" + json.get("tod"));
return "success";
}
现在输出
Present OUTPUT
TAN=null
PAN=null
TOD=null
推荐答案
由于我使用的是名称,因此我必须使用名称来获取它。下面是工作代码
Since I am using name,value I must get it by using name. Below is the working code
JSONArray jsonArr = (JSONArray) new JSONParser().parse(jsonRequestdata);
for(int i=0;i<jsonArr.size();i++){
JSONObject json=(JSONObject) jsonArr.get(i);
System.out.println("name=" + json.get("name"));
System.out.println("value=" + json.get("value"));
}
这篇关于将serialize数组传递给struts2动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文