Spring MVC form values are automatically uploaded to javabean-annotations @modelattribute

Source: Internet
Author: User

making forms directly from form beans simplifies the processing of form submissions, especially for complex forms, which are not recommended because, after all, an extra form bean needs to be created.

There was a complex form in the previous time item, with multiple levels and arrays involved, and if it would be cumbersome not to handle the form bean, but multilayer and arrays, there was no way to find out how to handle it through form bean, but I believe Spring MVC should be able to do it, because spring is powerful because of the IOC, so in the online access to information, and continue to try to finally solve, now no matter how complex forms can be processed through form bean, greatly simplifying the processing of complex forms.
The solution is to use spring's annotated @modelattribute, which is believed to be familiar, since spring has many examples of how form processing is used, but these examples give a simpler form, and the spring Form label, and I prefer the original JSP and HTML. Gossip less, now formally starting our complex form processing, my form bean structure is as follows:
the structure of the Formbean is as follows:
private String name;
private int age;
Private string[] associate;
private comben[] bens;//Another array pair like
the structure of the Comben is as follows:
private String com;
private int[] typevalue;
private string[] TypeName;
Private thirdben[] thdben;//another layer of array pairs like
the structure of the Thirdben is as follows:
private String thdname;
Private String value[];//Another layer of array values
believe it's complicated enough, we just need to identify the Formbean in the controller as @modelattribute, as follows:
@RequestMapping (value= "/test/testform.do", method = Requestmethod.post)
Public String Testformbean (@ModelAttribute ("Fbean") Formbean ben) {

... ..

}

(Note: The identity name in the @ModelAttribute, which is the same as the form ID in the JSP).

and look at how the form in our JSP page is written (The form is given an ID that matches the name in the @modelattribute):



simple, we simply submit the form above to the controller, you can directly get this complex formbean in the controller, and after reading this example, I believe you can handle any complex form.

Spring MVC form values are automatically uploaded to javabean-annotations @modelattribute

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.