Struts2 Field Error key created by FileUploadInterceptor required
-
20-06-2021 - |
Question
I am using Struts2 - FileUploadInterceptor to handle content type and maximum size of the file being uploaded.
In order to display error messages, <s:fielderror/>
is displaying error message which is perfect.
I am working on a page which has multiple sections and i need to display error messages belonging only to that section, problem with <s:fielderror/>
is that it displays all error messages and to solve this parameters are to be passed, like
<s:fielderror><s:param>firstName</s:param></s:fielderror>
I am not sure what is the error key created by FileUploadInterceptor for content type error and maximum size error, please help me in addressing this issue.
Solution
If I understand you correctly, I think you want to do something like this:
<s:fielderror>[some reference to a content type error]</s:fielderror>
<s:fielderror>[some reference to a max size error]</s:fielderror>
so that you can render them separately/differently.
This is currently not possible since <s:fielderror/>
only takes field names.
You cannot separate the multiple errors for one field in this way.
Additionally, looking at the source code for the acceptFile
method in the FileUploadInterceptor
, the max size is checked first and an error is set if that check fails. The code doesn't continue to check for the allowed types if the max size already failed.
353 } else if (maximumSize != null && maximumSize < file.length()) {
354 String errMsg = getTextMessage(action, "struts.messages.error.file.too.large", new Object[]{inputName, filename, file.getName(), "" + file.length()}, locale);
355 if (validation != null) {
356 validation.addFieldError(inputName, errMsg);
357 }
358
359 LOG.warn(errMsg);
360 } else if ((!allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {
361 String errMsg = getTextMessage(action, "struts.messages.error.content.type.not.allowed", new Object[]{inputName, filename, file.getName(), contentType}, locale);
362 if (validation != null) {
363 validation.addFieldError(inputName, errMsg);
364 }
365
366 LOG.warn(errMsg);
367 }
So you should only get one of these errors at a time.