SpringMvc learning notes - parameter acquisition / binding / driving

In fact, it is to obtain the parameters submitted by the foreground. Some people like to call parameter binding and others like to call parameter driven. I think parameter acquisition is the most straightforward.

Simple parameters: (Integer, String, Float, Double, Boolean)

  • example:

jsp page:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
	<form action="${pageContext.request.contextPath }/testBaseParameter.action" method="POST">
		id: 
		<input name="id" type="text"/>
		name: 
		<input name="name" type="text"/>
		<input value="Submit" type="submit"/>
	</form>
</body>
</html>

Processor: Note: if you are creating a new test package, don't forget to configure to scan the package to make the annotation take effect.

package hh.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestGetParameter {

	@RequestMapping("/testBaseParameter.action")
	public void getBaseParameter(Integer id,String name) {
		
		System.out.println("id:"+id+"  name:"+name);
	}
}

Form submission page:

Submit results:

  • Console: you can see that the processor I wrote has no return value, but spring MVC will automatically get ModelAndView. Because I have not set View, it will automatically take the path I submitted in the previous step as the View path, so a prompt 404 will appear on the foreground page as well.

  • However, our goal has been achieved, and we have obtained the basic types of data submitted by the foreground. Note that when using this method, the parameters submitted by the foreground cannot have null values.

DEBUG [http-nio-8080-exec-7] - POST "/ssm/testBaseParameter.action", parameters={masked}
DEBUG [http-nio-8080-exec-7] - Mapped to public void hh.test.TestGetParameter.getBaseParameter(java.lang.Integer,java.lang.String)
id:111  name:Hu Hu
DEBUG [http-nio-8080-exec-7] - View name 'testBaseParameter', model {}
DEBUG [http-nio-8080-exec-7] - Forwarding to [testBaseParameter]
DEBUG [http-nio-8080-exec-7] - Completed 404 NOT_FOUND
  • Browser:

Object type parameter (entity class bean):

Take the User object as an example:

  • The User object has the following properties:
	private Integer id;
	private String name;
	private String address;
	private String sex;
  • Submit form:
<form action="${pageContext.request.contextPath }/testBeanParameter.action" method="POST">
	id: <input name="id" type="text" /><br>
	name: <input name="name" type="text" /><br>
	sex: <input name="sex" type="text" /><br>
	address: <input name="address" type="text" /><br>
	<input type="submit" value="Submit" />
</form>
  • Processor method:
	@RequestMapping("/testBeanParameter.action")
	public void getBaseParameter(User user) {
		
		System.out.println(user);
	}
  • Submit form content:

  • Submit results:
    • Console:
DEBUG [http-nio-8080-exec-3] - POST "/ssm/testBeanParameter.action", parameters={masked}
DEBUG [http-nio-8080-exec-3] - Mapped to public void hh.test.TestGetParameter.getBaseParameter(hh.pojo.User)
User [id=111, name=Hu Hu, sex=male, address=Shandong Province]
DEBUG [http-nio-8080-exec-3] - View name 'testBeanParameter', model {user=User [id=111, name=Hu Hu, sex=male, address=Shandong Province], org.springframework.validation.BindingResult.user=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
DEBUG [http-nio-8080-exec-3] - Forwarding to [testBeanParameter]
DEBUG [http-nio-8080-exec-3] - Completed 404 NOT_FOUND
    • Page:

You can see that the content submitted by the form is also successfully encapsulated in the object, indicating that the test is successful.

Note: when using object driven, the value of the name attribute of the < input > element of the submitted form should be consistent with the corresponding object attribute name.

Wrap class types (that is, there are references to object types in the class as properties):

For example: users In the Java class, there are the following attributes:

private Integer number;

private User user;

Where the User object is a property of reference type. In this case, if you want to directly encapsulate the parameters submitted by the foreground into the User reference in the Users object, it belongs to the wrapper class driver.

In this case, experienced programmers can guess how to write code.

  • Processor code:
	@RequestMapping("/testBeanParameter.action")
	public void getBaseParameter(Users users) {
		
		System.out.println(users.user);
	}
  • Submit form:
<form action="${pageContext.request.contextPath }/testBeanParameter.action" method="POST">
	id: <input name="user.id" type="text" /><br>
	name: <input name="user.name" type="text" /><br>
	sex: <input name="user.sex" type="text" /><br>
	address: <input name="user.address" type="text" /><br>
	<input type="submit" value="Submit" />
</form>

Just slightly change the name attribute of the < input > tag. There are no more tests here.

Spring MVC also provides parameter acquisition of user-defined types, such as Date and time. Some websites like to be independent and make a special Date format, such as 1999 * 9 * 9. At this time, spring MVC may not be able to automatically encapsulate this string into the attribute of Date type. At this time, you can customize a parser to parse this type of string. Although this situation is not common, you should also understand that it is basically to implement an interface and define your own packaging rules in a method. If you want to know more, you can find Du Niang. I won't talk about it in detail here.

Keywords: Spring Spring MVC

Added by Lefu on Fri, 17 Dec 2021 07:47:23 +0200