2

I am using <s:url> to create a URL like removeAction.action?id=10 which should be used by the action attribute in the <s:form> element.

The issue here is when the <s:form> converts to <form> element I can only see the action attribute value as action="/project/removeAction.action". The id parameter is getting trimmed. The result I wanted is action="/project/removeAction.action?id=10"

<s:url var="actionUrl" action="removeAction" includeContext="false">
  <s:param name="id" value="%{id}" /> 
</s:url>

<s:form action="%{actionUrl}" method="post" enctype="multipart/form-data" >
 <div>
  <s:file name="imgUpload"/>
  <s:submit> upload </submit>
 </div>
</s:form>

Recently I upgrade the struts2 core version to 2.3.12 and I am getting this issue. This issue starts after version 2.3.4.1

And I don't want to use a hidden attribute to pass the parameter as this parameter get lost when the file size is big to upload.

Is there any solution for this?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Tivakar
  • 163
  • 6
  • 15
  • Maybe this is your real problem: `And I don't want to use a hidden attribute to pass the parameter as this parameter get lost when the file size is big to upload.`? – Aleksandr M Apr 12 '13 at 08:21

3 Answers3

0

What does it means that the hidden parameter will be lost on file upload with a file too big ? It will be re-read and populated automatically...

  • don't call RemoveAction an action that is actually UPLOADING a file. Call it UploadAction, for the sake of the logic :|

  • it is not a good idea to use Query Parameters in POST requests, they should be used only in GET requests, possibly in the REST way...

  • To prevent the max multipart size exceeded error, put this in Struts.xml:

    <constant name="struts.multipart.maxSize" value="52428800" />
    
  • To tune up the max size (default is 2Mb) for a single file in the fileUpload Interceptor, put this in Struts.xml, in your Stack definition:

    <interceptor-ref name="fileUpload">
        <param name="maximumSize">10485760</param>
    </interceptor-ref>
    

    (with this example, you can upload up to 5 files of 10 MB each one in a row)

  • At last, for all the HTML5 compliant browsers (almost everyone except older IE and some mobile), you can prevent the upload BEFORE IT IS SENT, by checking its size in the onchange event like this:

    <s:file name="imgUpload"/ 
            onchange="javascript:checkFileSize(this);" />
    
    <script>
        const maxFileSize = 10485760; // 10MB
    
        function checkFileSize(fileElement){
            if (fileElement.files[0].size > maxFileSize) {
                var mb = (((fileElement.files[0].size) / 1024)/1024).toFixed(2);
                alert("Max file size exceeded: " + mb + " MegaBytes");
                fileElement.value = '';
            }
        }
    </script>
    
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
0

May be you could use a wildcard mapping

<action name="removeAction\\*" class="..">

</action>

And pass the id as part of the url it self.e.g: removeAction/101

Refer http://struts.apache.org/release/2.3.x/docs/wildcard-mappings.html

Dev Blanked
  • 8,555
  • 3
  • 26
  • 32
-1

That issue happens because org.apache.struts2.components.ServletUrlRenderer.renderUrl() method does not find the action configuration for your action "removeAction" because your URL (#actionUrl) already contains the ".action" suffix.

From struts2 s:form documentation on the action parameter:

Set action name to submit to, without .action suffix

The solution is quite simple: do not use <s:url> but rather:

<s:form action="removeAction?id=%{id}" method="post" enctype="multipart/form-data">
 <div>
  <s:file name="imgUpload"/>
  <s:submit> upload </s:submit>
 </div>
</s:form>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Walid
  • 1,262
  • 11
  • 10
  • Indeed, but looking at renderFormUrl() in ServletUrlRenderer, used by org.apache.struts2.components.Form class, I can see they handle an action name with parameters. I don't see why %{actionUrl} does not cause the "No action found" error, and why my code works for me and not for you. – Walid Apr 11 '13 at 20:47