Struts 2 XML Download

Struts 2 XML Download

In this demo you will see how to create XML report on server side and send XML response back to the client so that he could view/download report in his local system. To keep this demo easy to understand I have created XML using core java API but it is up to you to use any third party or other java API to create XML file write content in response.

Please see previous tutorial to create report in below format:

  • Create maven project Struts2XMLDownload in eclipse or you could download from bottom and import in eclipse. Below if final project structure:

Struts 2 XML Download

  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javahonk</groupId>
	<artifactId>Struts2XMLDownload</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Struts2XMLDownload Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<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>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>		
	</dependencies>
	<build>
		<finalName>Struts2XMLDownload</finalName>
	</build>
</project>
  • ApplicationResources_en.properties:
label.welcome = Struts 2 XML Download Example
  • Struts2XMLDownloadAction.java:
package com.javahonk.action;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;


public class Struts2XMLDownloadAction{

	@Action(value = "/downloadXMLFileAction")
	public void downloadCSVFileAction() throws Exception {

	    HttpServletResponse response = ServletActionContext.getResponse();
	    response.setContentType("text/xml");
		String reportName = "JavaHonk_Struts2_XML_Report_.xml";
		response.setHeader("Content-disposition", "attachment;filename="+ reportName);
 
		ArrayList<String> rows = new ArrayList<String>();
		rows.add("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		rows.add("<Details>");
		for (int i = 0; i < 4; i++) {
			rows.add("<Person" + (i + 1) + ">");
			rows.add("<FirstName>");
			rows.add("Java");
			rows.add("</FirstName>");
			rows.add("<LastName>");
			rows.add("Honk");
			rows.add("</LastName>");
			rows.add("<Test>");
			rows.add("Success");
			rows.add("</Test>");
			rows.add("</Person" + (i + 1) + ">");
 
		}
		rows.add("</Details>");
 
		Iterator<String> iter = rows.iterator();
		while (iter.hasNext()) {
			String outputString = (String) iter.next();
			response.getOutputStream().print(outputString);
		}
 
		response.getOutputStream().flush();
	}

}
  • index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Struts 2 XML Download Example</title>
<s:head />
</head>

<body>
	<h2 style="color: green">
		<s:text name="label.welcome" />
	</h2>
	<s:form method="post" action="downloadXMLFileAction">
		<s:submit value="Download XML" />
	</s:form>
</body>
</html>
  • Download and run this project on any server below will be output:

Struts 2 XML Download Struts 2 XML Download Struts 2 XML Download

  • Fork out your own copy from GitHub

download  Download Project: Struts2XMLDownload

Leave a Reply

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