Class DynamicFileUploadAction
java.lang.Object
org.apache.struts2.ActionSupport
org.apache.struts2.showcase.fileupload.DynamicFileUploadAction
- All Implemented Interfaces:
Serializable,Action,UploadedFilesAware,ValidationAware,LocaleProvider,Preparable,TextProvider,Validateable
public class DynamicFileUploadAction
extends ActionSupport
implements Preparable, UploadedFilesAware
Demonstrates dynamic file upload validation using WithLazyParams. This action shows how file upload validation rules can be determined at runtime based on action properties, session data, or other dynamic values.
The validation parameters (allowedTypes, allowedExtensions, maximumSize) are set dynamically in the prepare() method and then referenced in struts.xml using ${...} expressions. This allows the same action to enforce different validation rules based on runtime conditions.
This example demonstrates two use cases:
- Document Upload: Accepts PDF and Word documents up to 5MB
- Image Upload: Accepts JPEG and PNG images up to 2MB
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classConfiguration holder for dynamic file upload validation rules. -
Field Summary
Fields inherited from class org.apache.struts2.ActionSupport
container -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the upload configuration object.longinput()voidprepare()This method is called to allow the action to prepare itself.voidvoidsetUploadType(String uploadType) upload()voidwithUploadedFiles(List<UploadedFile> uploadedFiles) Notifies action about the multiple uploaded files, when a single file is uploaded the list will have just one elementMethods inherited from class org.apache.struts2.ActionSupport
addActionError, addActionMessage, addFieldError, clearActionErrors, clearErrors, clearErrorsAndMessages, clearFieldErrors, clearMessages, clone, execute, getActionErrors, getActionMessages, getContainer, getFieldErrors, getFormatted, getLocale, getLocaleProvider, getText, getText, getText, getText, getText, getText, getText, getText, getText, getTextProvider, getTexts, getTexts, hasActionErrors, hasActionMessages, hasErrors, hasFieldErrors, hasKey, isValidLocale, isValidLocaleString, pause, setActionErrors, setActionMessages, setContainer, setFieldErrors, toLocale, validate
-
Constructor Details
-
DynamicFileUploadAction
public DynamicFileUploadAction()
-
-
Method Details
-
input
- Overrides:
inputin classActionSupport
-
upload
-
withUploadedFiles
Description copied from interface:UploadedFilesAwareNotifies action about the multiple uploaded files, when a single file is uploaded the list will have just one element- Specified by:
withUploadedFilesin interfaceUploadedFilesAware- Parameters:
uploadedFiles- a list ofUploadedFile, cannot be null. It can be empty.
-
getContentType
-
getFileName
-
getOriginalName
-
getInputName
-
getUploadedFile
-
getUploadSize
public long getUploadSize() -
getUploadType
-
setUploadType
-
prepare
Description copied from interface:PreparableThis method is called to allow the action to prepare itself.Default implementation is empty, allowing actions to implement only per-method variants like
prepareInput(),prepareEdit(), etc.- Specified by:
preparein interfacePreparable- Throws:
Exception- thrown if a system level exception occurs.
-
prepareUpload
public void prepareUpload() -
getUploadConfig
Returns the upload configuration object. This is used in struts.xml with ${uploadConfig.allowedMimeTypes} expressions.
-