Java Spring MVC版本4中的集合数据未绑定
有人能帮我找出我的问题吗。我正在尝试使用SpringMVC传递集合Java Spring MVC版本4中的集合数据未绑定,java,spring,spring-mvc,Java,Spring,Spring Mvc,有人能帮我找出我的问题吗。我正在尝试使用SpringMVC传递集合 <form:form id="fileuploadForm" method="POST" enctype="multipart/form-data" class="cleanform" modelAttribute="metadataCollection"> <input type="text" class="form-control" name="met
<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>
Metadata
需要一个默认构造函数。我已将默认构造函数添加到元数据和MetadataCollection中,但仍然相同。请解释什么不起作用…name=“MetadataCollection.Metadata[0]。keyName”
需要是``name=“Metadata[0]。keyName”。而且可能
metadata[0]`需要是metadata[]
,因为您的列表是空的。@zeroflagL:这就是修复方法。谢谢Metadata
需要一个默认构造函数。我已将默认构造函数添加到元数据和MetadataCollection中,但仍然相同。请解释什么不起作用…name=“MetadataCollection.Metadata[0]。keyName”
需要是``name=“Metadata[0]。keyName”。而且可能
metadata[0]`需要是metadata[]
,因为您的列表是空的。@zeroflagL:这就是修复方法。谢谢
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");
}
}