JSON Jquery to Struts2 action(JSON Jquery 到 Struts2 动作)
问题描述
我想将我的 JSON 对象从 Javscript 发送到 Struts2 Action.
I want to send my JSON object from Javscript to Struts2 Action.
示例 JSON 对象
{
"lists":["list1","list2","list3","list4","list5"],
"maps": {
"key4":"value4","key3":"value3","key5":"value5","key2":"value2","key1":"value1"
},
"number1":123456789,
"numberarray1":[1,2,3,4,5,6,7,8,9],
"string1":"A",
"stringarray1":["A1","B1"]
}
我的 Jquery Ajax
My Jquery Ajax
$.ajax({
type: 'POST',
url: 'json/JSON.action',
data: JSON.stringify(data),
dataType: 'json',
async: false ,
contentType: 'application/json; charset=utf-8',
success: function(){window.alert('Done');}
});
Struts.xml 配置
Struts.xml config
<action name="JSON" class="com.actions.json.JsonAction" method="getJSON">
<result type="json"/>
</action>
我的行动课
public class JsonAction extends ActionSupport {
private String data;
public String getJSON() {
return ActionSupport.SUCCESS;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
我的问题是如何在动作类中接收 JSON 对象.
My Problem is how to receive the JSON Object in Action Class.
注意:POST OF JSON 对象是成功的.我只是不知道如何通过 Action Class 接收它.请帮助谢谢
NOTE: POST OF JSON object is successful.. I just don't know how to receive it via Action Class.. PLEASE HELP Thank you
推荐答案
- 您的
struts.xml
条目中有错字 - 您是否在
struts.xml
中定义了切片结果和拦截器.请查看此链接 - 您发送到服务器的 json,不包含任何
data
键.所以它将始终为空.由于 json 被表示为对象.您需要通过这种方式将 JSON 转换为 Java 对象.
- There is a typo in your
struts.xml
entry - Have you defined tiles result and interceptor in
struts.xml
. Please see this link - The json you are sending to the server, doesn't contain any
data
key. So it will be always null. Since json is denoted as objects. You need to convert JSON into Java objects in this way.
方法一.
为 lists,maps,number1,numberarray1,string1
等创建 setter.在这个链接的顶部,定义了这样做的方式.然后就可以通过这种方式访问所有的变量了.
Create setters for lists,maps,number1,numberarray1,string1
and so on. In the top of this link, is defined the way to do it. Then you can access all the variables in this way.
方法 2.在您的 javascript 中定义一个新对象.
Approach 2. In your javascript define a new object.
var sentData ={};
sentData ["sentData "] = data;
// And in your ajax call ,
data: JSON.stringify(sentData),
在你的 action 类中,为此创建 getter 和 setter.
And in your action class, create getters and setters for this.
Map<K.V> sentData = new HashMap<K,V>();
这将为您提供整个 json 对象作为地图.
This will give you whole json object as a Map.
这篇关于JSON Jquery 到 Struts2 动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSON Jquery 到 Struts2 动作
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01