3

I am using json plugin to handle json request and response in struts2. I want to send an array data in response in form of json but i am unable to do. At FE I always get an empty array. below is my Action class, struts.xml and response.

TestAction:

public class TestAction {

private String[] sArray = {"1","2"};
private int[] iArray = {1,2};
private String value = "value";

//getter setter
public String[] getSArray() {
    return sArray;
}
public int[] getIArray() {
    return iArray;
}
public String execute() {
return "success";    
}

struts.xml

<struts>
<package name="name" namespace="/" extends="default,json-default">
       <action name="Test" 
            class="com.actions.TestAction">
            <result name="success" type="json">
                <param name="excludeNullProperties">true</param>
                <param name="includeProperties">sArray,iArray,value</param>
            </result>
        </action>
</package>
</struts>

At Front End I get the following response

{"sArray":[],"iArray":[],"value":"value"}
Waqas Ali
  • 1,642
  • 4
  • 32
  • 55
  • 1
    `extends="default` should be `extends="struts-default`, not mandatory (since `json-default` already extends `struts-default`) and not cause of the problem, though. – Andrea Ligios Jul 03 '14 at 08:29
  • thanks for info. I change the param to this sArray and it work I get the value of sArray – Waqas Ali Jul 03 '14 at 08:30
  • And it worked ? Does it work if 1) you put the initialization in the execute() method, or 2) if you change from Array to List, or 3) if you remove exclude and includeproperties ? It should work, we're missing something basic :) Are you SURE your getters and setters are all there and all good ? – Andrea Ligios Jul 03 '14 at 08:35
  • Yeah no problem with Getter Setter. I follow different tutorial and there is no special thing that need to do if we want to send an array. also If i use above way the response is little different. I got response this ["1","2"] where as it should be {"sArray":["1","2"]} – Waqas Ali Jul 03 '14 at 08:39
  • Of course, mine suggestions were shots in the dark, because appearently it should work as is, and your `root` attempt works like that because instead of the whole action, it is serializing only the root object, exactly as requested: http://stackoverflow.com/a/17149414/1654265 – Andrea Ligios Jul 03 '14 at 08:41
  • I have added the getter method above. I also do some changes, I created a list of string and add a value in execute() function but in response I get empty list. I also initialize the iArray with null due to which in response I don't get iArray. – Waqas Ali Jul 03 '14 at 09:59
  • What can be the problem? Is there something necessary to do for parsing list or arrays? like adding some special interceptor? – Waqas Ali Jul 03 '14 at 10:01
  • One more thing in my application I am using CORS and I have to add some headers in the response to work on CORS. – Waqas Ali Jul 03 '14 at 10:10
  • Uhm... can you try the above code in a standalone page, not influenced by this CORS response changes ? – Andrea Ligios Jul 03 '14 at 10:21
  • Yeah I try It but same problem. I get an empty array. – Waqas Ali Jul 03 '14 at 10:27
  • One other thing I am using struts2-core-2.2.1.jar library – Waqas Ali Jul 03 '14 at 10:36
  • All the versions (struts-core, struts-json, and so on) must match. Also I **strongly** suggest you to upgrade all of them to latest version, or at least to 2.3.15.3, to avoid dangerous security problems – Andrea Ligios Jul 03 '14 at 12:08
  • I can't update the struts2-core but I can update the struts2-json plugin – Waqas Ali Jul 03 '14 at 12:29
  • I update the struts2-json-plugin but issue still there – Waqas Ali Jul 03 '14 at 12:37

1 Answers1

2

First of all don't name your object fields with single lower case letter (e.g. sArray). Change names to something different (e.g. strArray). In this way you don't need to guess what getters/setters names should be.

Second, the correct syntax for the includeProperties attribute of struts2-json-plugin for arrays is ^array\[\d+\].

So if your fields are strArray and intArray the configuration should be something like that:

<package name="name" namespace="/" extends="json-default">
  <action name="Test" 
      class="com.actions.TestAction">
    <result name="success" type="json">
      <param name="excludeNullProperties">true</param>
      <param name="includeProperties">^strArray\[\d+\],^intArray\[\d+\],value</param>
    </result>
  </action>
</package>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143