Three ways for the SpringMVC framework to receive client request parameters

1. [Not recommended] Use HttpServletRequest to receive request parameters

You can add HttpServletRequest type parameters to the parameter list of the method of processing the request, and then obtain the value of the request parameter in the traditional way:

@RequestMapping("handle_reg.do")
public String handleReg(HttpServletRequest request) {
	System.out.println("UserController.handleReg()");
	
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	String age = request.getParameter("age");
	String phone = request.getParameter("phone");
	String email = request.getParameter("email");
	System.out.println("username=" + username);
	System.out.println("password=" + password);
	System.out.println("age=" + age);
	System.out.println("phone=" + phone);
	System.out.println("email=" + email);
	
	return null;
}

This approach is not recommended, the main reasons are:

  1. Very cumbersome to use;
  2. If the expected parameter type is not String, you need to convert the data type yourself;
  3. Inconvenient to perform unit tests.

2. [Recommended] Add the required request parameters to the parameter list of the method to process the request

You can list the required parameters one by one in the parameter list of the method of processing the request, and the order is not distinguished, but the names must be consistent with the parameters submitted by the client:

@RequestMapping("handle_reg.do")
public String handleReg(String username, String password, 
	Integer age, String phone, String email) {
	System.out.println("UserController.handleReg()");
	System.out.println("username=" + username);
	System.out.println("password=" + password);
	System.out.println("age=" + (age + 1));
	System.out.println("phone=" + phone);
	System.out.println("email=" + email);
	return null;
}

When using this approach, you can declare the parameters directly as the desired type.
If the name of a defined parameter is a parameter not submitted by the client, the parameter value in the method will be null.
This approach is not suitable for application scenarios with a large number of request parameters!

3. [Recommended] Encapsulate the required request parameters in a custom type and use them as the parameters of the request processing method

Several request parameters can be encapsulated, for example:

public class User {
	private String username;
	private String password;
	private Integer age;
	private String phone;
	private String email;
	// 添加匹配的SET/GET方法
}

Then, in the parameter list of the method to process the request, add the parameters of the above custom data type:

@RequestMapping("handle_reg.do")
public String handleReg(User user) {
	System.out.println("UserController.handleReg()");
	
	System.out.println(user);
	
	return null;
}

4. Summary

First of all, the first approach (using HttpServletRequest) is not recommended, no matter in any application scenario, when receiving request parameters in the controller (Controller), this approach is not used!

The second approach (enumerate the parameters exhaustively) and the third approach (encapsulate the parameters) have their own advantages. When the number of request parameters is small (usually no more than 4) and is fixed, the second approach should be used first This will make the source code very intuitive and easier to understand the meaning of the code when reading it. On the contrary, when the number of request parameters is large (usually more than 6), or when the number of request parameters may change (when the requirements are adjusted in the future, The number may increase or decrease, or change to require other parameters), the third method should be used first!

In addition, the second method and the third method above can be used at the same time!

Guess you like

Origin blog.csdn.net/qq_37669050/article/details/101710911