开发者

Struts 2 json annotation

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:

<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 http://stikiflem开发者_运维百科.wordpress.com/2008/08/27/struts-2-json-sample/

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

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. :(


A similar question was asked here: Struts2 JSON Plugin With Annotations

I got your action working by annotating it as follows:

@ParentPackage("json-default")
@Result(name="success", type="json")
public class JsonAction extends ActionSupport {


Get the JAR Dependencies

   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>2.3.20</version>
</dependency>
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-json-plugin</artifactId>
    <version>2.3.20</version>
</dependency>

Convention Plugin

The Convention Plugin is bundled with Struts since 2.1 and replaces the Codebehind Plugin and Zero Config plugins. It provides the following features :

Action location by package naming conventions

Result (JSP, FreeMarker, etc) location by naming conventions

Class name to URL naming convention

Package name to namespace convention

Action name overrides using annotations

Namespace overrides using annotations

XWork package overrides using annotations

Set Parent Package

Using annotation set the package as json-default to support the JSON.

 @ParentPackage("json-default")

Set Result Type

  @Result(name="success", type="json")

Define filter in web.xml

Define the struts 2 filter in web.xml and pass the action class by defining actionPackages.

Action Class

In this class data converted into JSON format.

   @Result(name = "success", type = "json")
   @ParentPackage("json-default")
   public class StrutsJsonAnnotationAction extends ActionSupport {

private static final long serialVersionUID = 3516335522937177571L;
private String name = "Narendra Modi";
private String designation = "Prime Minister of India";
private String dob = "17 September 1950";
private String[] education = {"MA", "BA"};
private List<String> favBooks = new ArrayList<String>();
private Map<String, String> assumedOffice = new HashMap<String, String>();

public StrutsJsonAnnotationAction() {

    favBooks.add("Ramayan");
    favBooks.add("Geeta");

    assumedOffice.put("President", "Pranab Mukherjee");
    assumedOffice.put("Preceded by", "Manmohan Singh");
}

@org.apache.struts2.convention.annotation.Action("/india")
@Override
public String execute() {
    return SUCCESS;
}

Source: http://www.websparrow.org/struts/struts2-and-json-integration-using-annotation-example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜