Chapter 4. Spring MVC Processing Request Data

Request data: request parameters, request header (Cookie), request body (parameters submitted by post)

1. Request Processing Method Signature

(1) Spring MVC analyses the signature of the processing method (method name + parameter list).

The HTTP request information is bound to the corresponding parameters of the processing method.

(2) Spring MVC has very loose restrictions on the signature of the controller processing method, and can sign the method in almost any way you like.

(3) The method and method can be annotated as necessary.

(@PathVariable, @RequestParam, @RequestHeader, etc.))

(4) The Spring MVC framework binds the HTTP request information to the corresponding method.

According to the return value type of the method, the corresponding follow-up processing is made.

2. @RequestParam annotation

(1) The request parameters can be passed to the request method by using @RequestParam at the processing method entry

(2) value: parameter name

(3) required: Is it necessary? The default is true, which means that the request parameter must contain the corresponding parameter. If it does not exist, an exception will be thrown.

(4) defaultValue: Default value that is used when no parameter is passed

Sample code

1. Request page

<a href="${pageContext.request.contextPath }/testRequestParam?username=xxj&age=12">test RequestParam</a>

2. Control Processor

/**
 * @RequestParam Annotations are used to map request parameters
 *         value Name of parameter used to map request
 *         required Used to set whether request parameters are required
 *         defaultValue Set the default value to use when no parameter is passed
 */

@RequestMapping(value="/testRequestParam")
	public String testRequestParam(
	@RequestParam(value="username") String username,
								   @RequestParam(value="age",required=false,defaultValue="0") Integer age) {
		System.out.println(username+":::"+age);
		return "success";
	}

3. @RequestHeader annotation

(1) Use @RequestHeader to bind the attribute value of the request header

(2) The request header contains several attributes from which the server can know the information of the client and bind the value of the attributes in the request header to the input of the processing method through @RequestHeader.

(3) All attributes in the request message

  1. Accept
  2. Accept-Encoding
  3. Accept-Language
  4. Connection
  5. Cookie
  6. Host
  7. Referer
  8. Upgrade-Insecure-Requests
  9. User-Agent:

Sample code

1. Request page

<a href="${pageContext.request.contextPath }/testRequestHeader">test RequestParam</a>

2. Control Processor

@RequestMapping("/testRequestHeader")
	public String testRequestHeader(@RequestHeader("Cookie")String a,@RequestHeader("Accept-Language")String b) {
		System.out.println(a);
		System.out.println(b);
		return "success";
}

4. @CookieValue annotation

(1) Bind the Cookie value in the request with @CookieValue

(2) @CookieValue allows processing methods to be bound to a Cookie value

Sample code

1. Request page

<a href="${pageContext.request.contextPath }/testCookieValue">test CookieValue</a>

2. Control Processor

@RequestMapping("/testCookieValue")
	public String testCookieValue(@CookieValue("JSESSIONID") String jessionid) {
		
		System.out.println("jessionid:"+jessionid);
		
		return "success";
	}

5. Using POJO as a parameter

(1) Binding request parameter values using POJO objects

(2) Spring MVC automatically matches the request parameter name with the POJO attribute name and fills the attribute value for the object. Supports cascading attributes. For example: dept.deptId, dept.address.tel, etc.

Sample code

1. Request page

<form action="${pageContext.request.contextPath }/testPOJO" method="post">
		//Name: <input type="text" name="username"/><br>
		//Gender: <input type="radio" name="gender" value="1"/>male
			<input type="radio" name="gender" value="0"/>female<br>
		//Age: <input type="text" name="age"/><br>
		//Provinces: <input type="text" name="address.province"/><br>
		//City: <input type="text" name="address.city"/><br>
		<input type="submit" value="register"/><br>
		
</form>

2. Relevant bean s

@Component
public class Person {
	private String username;
	private String gender;
	private Integer age;
	private Address address;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Person [username=" + username + ", gender=" + gender + ", age=" + age + ", address=" + address + "]";
	}
	
	
}




@Component
public class Address {
	private String province;
	private String city;
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	@Override
	public String toString() {
		return "Address [province=" + province + ", city=" + city + "]";
	}
	
	
}

3. Control Processor

@RequestMapping("/testPOJO")
	public String testPOJO(Person person) {
		System.out.println(person);
		return "success";
	}

6. Using Servlet native API as a parameter

6.1 MVC Handler method which Servlet API type parameters can be accepted

(1)HttpServletRequest

(2)HttpServletResponse

(3)HttpSession

(4)java.security.Principal

(5)Locale

(6) InputStream

(7)OutputStream

(8)Reader

(9)Writer

Except (4) (5) can not be obtained from (1) (2), everything else can be obtained, so we must master (1) (2).

6.2 Source Reference: Annotation Method Handler Adapter L866

@Override
protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception {
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
 
if (ServletRequest.class.isAssignableFrom(parameterType) ||
MultipartRequest.class.isAssignableFrom(parameterType)) {
Object nativeRequest = webRequest.getNativeRequest(parameterType);
if (nativeRequest == null) {
throw new IllegalStateException(
"Current request is not of type [" + parameterType.getName() + "]: " + request);
}
return nativeRequest;
}
else if (ServletResponse.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
Object nativeResponse = webRequest.getNativeResponse(parameterType);
if (nativeResponse == null) {
throw new IllegalStateException(
"Current response is not of type [" + parameterType.getName() + "]: " + response);
}
return nativeResponse;
}
else if (HttpSession.class.isAssignableFrom(parameterType)) {
return request.getSession();
}
else if (Principal.class.isAssignableFrom(parameterType)) {
return request.getUserPrincipal();
}
else if (Locale.class.equals(parameterType)) {
return RequestContextUtils.getLocale(request);
}
else if (InputStream.class.isAssignableFrom(parameterType)) {
return request.getInputStream();
}
else if (Reader.class.isAssignableFrom(parameterType)) {
return request.getReader();
}
else if (OutputStream.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
eturn response.getOutputStream();
}
else if (Writer.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response.getWriter();
}
return super.resolveStandardArgument(parameterType, webRequest);
}

6.3 Sample Code

/**
 * Serlvet native API s can be used as parameters of the target method to support the following types
 * 
 * HttpServletRequest 
 * HttpServletResponse 
 * HttpSession
 * java.security.Principal 
 * Locale InputStream 
 * OutputStream 
 * Reader 
 * Writer
 * @throws IOException 
 */
@RequestMapping("/testServletAPI")
public void testServletAPI(HttpServletRequest request,HttpServletResponse response, Writer out) throws IOException {
	System.out.println("testServletAPI, " + request + ", " + response);
	out.write("hello springmvc");
	//return "success";
}

<! -- Testing the Servlet API as a processing request parameter - >
<a href="springmvc/testServletAPI">testServletAPI</a>

Keywords: Spring Attribute Java encoding

Added by jdeer0618 on Fri, 06 Sep 2019 12:48:32 +0300