Basic Spring MVC Data Binding

后端 未结 4 566
后悔当初
后悔当初 2020-12-10 06:50

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 cont

相关标签:
4条回答
  • 2020-12-10 07:15

    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>
    
    0 讨论(0)
  • 2020-12-10 07:17

    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;}
    }
    
    0 讨论(0)
  • 2020-12-10 07:21

    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.

    0 讨论(0)
  • 2020-12-10 07:30

    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.

    0 讨论(0)
提交回复
热议问题