Struts 2 and JSON Integration Example

In this Struts 2 tutorial, we get the JSON response through our Struts 2 application. JSON is the most popular way to data-interchange.

In this example, we are trying to make the code super simple for easy to understand.

Software Used

  1. Eclipse IDE
  2. Tomcat 8
  3. JDK 8

Get the JAR Dependencies


struts2-json-plugin-2.x.x.jar file allows you to serialize the Action class attribute which has setters into a JSON object.

Project Structure in Eclipse

Struts 2 and JSON Integration Example

Add Struts 2 Filter

Define the struts 2 filters into web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns=""
	xsi:schemaLocation="" id="WebApp_ID" version="3.1">

Create Action Class

In this class data converted into JSON format.
package org.websparrow;

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

import com.opensymphony.xwork2.Action;

public class StrutsAndJsonAction {

	private String name = "Atul Rai";
	private String[] education = { "MCA", "BCA" };
	private int mobileNumber = 123456789;
	private int[] favoriteNumber = { 11, 22, 33 };
	private Map<String, String> marks = new HashMap<String, String>();
	private List<String> books = new ArrayList<String>();

	public StrutsAndJsonAction() {
		marks.put("Java", "72");
		marks.put("C", "58");
		marks.put("C++", "83");
		marks.put("PHP", "66");

	public String execute() {
		return Action.SUCCESS;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String[] getEducation() {
		return education;

	public void setEducation(String[] education) { = education;

	public int getMobileNumber() {
		return mobileNumber;

	public void setMobileNumber(int mobileNumber) {
		this.mobileNumber = mobileNumber;

	public int[] getFavoriteNumber() {
		return favoriteNumber;

	public void setFavoriteNumber(int[] favoriteNumber) {
		this.favoriteNumber = favoriteNumber;

	public List<String> getBooks() {
		return books;

	public void setBooks(List<String> books) {
		this.books = books;

	public Map<String, String> getMarks() {
		return marks;

	public void setMarks(Map<String, String> marks) {
		this.marks = marks;

Map Action Class

Map your action class in struts.xml. You will need to set package extends="json-default" result type is json.

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<constant name="struts.devMode" value="true" />
	<package name="default" namespace="/" extends="json-default">
		<action name="myjson" class="org.websparrow.StrutsAndJsonAction">
			<result type="json" />


To run the application start your server and hit this URL in address bar http://localhost:8081/Struts2JsonIntegration/myjson.action. To get the JSON data in the arranged form use the Chrome Postman.

Struts 2 and JSON Integration Example


  1. JSON Plugin

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.