Spring MVC版本4中没有绑定的集合数据

时间:2016-01-28 05:23:33

标签: java spring spring-mvc

在我的案例中,有人可以帮助我找出问题所在。我试图使用spring MVC传递集合

   <form:form id="fileuploadForm" method="POST" enctype="multipart/form-data"
            class="cleanform" modelAttribute="metadataCollection">

            <input type="text" class="form-control" name="metadataCollection.metadata[0].keyName" placeholder="KeyName" />
            <input type="text" class="form-control" name="metadataCollection.metadata[0].value" placeholder="Value" />
            <button type="button" class="btn add-more addButton">+</button>

</form:form>            

模型对象

package org.springframework.samples.mvc.fileupload;

import java.util.ArrayList;
import java.util.List;

public class MetadataCollection {

    private List<Metadata> metadata = new ArrayList<Metadata>();

    public List<Metadata> getMetadata() {
        return metadata;
    }

    public void setMetadata(List<Metadata> metadata) {
        this.metadata = metadata;
    }
 }

package org.springframework.samples.mvc.fileupload;

public class Metadata {

        private String keyName;
        private String value;

        public Metadata(String keyName, String value) {
            super();
            this.keyName = keyName;
            this.value = value;
        }
        public String getKeyName() {
            return keyName;
        }
        public void setKeyName(String keyName) {
            this.keyName = keyName;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
}

控制器

@Controller
@RequestMapping("/fileupload")
public class FileUploadController {

    @ModelAttribute
    public void ajaxAttribute(WebRequest request, Model model) {
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
    }

    @RequestMapping(method=RequestMethod.GET)
    public @ModelAttribute("metadataCollection") MetadataCollection fileUploadForm(Model model) {
        model.addAttribute("metadataCollection",new MetadataCollection());
        return new MetadataCollection();
    }

    @RequestMapping(method=RequestMethod.POST)
    public @ResponseBody  String processUpload(@ModelAttribute(value = "metadataCollection") MetadataCollection metadata, HttpServletRequest request,BindingResult bindResult, ModelMap model) throws IOException {
        System.out.println("***** POST ******"  + metadata.getMetadata().size());
        //System.out.println("***** POST ******" + metadata.size());
        MetadataCollection m = (MetadataCollection)model.get("metadataCollection");
        System.out.println(m.getMetadata().size());
        //model.addAttribute("message", "File '" + file.getOriginalFilename() + "' uploaded successfully");
        return "presigned url " + request.getParameter("name");
    }

}

0 个答案:

没有答案