Basic Spring MVC Data Binding

I am learning Spring MVC, and I searched everywhere to make only the base controller to view the data binding, but tried nothing to work. I can associate viewing the declaration back with the controller, and I can see pojo with the properties there, however whenever I try to add this object to the model, I get nothing. Here is what I still have:

controller

@Controller public class HomeController { @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Model model) { model.addAttribute(new Person()); return "home"; } @RequestMapping(value="/about", method=RequestMethod.POST) public void about(Person person, Model model) { model.addAttribute("person", person); } } 

Class I want to bind

 public class Person { private String _firstName; private String _lastName; private Date _Birthday; //Set public void setFirstName(String FirstName){this._firstName = FirstName; } public void setLastName(String LastName){this._lastName= LastName; } public void setBirthDate(Date BirthDate){ this._Birthday = BirthDate;} //get public String getFirstName(){return _firstName;} public String getLastName(){return _lastName;} public Date getBirthDate(){return _Birthday;} } 

View - controller-to-form! Working

 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <html> </head> <body> FirstName: ${model.person.getFirstName} LastName: ${model.person.getLastName} </body> </html> 

What do I need or need to do to tie it?

+7
source share
4 answers

The model attribute is what you are missing here.

 @Controller public class HomeController { @ModelAttribute("person") public Person getPerson(){ return new Person(); } @RequestMapping(value = "/", method = RequestMethod.GET) public String home() { return "home"; } @RequestMapping(value="/about", method=RequestMethod.POST) public void about(@ModelAttribute("person") Person person, BindingResult result, Model model) { if( ! result.hasErrors() ){ // note I haven't compiled this code :) } } } 

The idea is that the @ModelAttribute method will be called both in GET and in POST, at the request of GET it will simply be open for presentation, where, as in POST, it will be used to bind request parameters.

Note that the BindingResult is passed to the POST method, so you can do something with the command.

+7
source

1) The contents of modelMap are implicitly displayed in the JSP. You do not need to specify a "model" when accessing them.

2) JSP-EL accesses fields through bean attributes of access to resources, and not to method calls. You do not specify "get" to call the actual method. You are using the bean property name. for example, $ {person.firstName} to get the result person.getFirstName ();

 @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Model model) { Person person = new Person(); person.setFirstName("Kai"); person.setLastName("Cooper"); model.addAttribute("person", person); return "home"; } 

`

 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <html> </head> <body> FirstName: ${person.firstName} LastName: ${person.lastName} </body> </html> 
+1
source

You also need to modify the Person object, before this the attributes must not have a "_". Change "_firsName" to "firstName". Getter, the setter is fine. Spring binding follows attribute names to their respective recipients and setters.

Also change the access method in the view. Use as $ {person.firstName}. You do not need a “model” before “person.firstName”, nor do you need to specify getFirstName, Spring automatically does this for you.

+1
source

Please check the variable names for Person, getter and setter. The getter method should begin with "get" and the first capital letter of your variable name and the same case for another letter, for example: getFirstName() , and the setter method should also use the getter method. If the naming convention for your getter and setter methods is different, the binding does not work properly. The following is the version of your Person class.

 public class Person { private String firstName; private String lastName; private Date Birthday; //Set public void setFirstName(String FirstName){this.firstName = FirstName; } public void setLastName(String LastName){this.lastName= LastName; } public void setBirthDate(Date BirthDate){ this.Birthday = BirthDate;} //get public String getFirstName(){return firstName;} public String getLastName(){return lastName;} public Date getBirthDate(){return Birthday;} } 
+1
source

All Articles