struts2 s:form element trims the s:url parameter in the action attribute
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?
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>
May be you could use a wildcard mapping
<action name="removeAction*" class="..">
</action>
And pass the id as part of the url it self.eg: removeAction/101
Refer http://struts.apache.org/release/2.3.x/docs/wildcard-mappings.html
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>
链接地址: http://www.djcxy.com/p/41102.html