Upload file and send parameters to spring portlet controller

1.5k Views Asked by At

Can any one of you tell me how to upload a file and also pass a input parameter to the controller using spring mvc portlet controller ?

Action URL and form in jsp :

<portlet:actionURL var="addDetailsURL"> 
    <portlet:param name="addDetails" value="addDetailsValue"/> 
</portlet:actionURL>


<div id = "<portlet:namespace/>addDetailsDIV" >
    <form action="${addDetailsURL}" enctype="multipart/form-data" method="post">

        <input style="display:block;" type="text" name="selectedDetail" id='selectedDetail'/> <%-- holds the parameter that is supposed to be passed to the controller --%>
        <table width = 100% cellspacing="5" cellpadding="5">
            <tr>
                <td colspan='2'><fmt:message key = "selectFileUploadTxt"/></td>
            </tr>
            <tr>
                <td colspan='2'><input id="uploadedFile" type="file" name="uploadedFile" /></td>
            </tr>
            <tr>
                <td >
                <span id="<portlet:namespace />closeAddDetailDia">cancel</span>
                </td>
                <td align="left">
                <Button id="uploadButton"><fmt:message key="addDetailButtonTxt" /></Button>

                </td>
            </tr>
        </table>
    </form>
</div>

On submit the request goes to the controller but the "selectedDetail" parameter is null.

Controller code :

@RequestMapping("VIEW")
public class AddDetailsController {

    /**
     * 
     * @param actionRequest
     * @param actionResponse
     * @return
     * @throws IOException 
     */
    @SuppressWarnings({ "rawtypes", "unused" })
    @ActionMapping(params = "addDetails=addDetailsValue")
    public String fileUpload(ActionRequest actionRequest, ActionResponse actionResponse,
        @RequestParam String selectedDetail
            ) throws PortletException, IOException {
        System.out.println("selectedDetail : " + selectedDetail); // it is null
        System.out.println("selectedDetail : " + actionRequest.getParameter("selectedDetail")); // it is null

The file upload part is working fine but I am not getting the "selectedDetail" parameter. Please help .

Environment Details

  1. WebSphere Portal v7002
  2. IDE - RAD v8.5
  3. Spring version 3.1.0

Please let me know if you need any other detail.

1

There are 1 best solutions below

0
On

On the backend, define a bean to hold the form data including the file.

I'll call it FormBean. FormBean should have an instance variable of type
private org.springframework.web.multipart.commons.CommonsMultipartFile file;

Changes in the form will include having "commandName" and "path" in form elements:

<form:form enctype="multipart/form-data" commandName="formBean"... />

&
<form:input id="uploadedFile" type="file" name="uploadedFile" path="file"/>

The following link explains this well: https://portalhub.wordpress.com/2012/05/20/spring-mvc-portlet-file-upload-and-download/