7

I'm learning Spring MVC and I've looked everywhere to do just a basic controller to view data binding but nothing I've tried as work. I can bind view posting back to controller and I can see the pojo with properties there, however whenever I tried to add that object to the model I get nothing. Here is what I have so far:

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 can I or need this to do get it to bind?

4 Answers4

8

The model attribute is the thing 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 invoked on both the GET and the POST, on the GET request it will just be exposed to the view where as on the POST it will be used to bind the request parameters.

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

Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
1

1) The contents of the modelMap are implicit in the JSP. You don't need to specify 'model' when you access them.

2) JSP-EL accesses fields via bean property accessors, not invoking methods. You don't specify the 'get' to call an actual method. You use the bean property name. e.g., ${person.firstName} to get the result of 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>
Affe
  • 47,174
  • 11
  • 83
  • 83
1

You need to change the Person object as well, the attributes should not have a "_" before that. Change "_firsName" to "firstName". The getter, setter's are fine. Spring binding goes by attribute names to their corresponding getter and setter's.

Also change the way you access in the view. Use like ${person.firstName}. You don't need the "model" before the "person.firstName" and also you dont need to mentione getFirstName, spring automatically does that for you.

raddykrish
  • 1,866
  • 13
  • 15
1

Please check your Person class variable, getter and setter methods naming. Getter method should start with 'get' and first capital letter of your variable name and same case for other letter for example : getFirstName() and setter method should also like getter method. If naming convention of your getter and setter methods are different, binding does not work properly. Here is the update 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;}
}
Thiha Zaw
  • 806
  • 1
  • 9
  • 25