How is it possible to use validation/workflow interceptors in combination with
fileUpload interceptor?
For the config below, if I submit an html form that includes the file tag, struts returns a blank white page instead of the intended success result. This occurs regardless of whether form is filled out correctly or not. I would assume struts is looking for an input result since there was a file upload error, assuming, though?
From log statements the validate() method is executed but the execute() method is not, despite the form being filled out properly. The File object is even available in validate() and has the correct info (fileName, contentType), I can even see the temp file since it’s not delete since execute() does not run. What is the proper interceptor stack configuration for using validation, workflow, and fileUpload?
I have tried setting each interceptor manually, in a number of different stack positions, but it seems like file upload cancels out validation and workflow.
btw, if the same form is submitted without a file then the action behaves as expected.
The blank page problem occurs when the FileUploadInterceptor detects a file. Also, I have tried to use SaveItem-validation.xml instead of manual validate() with the same results but I also have a specific reason to use the manual validate().
Any suggestions to get the fileUpload interceptor to return either the success result or input result instead of the blank page.
-- jars in lib;
struts2-core 2.0.11.2
commons-fileupload 1.1.1
commons-io 1.0
-- struts-xml
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="tokenPrepare!*" class="com.sandplains.view.action.SaveItem" method="{1}">
<result name="input">WEB-INF/jsp/post-item.jsp</result>
</action>
<action name="saveitem" class="com.sandplains.view.action.SaveItem">
<interceptor-ref name="token"/>
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<result name="input">WEB-INF/jsp/post-item.jsp</result>
<result name="invalid.token">/error/dbl-click-post-item.jsp</result>
<result>WEB-INF/jsp/results-save-item.jsp</result>
<result name="failure">/error/error.jsp</result>
</action>
</package>
</struts>
-- struts.properties
struts.multipart.parser=jakarta
struts.multipart.saveDir=c:/tmp
struts.multipart.maxSize=1000000
-- jsp
<s:form name="savefrm" action="saveitem.does" method="post"
enctype="multipart/form-data">
<s:token/>
<s:textarea label="Desc" name="description" cols="35" rows="4"
required="true"/>
<s:file name="upload" label="Picture"/>
<s:submit name="submit1" align="left"/>
</s:form>
action
public class SaveItem extends ActionSupport {
private static final long serialVersionUID = 1L;
static Logger log = Logger.getLogger(SaveItem.class);
private String description;
private File file;
private String contentType;
private String filename;
public String execute() throws Exception {
if (AppConstants.DEBUG) {
log.info("description: " + description);
log.info("contentType: " + contentType);
log.info("filename: " + filename);
}
// do stuff...
return SUCCESS;
}
public void validate() {
log.info("filename: " + filename);
log.info("contentType: " + contentType);
//
// Validate fields.
//
if (description != null && description.trim()..length()
> 0) {
if (description.length() > 5000) {
addFieldError("description",
"Description
must not be more than 5000 characters, including whitespace..");
}
} else {
addFieldError("description", "Description is a
required field.");
}
}
public void setUpload(File file) {
this.file = file;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
public void setUploadFileName(String filename) {
this.filename = filename;
}
}