file-uploadstruts2struts-action

Struts 2 file upload without Struts tags


Do I need to make use the Struts tags:

<s:form action="doUpload" method="post" enctype="multipart/form-data">
    <s:file name="upload" label="File"/>
    <s:submit/>
</s:form>

to make use of File upload functionality that Struts 2 is providing ?

Can I achieve the same functionality without Struts 2 tags? If yes can I know the conventions need to be incorporated in action or in configuration files to achieve the same?


Solution

  • Shortly, yes, you can. Then the configuration and conventions used by the action remains the same.

    If you use the <form> tag then you should place the action attribute value with the path that maps to the action. More about how the action mapper works and translates the path to the ActionMapper interface.

    In the form tag you should place the enctype="multipart/form-data", so the Struts is able to wrap the http request to the MultipartRequestWrapper class and parse form data. Then fileUpload interceptor adds parameters to the action context needed for the params interceptor to inject the file properties to the action that handles uploading.

    The name of the input tag should correspond the name of the property File type. It's used by both interceptors above and finally the object is injected via OGNL.

    If you need multiple files upload then you should change the properties type to be able to handle a collection of objects. Look like it's done in this example.