javastruts2uploadognlstruts2-interceptors

How to make fileUpload parameters dynamic in Struts 2


In a Struts 2 application we use fileUpload interceptor to get file from the user.

The fileUpload has some configurations maximumSize , allowedTypes , allowedExtensions that can be used as:

<interceptor-ref name="fileUpload">
    <param name="maximumSize">200000</param>                                          
    <param name="allowedTypes">text/plain</param>
    <param name="allowedExtensions">txt</param> 
</interceptor-ref>

Is it possible to make these parameters dynamic !?

For example :

<param name="maximumSize">${maxsize}</param>    

and let the action set its max file size.


Solution

  • It's not possible to make these parameters dynamic. But at runtime when interceptor is invoked you can get the value dynamically

    String maxsize = TextParseUtil.translateVariables(maximumSize, actionInvocation.getStack());
    

    The action is invoked after interceptors chain, so it can't set the value. However you can translate the value before chained result.