Hello.

This is a Struts 2 example to show the use of custom result type to
download file.

I have in jsp :
- 1 password textfield
- 1 submit button

If I push submit button without insert a password I correctly obtain a
field error "Field required!".
Then If I set "XYZ" password (correct password), I correctly download
"downloadfile.txt" but
field error remain.

Why?

Thanks

DownloadAction.java

package com.example.common.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{

        private InputStream fileInputStream;
        private String password;

        public InputStream getFileInputStream() {
                return fileInputStream;
        }

        public String getPassword() {
                return password;
        }

        public void setPassword(String password) {
                this.password = password;
        }

        public void validate() {
                if ( (password==null) || (password.trim().equals("")) ) {
                        addFieldError("password", "Field required!");
                }
                if (!password.equals("XYZ") {
                        addFieldError("password", "Wrong password!");
                }
        }

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

downloadPage.jsp


<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 download file example</h1>
        <s:form action="download" method="POST">
                <p><s:textfield name="password" /></p>
                <p><s:submit id="submitButton"/></p>
        </s:form>

</body>
</html>

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.devMode" value="true" />

<package name="default" namespace="/" extends="struts-default">
   <action name="">
        <result name="success">pages/downloadPage.jsp</result>
   </action>

   <action name="download"
class="com.example.common.action.DownloadAction">
        <interceptor-ref name="defaultStack" />
        <result name="success" type="stream">
          <param name="contentType">application/octet-stream</param>
          <param name="inputName">fileInputStream</param>
          <param
name="contentDisposition">attachment;filename="fileABC.txt"</param>
          <param name="bufferSize">1024</param>
        </result>
   </action>
</package>

</struts>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to