springmvc form 提交表单数据,list,数组数据绑定

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

转载于:https://www.cnblogs.com/xuyung/p/4591011.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值