2

I am using Struts 2.3.24.1 and after switching to the jakarta-stream implementation because of the bug mentioned here, I discovered a new error when the file upload field in my form is empty. My form looks like this (abridged):

<s:form enctype="multipart/form-data" method="POST" action="persistAddNote" id="noteForm">
    <s:token/>
    <s:file name="fileUpload" size="79"/>
    <s:if test="hasActionErrors()">
        <s:property value="%{#request.uploadError}" escape="false"/>
    </s:if>
    <s:submit cssClass="buttonFormat" value="Save"
              onclick="javascript: disableButtons(); document.noteForm.submit();"/>
</s:form>

The persistAddNote action is configured as follows:

<action name="persistAddNote" class="my.example.action.PersistNoteAction">
    <param name="parameter">my.example.encoder.Latin1ToLatin9Encoder</param>
    <interceptor-ref name="tokenSession"/>
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="staticParams"/>
    <interceptor-ref name="actionMappingParams"/>
    <interceptor-ref name="params"/>
    <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>
    <exception-mapping name="notes.error.filesize"
                       exception="my.example.exception.UploadFileSizeException"
                       result="/notes/error.jsp"/>
    <result name="input">/notes/addnote.jsp</result>
    <result name="added">/notes/newnoteok.jsp</result>
    <result name="novalidaction">/notes/noValidActionError.jsp</result>
</action>  

The error occurs in the JakartaStreamMultiPartRequest.createTemporaryFile(String, String) method because no filename is set. This is to be expected since the file upload field is not required. I tried debugging the Struts code but it looks as if the multipart/form-data enctype always triggers the creation of a temporary file. The error is gone when I switch back to the standard jakarta implementation in the struts.properties but that brings back the issue I mentioned above.

Does anyone have an idea how to fix this issue?

Community
  • 1
  • 1
Sebastian
  • 858
  • 7
  • 21
  • 2
    Looks like a bug. You can report it [here](https://issues.apache.org/jira/browse/WW). – Aleksandr M Jan 06 '16 at 09:27
  • Ok, will do. Thanks. At least my configuration is correct then which is kind of a relief ;-) – Sebastian Jan 06 '16 at 09:34
  • 2
    [WW-4583](https://issues.apache.org/jira/browse/WW-4583). – Aleksandr M Jan 06 '16 at 17:15
  • 2
    Thank you, Aleksandr. I am going to write an answer to my question in order to properly close it. – Sebastian Jan 07 '16 at 08:51
  • @AleksandrM One last question: Will there be a new snapshots deployment containing your fix? I just looked through the repository and the latest snapshot for version 2.3.25 dates back to october. Thanks for your assistance. – Sebastian Jan 07 '16 at 13:04
  • Not sure for the 2.3.25 snapshots. :( You can get a jar from Jenkins build. See my answer for simple workaround. – Aleksandr M Jan 08 '16 at 09:54

2 Answers2

1

As Aleksandr mentioned, it was a bug in the JakartaStreamMultiPartRequest (see WW-4583). The bug has been fixed in version 2.3.28 and above.

Sebastian
  • 858
  • 7
  • 21
1

Until the new version is released you can apply simple JavaScript workaround to fix this issue. Just disable file input field if it is empty prior to submitting.

function checkFileInput() {
    var fileInput = document.getElementById("fileInputId");
    if (fileInput.value.length < 1) {
        fileInput.disabled = true;
    }
}
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143