Can't get selected option in controller

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't get selected option in controller

mikezang
I have a form to list up buckets in select options, it uploads file to bucket after select bucket and file, but I can't get selected bucket in my controller, The folder alwsy return empty string, though mutipartFile is no problem, I really want to know why!
I googled for all this week but no result what I need!
I am very new in thymeleaf even in spring framework:(
Pls help me to solve this simple problem to you:)

part of html file as below:
<form role="form" enctype="multipart/form-data" action="#" th:object="${folder}" th:action="@{'/drill/skin/upload'}" method="POST">
  <div class="form-group">
    <label class="form-control-static">Select Bucket</label>
      <select class="form-control" th:field="${folder}">
        <option th:each="bucket : ${buckets}" th:value="${bucket.name}" th:text="${bucket.name}">bucket</option>
      </select>
    </div>

    <label class="form-control-static" for="inputSuccess">Select Upload File</label>
    <div class="form-group">
      <input type="file" class="form-control" name="uploadFile"/>
    </div>
  <div class="form-group">
    <button class="btn btn-primary center-block" type="submit">Upload</button>
  </div>
</form>
controller as below:
@RequestMapping(value="/", method=RequestMethod.GET)
public String provideUploadInfo(Model model) {
  List<Bucket> buckets = s3Service.listBuckets();
  model.addAttribute("buckets", buckets);
  model.addAttribute("folder", "com.smartstudy");
  return "index";
}

@RequestMapping(value="/upload", method=RequestMethod.POST)
public String handleFileUpload(
  @ModelAttribute("folder") String folder,
  @RequestParam("uploadFile") MultipartFile uploadFile, Model model) {
  log.info("Bucket: " + folder + ", uploadFile: " + uploadFile.getOriginalFilename());
  if (!uploadFile.isEmpty() && !folder.isEmpty()) {
    return s3Service.upload(uploadFile, folder);
  }
  return "index";
}
Reply | Threaded
Open this post in threaded view
|

Re: Can't get selected option in controller

Zemi
Administrator
Hello,

to get the parameter 'folder' you have to use @RequestParam instead of @ModelAttribute.

@ModelAttribute is used to set an attribute object into the model for further usage.

Regards,
  Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Can't get selected option in controller

mikezang
If I like you said to use as below, I got error 400! how can I do next?

    @RequestMapping(value="/drill/skin/upload", method=RequestMethod.POST)
    public String handleFileUpload(
    		@RequestParam("folder") String folder,
    		@RequestParam("uploadFile") MultipartFile uploadFile, Model model) {
...
Reply | Threaded
Open this post in threaded view
|

Re: Can't get selected option in controller

Zemi
Administrator
Hi,

now I see that you have a typo in your template. Instead of

   th:field="${folder}"

you should use the field syntax

   th:field="*{folder}"

Regards,
   Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Can't get selected option in controller

mikezang
That doesn't work, I also got error! In my source that folder is not part of object.

People on other forum told me to set folder as part of object, now I can use "*{folder}" and I got folder in controller.

Anyway, thanks for your message.