Spring MVC form提交

这篇文章从业务可能的角度记录 form请求的情况,主要涉及到Spring MVC的 @RequestParam注解

参考 HTTP中application/x-www-form-urlencoded字符说明 https://www.cnblogs.com/zeroingToOne/p/8992746.html

1.form请求的编码格式

     form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。

     当enctype为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url

     当enctype为post时候,浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,浏览器就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。 

     所以如果form请求,没有没有type=file的话,那么它的编码格式就是application/x-www-form-urlencoded  1.可以用@RequestParam注解获取参数,如果参数名没有特殊符号,也可以省略@RequestParam注解 直接用参数名来接收  2.如果这些参数是一个实体类的属性,也可以省略注解直接用也给实体类来接收。

准备两个实体类

public class Person {

	private String userName;
	private Long age;
	private Book book;
    //getter setter 方法,重写 toString方法
}
public class Book {

	private String author;
	private String name;
    //getter setter 方法,重写 toString方法

}

 

2.form  提交一些参数/一个实体类  

.jsp文件

<html>
<body>

 	<!-- <form id="forEntityPost" action="/SpringMVCDemo/formSubmit/forEntity" method="post"> -->
 	<!-- <form id="forParamPost" action="/SpringMVCDemo/formSubmit/forParam" method="post"> -->
 	<!-- <form id="forEntityGet" action="/SpringMVCDemo/formSubmit/forEntity" method="get"> -->
 	<form id="forParamGet" action="/SpringMVCDemo/formSubmit/forParam" method="get">
		<div>userName<input type="text" name="userName" /></div>
		<div>age<input type="text" name="age" /></div>
		<div>bookName<input type="text" name="book.name" /></div>
		<div>bookAuthor<input type="text" name="book.author" /></div>
		<div><input type="submit" value="forGet" /></div>
	</form>
	<script type="text/javascript">		
	</script>
</body>
</html>

controller java文件  不管form的提交方式时get还是post,控制器方法都可以如下写。

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.springmvcdemo.entity.Person;

@Controller
@RequestMapping("formSubmit")
public class FormController {

	// 1.可以用@RequestParam注解获取参数,如果参数名没有特殊符号,也可以省略@RequestParam注解 直接用参数名来接收
	@RequestMapping(value = "/forParam")
	public String forPost(String userName, Long age, @RequestParam("book.name") String bookName,
			@RequestParam("book.author") String bookAuthor) {
		System.out.println("----------forParam--------userName==" + userName + "-aget==" + age 
				+ "--bookName==" + bookName + "bookAuthor==" + bookAuthor);
		return null;
	}

	// 2.如果这些参数是一个实体类的属性,也可以省略注解直接用也给实体类来接收。
	@RequestMapping(value = "/forEntity")
	public String forEntityPost(Person person) {
		System.out.println("------------forEntity------person==" + person.toString());
		return null;
	}

}

contoller 控制台输出log

get请求和post请求 虽然编码方式是一样的,所以在控制器中接收参数的方式是一样的。但是其实它们的参数组装方式不一样

get请求的参数是拼接在url中的。

post的参数是在请求体中

 

使用HttpServletRequest HttpServletResponse

@RequestMapping("/home")
public String hello(HttpServletRequest request,HttpServletResponse response) {
	System.out.println(request.getParameter("username"));		
	return "home";
}

 

posted on 2019-08-02 16:45  dreamstar  阅读(265)  评论(0编辑  收藏  举报