Home >> Struts
Struts Framework and Stream Result type configuration for the
Action to send binary stream to the browser as PDF content type
In this example I shall be showing step by step ways to follow
through various pieces of code to process or achieve an objective
of presenting binary content type to browser based client and
this is presented to the client with the same name as mentioned
in struts.xml configuration file.
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>
<package name="stream-example" namespace="/sample-stream"
extends="struts-default">
<action name="test" class="sample.ExampleStreamAction"
method="preview">
<result name="test-stream" type="stream">
<param name="inputName">fileStream</param>
<param name="contentType">application/pdf</param>
<param name="contentDisposition">filename="test.pdf"</param>
</result>
</action>
</package>
</struts>
|
Action class "ExampleStreamAction.java" is having a preview method
that assigns an input stream to a private instance variable, this
variable is mentioned as the inputName param value to the result
tag.
In this xample, the inputName param is having value as fileStream
, that is defined as an instance variable in Action class.
ExampleStreamAction
package sample;
import java.io.*;
public class ExampleStreamAction
{
private InputStream fileStream;
public void setFileStream(InputStream arg) {
fileStream = arg;
}
public InputStream getFileStream() {
return fileStream;
}
public String preview() throws Exception {
try{
fileStream = new DataInputStream(
new FileInputStream("c:/sources/test.pdf"));
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
return "test-stream";
}
}
|
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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>Struts 2 Stream Result type</display-name>
<filter>
<filter-name>sample-filter</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sample-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
|
After deploying this example to Jakarta Tomcat web server
and tested this example with a URL as
http://localhost:8080/struts2sample/sample-stream/test
This example application reads the PDF file from the path
mentioned in the Action class and preview method.
This result type definition in struts.xml file sends those to the client
as downloadable file name.
Please write your comments about this example in this form.
As a developer and working on application/projects using
Struts Framework for more than 5 years, I am exploring features
such as various result types available to us for doing certain
things like rendering various output types, with ease.
If you like to share your comment/suggestions/feedback relating to this Page,
you can do so by droping us an email at
usingframeworks @ gmail . com
with the subject line mentioning URL for this Page (i.e,
/struts2-pdf-result.php) or use this
LINK.
As per this website's privacy policy, we never disclose your email id,
though we shall post your comments/suggestions/feedback with
your name (optional) and date on this Page. If you don't want your
comments/suggestions/feedback to be shared in this Page, please
mention so in your email to us. Thank you very much.....
If anything missed out , please let me know at
techienjoy at yahoo . com