Friday, July 27, 2012

Struts2 Programs 006 Stream Result

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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="kites" extends="struts-default">
<action name="resultStream">
<result name="success">/jsp/page.jsp</result>
</action>

<action name="download" class="com.kites.StreamResultExample">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="testFile.txt"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>

</struts>
----------------------------------------------------------------------------------------------------------------------------------------
web.xml
--------------
<?xml version="1.0" encoding="UTF-8"?>

<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>Struts 2</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.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
--------------------------------------------------------------------------------------------------------------------------------------
StreamResultExample.java
-------------------------------------------
package com.kites;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

public class StreamResultExample extends ActionSupport{

private InputStream fileInputStream;

public InputStream getFileInputStream() {

return fileInputStream;
}
public String execute() throws Exception {
fileInputStream = new FileInputStream(new File("downloadfile.txt"));

return SUCCESS;

}

}
----------------------------------------------------------------------------------------------------------------------------------
page.jsp
------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Chain Result Example</title>



</head>
<body>
<font color="megenta" size="6" face="Comic Sans MS">Stream Result example</font><br clear="all"/><br clear="all"/>

<s:url action="download" id="copy"></s:url>

<strong><font color="green" size="5" face="arier">Click on <s:a href="%{copy}">file</s:a> to Download</font></strong>

</body>
</html>

--------------------------------------------------------------------------------------------------------------------------------
downloadfile.txt
------------------------------

Struts2 is popular and mature web application framework based on the MVC design pattern. Struts2 is not just the next version of Struts 1, but it is a complete rewrite of the Struts architecture.

The WebWork framework started off with Struts framework as the basis and its goal was to offer an enhanced and improved framework built on Struts to make web development easier for the developers.

After some time, the Webwork framework and the Struts community joined hands to create the famous Struts2 framework.

---------------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment