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
See Also:
  • Constructor Details

    • DynamicFileUploadAction

      public DynamicFileUploadAction()
  • Method Details

    • input

      public String input()
      Overrides:
      input in class ActionSupport
    • upload

      public String upload()
    • withUploadedFiles

      public void withUploadedFiles(List<UploadedFile> uploadedFiles)
      Description copied from interface: UploadedFilesAware
      Notifies action about the multiple uploaded files, when a single file is uploaded the list will have just one element
      Specified by:
      withUploadedFiles in interface UploadedFilesAware
      Parameters:
      uploadedFiles - a list of UploadedFile, cannot be null. It can be empty.
    • getContentType

      public String getContentType()
    • getFileName

      public String getFileName()
    • getOriginalName

      public String getOriginalName()
    • getInputName

      public String getInputName()
    • getUploadedFile

      public Object getUploadedFile()
    • getUploadSize

      public long getUploadSize()
    • getUploadType

      public String getUploadType()
    • setUploadType

      public void setUploadType(String uploadType)
    • prepare

      public void prepare() throws Exception
      Description copied from interface: Preparable
      This 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:
      prepare in interface Preparable
      Throws:
      Exception - thrown if a system level exception occurs.
    • prepareUpload

      public void prepareUpload()
    • getUploadConfig

      public DynamicFileUploadAction.UploadConfig getUploadConfig()
      Returns the upload configuration object. This is used in struts.xml with ${uploadConfig.allowedMimeTypes} expressions.