How to override struts 2.x file upload error messages?
In my previous tutorial I have explained about how to upload file in struts 2 web application. This article aims at overriding the default file upload error message which are added by file upload interceptor during file upload validation. These error messages are stored in the struts-messsages.properties file. The values of the messages can be overridden by providing the text for the following keys:
struts.messages.error.uploading - error when uploading of file fails
struts.messages.error.file.too.large - error occurs when file size is large
struts.messages.error.content.type.not.allowed - when the content type is not allowed
struts.messages.error.file.extension.not.allowed - when the file extension is not allowed
** UPDATE: Struts 2 Complete tutorial now available here.
In-order to override these message, you need follow the below steps.
1. Create a global.properties file in the src folder and add the following test to your global.properties file:
struts.messages.error.file.too.large = Uploaded File size is too large
struts.messages.error.uploading = File upload failed
struts.messages.error.content.type.not.allowed =File type is not allowed.
struts.messages.error.file.extension.not.allowed =File extension is not allowed.
2. Add the constant In struts.xml:
<constant name="struts.custom.i18n.resources" value="global" />
After implementing these changes restart the application, and now in case of error in validation you get the custom error message which we have written in the properties file.