You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by lu...@maggioli.it on 2012/07/27 15:47:35 UTC
Clear field error on submit
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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Rif: Re: RE: Clear field error on submit
Posted by lu...@maggioli.it.
Hi Dave,
no I'm not using Spring.
Dave Newton
<davelnewton@gmai
l.com> Per
Struts Users Mailing List
27/07/12 16.06 <us...@struts.apache.org>
CC
Per favore, Oggetto
rispondere a Re: RE: Clear field error on submit
"Struts Users
Mailing List"
<user@struts.apac
he.org>
Are you defining your actions in the Spring config file (which isn't
necessary)?
If so, make sure they're scoped "prototype".
Dave
On Fri, Jul 27, 2012 at 10:03 AM, <lu...@maggioli.it> wrote:
> Yes. I think this is the problem.
> Struts2 "Stream result type" doesn't refresh page.
>
> Some suggestions?
> Thanks
>
>
>
>
> Puneet Babbar 2
> <pbabbar2@sapient
> .com>
Per
> Struts Users Mailing List
> 27/07/12 15.52 <us...@struts.apache.org>
>
CC
>
> Per favore,
Oggetto
> rispondere a RE: Clear field error on submit
> "Struts Users
> Mailing List"
> <user@struts.apac
> he.org>
>
>
>
>
>
>
> My take on your problem - Your page isn't getting refreshed so the field
> errors remain.
>
> Regards
>
> Puneet Babbar
>
>
>
> -----Original Message-----
> From: luca.zoffoli@maggioli.it [mailto:luca.zoffoli@maggioli.it]
> Sent: Friday, July 27, 2012 7:18 PM
> To: Struts Users Mailing List
> Subject: Clear field error on submit
>
>
> 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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: RE: Clear field error on submit
Posted by Dave Newton <da...@gmail.com>.
Are you defining your actions in the Spring config file (which isn't
necessary)?
If so, make sure they're scoped "prototype".
Dave
On Fri, Jul 27, 2012 at 10:03 AM, <lu...@maggioli.it> wrote:
> Yes. I think this is the problem.
> Struts2 "Stream result type" doesn't refresh page.
>
> Some suggestions?
> Thanks
>
>
>
>
> Puneet Babbar 2
> <pbabbar2@sapient
> .com> Per
> Struts Users Mailing List
> 27/07/12 15.52 <us...@struts.apache.org>
> CC
>
> Per favore, Oggetto
> rispondere a RE: Clear field error on submit
> "Struts Users
> Mailing List"
> <user@struts.apac
> he.org>
>
>
>
>
>
>
> My take on your problem - Your page isn't getting refreshed so the field
> errors remain.
>
> Regards
>
> Puneet Babbar
>
>
>
> -----Original Message-----
> From: luca.zoffoli@maggioli.it [mailto:luca.zoffoli@maggioli.it]
> Sent: Friday, July 27, 2012 7:18 PM
> To: Struts Users Mailing List
> Subject: Clear field error on submit
>
>
> 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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Rif: RE: Clear field error on submit
Posted by lu...@maggioli.it.
Yes. I think this is the problem.
Struts2 "Stream result type" doesn't refresh page.
Some suggestions?
Thanks
Puneet Babbar 2
<pbabbar2@sapient
.com> Per
Struts Users Mailing List
27/07/12 15.52 <us...@struts.apache.org>
CC
Per favore, Oggetto
rispondere a RE: Clear field error on submit
"Struts Users
Mailing List"
<user@struts.apac
he.org>
My take on your problem - Your page isn't getting refreshed so the field
errors remain.
Regards
Puneet Babbar
-----Original Message-----
From: luca.zoffoli@maggioli.it [mailto:luca.zoffoli@maggioli.it]
Sent: Friday, July 27, 2012 7:18 PM
To: Struts Users Mailing List
Subject: Clear field error on submit
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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
RE: Clear field error on submit
Posted by Puneet Babbar 2 <pb...@sapient.com>.
My take on your problem - Your page isn't getting refreshed so the field errors remain.
Regards
Puneet Babbar
-----Original Message-----
From: luca.zoffoli@maggioli.it [mailto:luca.zoffoli@maggioli.it]
Sent: Friday, July 27, 2012 7:18 PM
To: Struts Users Mailing List
Subject: Clear field error on submit
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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Clear field error on submit
Posted by Maurizio Cucchiara <mc...@apache.org>.
I suppose b/c your browser doesn't change the page and b/c the content type.
You should try to break your workflow in the following way:
* authentication
* link to download
* download
Twitter :http://www.twitter.com/m_cucchiara
G+ :https://plus.google.com/107903711540963855921
Linkedin :http://www.linkedin.com/in/mauriziocucchiara
Maurizio Cucchiara
On 27 July 2012 15:47, <lu...@maggioli.it> wrote:
> Why?
Re: Clear field error on submit
Posted by Dave Newton <da...@gmail.com>.
How are you creating your actions? Are you using Spring?
Dave
(pardon brevity, typos, and top-quoting; on cell)
On Jul 27, 2012 9:48 AM, <lu...@maggioli.it> wrote:
>
> 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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>