Struts 2 json annotation

admin

Administrator
Staff member
Ive been wanting to create a struts 2 with json return type using the annotation configuration. Ive successfully created this using the xml-type configuration like this snippet:

Code:
<action name="FetchJSON" class="com.stikiflem.Json" method="getJSON">
        <result type="json"/>
</action>

I have posted a working demo of using an xml-type config here
<a href="http://stikiflem.wordpress.com/2008/08/27/struts-2-json-sample/" rel="nofollow">http://stikiflem.wordpress.com/2008/08/27/struts-2-json-sample/</a>

But how do I convert this to annotation? Here is my sample class:

Code:
public class JsonAction extends ActionSupport{

private List sampleList;

public String execute() {

    sampleList = new ArrayList();

    sampleList.add("stikiflem sample 1");
    sampleList.add("stikiflem sample 2");
    sampleList.add("stikiflem sample 3");
    sampleList.add("stikiflem sample 4");

    System.out.println("----------------------------------------------");
    System.out.println("----------------------------------------------");
    System.out.println("-sample111List:" + sampleList.toString());
    System.out.println("----------------------------------------------");
    System.out.println("----------------------------------------------");

    return SUCCESS;
}


@Action(value="FetchJSON", results = {
     @Result(name="success", type="json")
 })  

public String getJSON(){
 System.out.println("get jason ko");
 return execute();
}

public List getSampleList() {
    return sampleList;
}

public void setSampleList(List sampleList) {
    this.sampleList = sampleList;
}



}

Tried calling it by "json.action", it triggers the execute() method of course but cannot return a json type. Calling it by "FetchJSON" doesnt do anything. This question sounds stupid but there are just a small amount of tutorials and example of a detailed annotation in the net. Ive read a Manning Struts 2 in action book but it just barely scratch the surface, just the typical hello world and sucess,input redirection.

Ive searched the net high and low and so far, i havent seen any. I know there are a lot of programmers searching for this too.Hope someone can enlighten me about this one. Ive been banging my head on this for days already. :(