标签:
public class Person { private String name; private List<Son> sons; public Person () { } public Person(String name,List<Son> sons) { this.name=name; this.sons=sons; } public void setName(String name) { this.name=name; } public String getName() { return this.name; } public void setSons(List<Son> sons) { this.sons=sons; } public List<Son> getSons() { return this.sons; } }
public class Son { private String name; public Son() { } public Son(String name) { this.name=name; } public void setName(String name) { this.name=name; } public String getName() { return this.name; } }
用到的类包
<!-- 文件上传所需要的包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
selver-xml配置
<!-- 支持上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为1MB --> <property name="maxUploadSize"> <value>1048576</value> </property> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean>
接受的controller
@RequestMapping(value="/databind",method={RequestMethod.POST}) public String datebind(@RequestParam("files") MultipartFile[] files,Person person){ System.out.println(files[0].getOriginalFilename()); System.out.println(files[1].getOriginalFilename()); System.out.println(person.getName()); System.out.println(person.getSons().get(0).getName()); System.out.println(person.getSons().get(1).getName()); return "datebind"; }
jsp
<form action="/helloworld/helloworld/databind" method="post" enctype="multipart/form-data"> <input type=‘text‘ name=‘name‘/><br> <input type=‘text‘ name=‘sons[0].name‘/><br> <input type=‘text‘ name=‘sons[1].name‘/><br> <input type="file" name="files"/><br> <input type="file" name="files"/><br> <input type=‘submit‘ value=‘submit‘/> </form>
http报文
------WebKitFormBoundarySKTvZMjQOULPb6gV
Content-Disposition: form-data; name="name"
name
------WebKitFormBoundarySKTvZMjQOULPb6gV
Content-Disposition: form-data; name="sons[0].name"
son1name
------WebKitFormBoundarySKTvZMjQOULPb6gV
Content-Disposition: form-data; name="sons[1].name"
son2name
------WebKitFormBoundarySKTvZMjQOULPb6gV
Content-Disposition: form-data; name="files"; filename="314.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
------WebKitFormBoundarySKTvZMjQOULPb6gV
Content-Disposition: form-data; name="files"; filename="foot.txt"
Content-Type: text/plain
${index7}
${footnews} 2
${foottags} 10
${footvideos}6
------WebKitFormBoundarySKTvZMjQOULPb6gV--
结果
314.docx
foot.txt
name
son1name
son2name
springmvc form 提交表单数据,list,数组数据绑定
标签:
原文地址:http://www.cnblogs.com/xuyung/p/4591011.html