Struts 2 JASON Annotation example

In this demo I will show how to return response in JASON format. Here we are using annotation to return data and if you are not using annotation based struts application you could use struts.xml file to configure and return data in JSON format.

Steps:

  • pom.xml:
    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.16.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.16.3</version>
        </dependency>
        
        <!-- JASON Plug-in -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.3.16.3</version>
        </dependency>
    </dependencies>
  • JasonAction.java:
package com.javahonk.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default")
public class JasonAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private Map<String, String> jasonMap = new HashMap<String, String>();
    private List<String> jasonList = new ArrayList<String>();
    private String name;

    public JasonAction() {
        jasonMap.put("Java", "Java");
        jasonMap.put("Honk", "Honk");
        jasonList.add("Java");
        jasonList.add("Honk");
        name = "Java Honk";
    }

    @Override
    @Action(value = "/jasonDataAction", results = 
        { @Result(name = "success", type = "json") })
    public String execute() throws Exception {
        return ActionSupport.SUCCESS;
    }

    public Map<String, String> getJasonMap() {
        return jasonMap;
    }

    public void setJasonMap(Map<String, String> jasonMap) {
        this.jasonMap = jasonMap;
    }

    public List<String> getJasonList() {
        return jasonList;
    }

    public void setJasonList(List<String> jasonList) {
        this.jasonList = jasonList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

  • Note: Struts.xml configuration if you are not using annotation:
<package name="default" namespace="/" extends="json-default">
  <action name="jasonAction" class="com.javahonk.action.JSONAction">
    <result type="json" />
  </action>
</package>
  • Run application:

Struts 2 JASON Annotation exampleStruts 2 JASON Annotation exampleFor more information please read this tutorial

download2 Download Project: Struts2JASON

 

Leave a Reply

Your email address will not be published. Required fields are marked *