Struts 2 JASON Annotation example

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.


  • pom.xml:
        <!-- JASON Plug-in -->
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;

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");
        name = "Java Honk";

    @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) { = 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" />
  • 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 *

I am not Robot *