struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Puneet Babbar 2 <pbabb...@sapient.com>
Subject RE: Clear field error on submit
Date Fri, 27 Jul 2012 13:52:14 GMT
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


Mime
View raw message