无法在控制器中选择选项

时间:2016-01-08 06:31:06

标签: spring select amazon-s3 controller thymeleaf

我有一个表单在select选项中列出存储桶,它在选择存储桶和文件后将文件上传到存储桶,但我无法在我的控制器中获取选择存储桶,文件夹alwsy返回空字符串,尽管mutipartFile是没问题,我真的很想知道为什么! 我这周搜索了所有内容,但没有结果我需要的! 即使在春天的框架中,我在百里香中也很新:( 请帮我解决这个简单的问题:)

部分html文件如下:

<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>
控制器如下:

@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";
}

1 个答案:

答案 0 :(得分:3)

您的代码中存在几个问题。

理想情况下,整个表单应封装在一个表单支持对象中。在您的情况下,创建一个将文件夹和文件包装在一起的Java对象。

class BucketFileForm{

    private MultipartFile uploadFile;
    private String folder;

    public String getFolder() {
        return folder;
    }
    public void setFolder(String folder) {
        this.folder = folder;
    }
    public MultipartFile getUploadFile() {
        return uploadFile;
    }
    public void setUploadFile(MultipartFile uploadFile) {
        this.uploadFile = uploadFile;
    }
}

在模型中使此对象可用,以便您可以在视图中访问它

@RequestMapping(value="/", method=RequestMethod.GET)
public String provideUploadInfo(Model model) {
    List<Bucket> buckets = s3Service.listBuckets();
    model.addAttribute("buckets", buckets);

    //replace this
    //model.addAttribute("folder", "com.smartstudy");

    //with
    BucketFileForm bucketFileForm = new BucketFileForm();
    bucketFileForm.setFolder("com.smartstudy");
    model.addAttribute("bucketFileForm", bucketFileForm);

    return "index";
}

现在,以

形式使用此表单支持对象
<form role="form" enctype="multipart/form-data" action="#" th:object="${bucketFileForm}" 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" th:field="*{uploadFile}" class="form-control" name="uploadFile"/>
    </div>
    <div class="form-group">
        <button class="btn btn-primary center-block" type="submit">Upload</button>
    </div>
</form>

然后修改POST端点以适应这些更改。

@RequestMapping(value="/upload", method=RequestMethod.POST)
public String handleFileUpload(@ModelAttribute("bucketFileForm") final BucketFileForm form, final BindingResult bindingResult, Model model) {

    log.info("Bucket: " + form.getFolder() + ", uploadFile: " +   form.getUploadFile().getOriginalFilename());

    if (!form.getUploadFile().isEmpty() && !form.getFolder().isEmpty()) {
        return s3Service.upload(uploadFile, folder);
    }

    return "index";
}