Tech I Enjoy Logo

Custom Search




  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
Apache Struts 2 Validation With Example :
Validation Example using Struts 2
and Example code explained.
Struts Validation Example :
Example using Validation with Struts 2 and 
code explained.
Apache Struts 2 Tiles and I18N Example :
Example Steps of using Struts 2 With Tiles
and I18N code explained.
Apache Struts with DOJO Example Part-2 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 and XSLT With Example :
XSLT Example using Struts 2 and Example 
code explained.
List of Struts Example :
Examples List using Struts 2 and 
code explained.
Struts 2 with JSF Example :
Example using JSF with Struts 2 and 
code explained.
Struts 2 Result Chain Example :
Example using Result Chain with Struts 2 and 
code explained.
Struts 2 Tiles and I18N Example :
Example using Tiles and I18N with Struts 2 and 
code explained.
Apache Struts 2 Result PlainText Example :
Example Steps of using Struts 2 PlainText Result
and code explained.
Apache Struts 2 upload Example :
Uploading Example Steps of using Struts 2
and Example code explained.
Apache Struts Validation with Example :
Example of Validation using Struts and 
code explained.
Apache Struts 2 Tags Example :
Example of Struts 2 Tags and code explained.
Apache Struts 2 Example Steps :
Example Steps of using Struts 2 and code explained.
Apache Struts with DOJO Example Part-3 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts Date Validation with Example :
Example of Date Validation using Struts and 
code explained.
Apache Struts with DOJO Example Part-4 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Tags Example :
Example Steps of using Struts 2 Tags
and code explained.
Apache Struts 2 Validation Example :
Validation Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 Validation With Expression :
Validation with Expression using Struts 2
and Example code explained.
Apache Struts 2 Custom Validator :
Example Steps of using Struts 2 with Custom Validator
and code explained.
Struts 2 Plugin Example :
Example using Struts 2 Plugin
Apache Struts 2 Radio Tag Example :
Example Steps of using Struts 2 Radio Tags
and code explained.
Apache Struts with DOJO Example Part-1 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 PDF Result :
Example Steps of using Struts 2 with PDF Result
and code explained.


References :
Tags: Struts 1 Struts2 plugin
Tags: struts jsf integrate
Tags: struts result chain
Tags: struts tiles i18n
Tags: Struts validation struggle
Tags: Struts
Tags: struts1 date validation
Tags: struts1 struts2 validation
Tags: struts2 dojo 1
Tags: struts2 dojo 2
Tags: struts2 dojo 3
Tags: struts2 dojo 4
Tags: Struts2 example steps Tag usage
Tags: Struts2 example steps
Tags: struts2 own validator
Tags: struts2 pdf result
Tags: struts2 radio tag example
Tags: struts2 result plaintext
Tags: struts2 tags example
Tags: struts2 tiles i18n
Tags: struts2 upload example
Tags: struts2 validation 1
Tags: struts2 validation expression
Tags: struts2 validation
Tags: struts2 xslt article



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions