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

pom.xml
<dependencies>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-core</artifactId>
		<version>2.1.8</version>
	</dependency>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-json-plugin</artifactId>
		<version>2.1.8</version>
	</dependency>
</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.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
	<display-name>Struts2JsonIntegration</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

Create Action Class

In this class data converted into JSON format.

StrutsAndJsonAction.java
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() {
		books.add("Java");
		books.add("C");
		books.add("C++");
		books.add("PHP");
		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) {
		this.name = name;
	}

	public String[] getEducation() {
		return education;
	}

	public void setEducation(String[] education) {
		this.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.

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

Output:

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

References

  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.