Note when using bean in SpringMVC to receive parameters submitted by form forms. springmvcbean
This is a summary of SpringMVC's experience in receiving form data records:
SpringMVC receiving page form parameters
Several methods for obtaining springmvc Request Parameters
The following are the details and notes I have discovered when using them:
When bean is used to receive parameters submitted by form forms, pojo must contain the default (empty) constructor. At the same time, the variables to be set to bean must have the setter method.
Note: The following code is sample code. If you do not actually run the code yourself, add it yourself.
For example, I have a bean class named User with the variables username and password. At the same time, the content submitted by the form is:
<Form action = "save-user-info" method = "post"> <span> account: </span> <input type = "text" name = "username"> <br> <span> password: </span> <input type = "text" name = "password"> <br> <input type = "submit" value = "save"> </form>
In the User. java file
public User() {}public void setUsername(String username) { this.username = username;}public void setPassword(String password) { this.password = password;}
At this time, I can successfully receive parameters in the Controller and generate the corresponding bean object.
@RequestMapping(value="/save-user-info")public String saveUser(SsbiUser user) { System.out.println(user.toString()); return "user-info";}
Through some tests, I understood this process as: After the front-end submits a form containing User data, after receiving the parameters in the backend, first, a User object that does not contain any parameters is generated, and then the corresponding value is set to this empty object through the setter method, and the User object we need is finally obtained.
Instead of what I thought at the beginning, the backend receives parameters and directly calls the corresponding User (username, password) constructor to generate the required object.