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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM