user2962615
user2962615

Reputation: 31

How to post an Array of object in json to Struts2 action

I am posting JSON request to Struts2 application. The json request has array of values. Here is the JSON reqeust:

{"row":"10","col":"10","data":[{"word":"word1","clue":"clue1"},{"word":"word2","clue":"clue2"}]}

JQuery code :

jasonRequest = createpuzzle_createjson();

$.ajax({
    type: 'POST',
    url:'create.action',
    dataType: 'json',
    data: jasonRequest,
    success: function(data){
        console.log(stringify(data));
    }
});

Action Class:

public class GenerateCWAction extends ActionSupport{

private String row;
private String col;
private WCMap[] data;

public String getRow() {
    return row;
}
public void setRow(String row) {
    this.row = row;
}
public String getCol() {
    return col;
}
public void setCol(String col) {
    this.col = col;
}
public WCMap[] getData() {
    return data;
}
public void setData(WCMap[] data) {
    this.data = data;
}
public String execute() {
System.out.println("getRow:" + getRow());   
System.out.println("getCol:" + getCol());
System.out.println("getData:" + getData());
return SUCCESS;
}
}

WCMap class:

public class WCMap {
private String word;
private String clue;
public String getWord() {
    return word;
}
public void setWord(String word) {
    this.word = word;
}
public String getClue() {
    return clue;
}
public void setClue(String clue) {
    this.clue = clue;
}

Output:

getRow:10
getCol:10
getData:null

I want to retrieve the array data

"data":[{"word":"word1","clue":"clue1"},{"word":"word2","clue":"clue2"}]

Also, I tried to change the array to list as below; Still i got getData:null

private WCMap[] data;

to

private List<WCMap> data;

Can you please help me to figure this out.

Upvotes: 3

Views: 3447

Answers (2)

Waqar
Waqar

Reputation: 426

This answer is for future googler's like me -

1. Create a Interceptor stack

<interceptors>
        <interceptor-stack name="jsonStack">
            <interceptor-ref name="json">
                <param name="enableSMD">true</param>
            </interceptor-ref>
        </interceptor-stack>
    </interceptors>  

2. Use this interceptor for your json action

<action name="youraction" class="your class" method="methodName">
            <interceptor-ref name="jsonStack"></interceptor-ref>
            <result type="json" />
        </action>

3. Ajax call

var ajaxData = {};
    ajaxData["array"] = [//your data]; // e.g ["data1","data2"];
    $.ajax( {
        "dataType": 'json',
        "type": "POST", 
        "url": 'youraction.action',
        "data":  JSON.stringify(ajaxData),
        contentType: "application/json; charset=utf-8",
        async : false,
        success: function (json) {
            console.log('success  :'+json);
        },
        complete: function (msg,a,b) {
            console.log('complete :'+msg); 
        },
        error : function(msg,a,b){
            console.log('error:'+msg);
        }
    } );

4. Create getters and setters for array in your action class method

List<String> array = new ArrayList<String>();

    public List<String> getArray() {
        return array;
    }
    public void setArray(List<String> array) {
        this.array = array;
    }

Upvotes: 4

coding_idiot
coding_idiot

Reputation: 13734

Make WCMap serializable

public class WCMap implments Serializable{
//...
}

Upvotes: 1

Related Questions