Note when using bean in SpringMVC to receive parameters submitted by form forms. springmvcbean

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.