8

I am using Spring framework and able to upload the file on server successfully.

<form action="upload.do" method="post" enctype="multipart/form-data">
    <input type="text" name="description" />
    <input type="file" name="locationMapFile" />
    ........
    ........
    <input type="submit" />
</form>

// controller layer

@RequestMapping(value = "/upload.do", method = {RequestMethod.POST})
public String addEditLocationToCompany(Model model
   ,@RequestParam("description")String desc
   ,@RequestParam(value="locationMapFile", required=false) CommonsMultipartFile locationMapFileData)
{

}

till now everything is fine. Now i am adding some dynamic hidden parameter on form using the javascript.

Note : as per setting i am defining the dynamic parameter name and its value like

<input type="hidden" name="setting_14" value="abcd"> 
<input type="hidden" name="setting_5" value="xyz"> 

How can i fetch these dynamic parameter into Spring controller.

I have tried

(1) I can not use the @RequestParam since do not want to hard code the parameter name

(2) request.getParameter() : not working and returning null since this is multipart/form-data request

(3) I have used this link How to upload files to server using JSP/Servlet? and tried

List<FileItem> items = 
       new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

but in spring controller items is null. so not able to iterate it and fetch the FileItem from it.

Please help me to find out the way to get the dynamic parameter's value into the spring framework.

Community
  • 1
  • 1
Rakesh Soni
  • 10,135
  • 5
  • 44
  • 51

2 Answers2

12

You can use MultipartHttpServletRequest to get the request parameters:

@RequestMapping(value = "/upload.do", method = {RequestMethod.POST})
public String addEditLocationToCompany(Model model
   ,@RequestParam("description")String desc
   ,@RequestParam(value="locationMapFile", required=false) CommonsMultipartFile locationMapFileData, MultipartHttpServletRequest mrequest)
{
String value = mrequest.getParameter("setting_14");
}
Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
0

Why not just add one hidden field and set it's value by concating all setting values You want? As You said You are using javascript to add those input fields. It will look like

<input type="hidden" name="setting" value="set1.value, set2.value, set3.value">

Then You will be free to hardcode setting parameter in your @RequestParam

Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
  • Sorry we can not use this. since setting value may have any text and user can enter the comma or any other character in it. – Rakesh Soni Nov 07 '13 at 08:55