Struts 2 CSV Download

Struts 2 CSV Download

Here you will see sample example how to create and download CSV file in Struts 2 application. We will create action class with one method where we will create CSV file and include them in response.

Please see previous tutorial to create report in below format:

  • Maven Struts2CSVDownload project structure:

Struts 2 CSV Download

  • web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Archetype Created Web Application</display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>struts.devMode</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
            <param-name>struts.custom.i18n.resources</param-name>
            <param-value>ApplicationResources</param-value>
        </init-param>				
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>
  • ApplicationResources_en.properties
label.welcome = Struts 2 CSV Download Example
  • Struts2CSVDownloadAction.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 Struts2CSVDownloadAction{

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

		HttpServletResponse response = ServletActionContext.getResponse();
		
		try {
			response.setContentType("text/csv");
			String reportName = "JavaHonk.csv";
			response.setHeader("Content-disposition", "attachment;filename="+reportName);
	 
			ArrayList<String> rows = new ArrayList<String>();
			rows.add("Name,Result");
			rows.add("\n");
	 
			for (int i = 0; i < 10; i++) {
				rows.add("Java Honk,Success");
				rows.add("\n");
			}
	 
			Iterator<String> iter = rows.iterator();
			while (iter.hasNext()) {
				String outputString = (String) iter.next();
				response.getOutputStream().print(outputString);
			}
	 
			response.getOutputStream().flush();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
  •  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 CSV Download Example</title>
<s:head />
</head>

<body>
	<h2 style="color: green">
		<s:text name="label.welcome" />
	</h2>
	<s:form method="post" action="downloadCSVFileAction">
		<s:submit value="Download CSV" />
	</s:form>
</body>
</html>
  • Run on any sever. You will see below output click Download CSV to download file:

Struts 2 CSV Download Struts 2 CSV Download

 

Struts 2 CSV Download

  • For more details please read Struts 2 official documentation here

download  Download Project:  Struts2CSVDownload

One thought on “Struts 2 CSV Download”

Leave a Reply

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