Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON Jquery到Struts2操作_Java_Jquery_Json_Struts2 - Fatal编程技术网

Java JSON Jquery到Struts2操作

Java JSON Jquery到Struts2操作,java,jquery,json,struts2,Java,Jquery,Json,Struts2,我想将JSON对象从Javscript发送到Struts2 Action 示例JSON对象 { "lists":["list1","list2","list3","list4","list5"], "maps": { "key4":"value4","key3":"value3","key5":"value5","key2":"value2","key1":"value1" }, "number1":

我想将JSON对象从Javscript发送到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

$.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配置

<action name="JSON" class="com.actions.json.JsonAction" method="getJSON">
    <result type="json"/>
</action>   
我的问题是如何在Action类中接收JSON对象

注意:发布JSON对象成功。。我只是不知道如何通过行动课来接收它。。请帮忙 多谢各位

  • 您的
    struts.xml
    条目中有输入错误
  • 您是否在
    struts.xml
    中定义了tiles结果和拦截器
  • 发送到服务器的json不包含任何
    数据
    键。所以它总是空的。 因为json被表示为对象。您需要以这种方式将JSON转换为Java对象
  • 方法1.

    为列表、映射、number1、numberray1、string1等创建setter。在的顶部,定义了执行此操作的方法。然后您可以通过这种方式访问所有变量

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

     var sentData ={};
     sentData ["sentData "] = data;
    // And in your ajax call , 
    data: JSON.stringify(sentData),
    
    在action类中,为此创建getter和setter

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

    这将为您提供整个json对象作为一个映射。

    您可以共享您的操作类吗。另外,您是否在struts.xml中定义了结果类型json,并在
    coma,actions
    中定义了拦截器
    class=“coma,actions.json.JsonAction”
    typo-in
    coma,actions
    ,所有你需要注意的是json格式和一些小规则,看看插件文档。我的问题是我的数据总是等于null@AshishGupta,感谢您的解释我已经尝试了这个解决方案,我可以看到我的数据在javascript端被正确读取并被正确发送。但是,我无法在Struts操作中看到数据。我和你说的一样。在我的struts.xml中是否有要设置的参数或拦截器?我感觉可能是这样。非常感谢。
    Map<K.V> sentData = new HashMap<K,V>();