Reputation: 309
I've implemented an upload utility using Struts2. I'm already restricting uploading a specific file type by programatically checking in my setFileContentType() method in my action class.
One remaining issue is to display to the user a customized error page in case the uploaded file exceeds the max file size setting.
I've researched this and saw how the validation interceptor should be used along with the returned "input" result. However, i still can't put all the pieces together.
My end goal is the following: If the user attempts to upload a large file, i want to display a new page with my own error message.
Any tips/suggestions?
UPDATE I have the following configuration in my struts.xml:
<action name="FileUpload" class="common.FileUpload">
<interceptor-ref name="fileUpload"/>
<result name="success">common/FileUpload/FileUpload.jsp</result>
<result name="UploadResult">common/FileUpload/FileUploadResult.jsp</result>
</action>
I know the above configuration is missing the validation interceptor in case i want to detect the file size error. The problem is that i'm not sure how that comes into play at this point.
Thanks
Upvotes: 0
Views: 2748
Reputation: 20323
FileUpload has a filesize parameter, you can use that in your configuration
<interceptor-ref name="fileUpload">
<param name="maximumSize">50</param>
</interceptor-ref>
If you want to provide custom message you can set here
struts.messages.error.file.too.large
Occurs when the uploaded file is too large as specified by maximumSize.
Make Your action ValidationAware
and you will be notified if Struts2 encounters this error, your addFieldError
will be invoked to notify you of the error where key
will struts.messages.error.file.too.large
and message that you have defined in properties file, once your addFieldError
is invoked you can take necessary action.
FileUploadInterceptor while uploading files will also run validation on the file or file type, size and if action implements ValidationAware then it will set validation message in that action by calling addFieldError callback method
Upvotes: 1