Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
147 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...