java JSON Jquery 到 Struts2 动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12746051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:02:56  来源:igfitidea点击:

JSON Jquery to Struts2 action

javajqueryjsonstruts2

提问by newbie

I want to send my JSON object from Javscript to Struts2 Action.

我想将我的 JSON 对象从 Javscript 发送到 Struts2 Action。

Sample JSON Object

示例 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"]
    }

My Jquery Ajax

我的 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 config

Struts.xml 配置

<action name="JSON" class="com.actions.json.JsonAction" method="getJSON">
    <result type="json"/>
</action>   

My Action Class

我的行动课

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;
    }



}

My Problem is how to receive the JSON Object in Action Class.

我的问题是如何在操作类中接收 JSON 对象。

NOTE: POST OF JSON object is successful.. I just don't know how to receive it via Action Class.. PLEASE HELP Thank you

注意:POST OF JSON 对象是成功的.. 我只是不知道如何通过 Action Class 接收它.. 请帮助谢谢

采纳答案by Ashish Gupta

  1. There is a typo in your struts.xmlentry
  2. Have you defined tiles result and interceptor in struts.xml. Please see this link
  3. The json you are sending to the server, doesn't contain any datakey. So it will be always null. Since json is denoted as objects. You need to convert JSON into Java objects in this way.
  1. 您的struts.xml输入有错别字
  2. 您是否在struts.xml. 请看这个链接
  3. 您发送到服务器的 json 不包含任何data密钥。所以它将始终为空。由于 json 被表示为对象。您需要通过这种方式将 JSON 转换为 Java 对象。

Approach 1.

方法一。

Create setters for lists,maps,number1,numberarray1,string1and 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.

创建 setterlists,maps,number1,numberarray1,string1等等。在此链接的顶部,定义了执行此操作的方法。然后你可以通过这种方式访问​​所有变量。

Approach 2.In your javascript define a new object.

方法 2.在您的 javascript 中定义一个新对象。

 var sentData ={};
 sentData ["sentData "] = data;
// And in your ajax call , 
data: JSON.stringify(sentData),

And in your action class, create getters and setters for this.

在您的操作类中,为此创建 getter 和 setter。

Map<K.V> sentData = new HashMap<K,V>();

This will give you whole json object as a Map.

这将为您提供整个 json 对象作为 Map。