Spring MVC automatically populates the property values with the request parameter name and the POJO property name for automatic matching. Cascading properties are supported. such as: Dept.deptid, dept.address, etc.
The popular point is that when we want to receive the form data of the request page and encapsulate it as a specific object, we need to do it in the corresponding method of a servlet to remove the form parameters from the request, type a good conversion, construct a specific type of object, and then save the form parameters in.
As long as you can guarantee that the name of the form entry for the requesting page is the same as the domain name of the Pojo object class.
In addition, this feature of SPRINGMVC supports cascading properties, which is the case where the domain in Pojo is another Pojo object. Accordingly, the name of the form is written as: Pojo1 domain name. pojo2 Domain Name
For example:
Person class
Public class person { public string getusername () { return username; } public void setusername (String username) { this.username = username; } public int getage () { return age; } public void setage (int age) { this.age = age; } public location getlocal () { return local; } public void setlocal (location local) { this.local = local; } string username; int age; Location local; @Override public string tostring () { return "Person [username=" + username + ", age= " + age + ", local= " + local + "]"; }}
Location class
Public class location { public string getcity () { return city; } public void setcity (string city) { this.city = city; } public string Getprovince () { return province; } public void setprovince (string province) { this.province = province; } String city; String province; @Override public string tostring () { return "location [city=" + city + " province=" + province + "]"; } }
Controller
@Controller @requestmapping ("/roger") public class pojorequestcontroller { @RequestMapping (value = "/addperson") public string addperson () { return "Addperson"; } @ @RequestMapping (value = "/showperson", method = Requestmethod.post) public string showperson (PrintWriter Printwriter, person person) { system.out.println ("Person:" + person.getname () + " " + person.getlocal (). getcity ()); printwriter.println ("Name:" + person.getname ()); printwriter.println ("City" + person.getlocal (). getCity ()); return null; }}
addperson.jsp
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
Attention:
If a field name item in the Pojo object does not exist in the requested form parameter, then pojo the property is null after binding.
If there are items in the requested form parameter that do not contain fields in the Pojo object, the parameter is lost after binding.
If the data for the form item is automatically converted from string to the field type of the corresponding Pojo. However, if the type cannot be converted, an error is provided. If I enter age as DAFSDFA, then error.
SPRINGMVC (iii): Request parameter value with POJO object binding