This is the summary experience of the predecessors on the SpringMVC receiving form data:
SpringMVC receives page form parameters
Several methods of obtaining springmvc request parameters
The following are the details and points I found when using them. The predecessors did not record:
When using beans to receive parameters submitted by form forms, the pojo must contain the default (i.e. empty) constructor. At the same time, the variables that need to be set to the bean must have a setter method.
Note: The following codes are all sample codes and are not actually running the codes myself. Please add them yourself.
For example: I have a bean class that is User with variables username and password. At the same time, the content submitted by the form is:
<form action="save-user-info" method="post"> <span>account number:</span><input type="text" name="username"><br> <span>password:</span><input type="text" name="password"><br> <input type="submit" value="save"> </form>
Then, in the file, you must have
public User() {} public void setUsername(String username) { = username; } public void setPassword(String password) { = password; }
At this point, I can successfully receive the parameters in the Controller and generate the corresponding bean object
@RequestMapping(value="/save-user-info") public String saveUser(SsbiUser user) { (()); return "user-info"; }
Through some tests, I understand this process asAfter the front desk submits a form form containing User data, after the background accepts the parameters, it will first generate a User object that does not contain any parameters, and then set the corresponding value to this empty object through the setter method, and finally get the User object we need.。
Instead of what I thought at the beginning, the background accepts parameters and directly calls the corresponding User(username, password) constructor to generate the required object.
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!