how to bind data to list in spring form

后端 未结 4 1252
南笙
南笙 2021-02-06 05:02

I have a spring form with having backing object for it. The form is like this-



        
4条回答
  •  误落风尘
    2021-02-06 05:40

    I dont know how to do it with spring form lib input but if you want to bind using simple html input than you can bind list like this

    Simple.jsp

    <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
    
    
    
    
        
            
    First Name Last Name

    @controller :

    @RequestMapping(value = "/save", method = RequestMethod.POST)
        public ModelAndView save(@ModelAttribute("contactForm") ContactForm contactForm) {
    
            List contacts = contactForm.getContacts();
    
            if(null != contacts && contacts.size() > 0) {
                ContactController.contacts = contacts;
                for (Contact contact : contacts) {
                    System.out.printf("%s \t %s \n", contact.getFirstname(), contact.getLastname());
                }
            }
    
            return new ModelAndView("show_contact", "contactForm", contactForm);
        }
    

    ContactForm.java

    import java.util.List;
    
    public class ContactForm {
    
        private List contacts;
    
        public List getContacts() {
            return contacts;
        }
    
        public void setContacts(List contacts) {
            this.contacts = contacts;
        }
    }
    

    Contact.java

    public class Contact {
        private String firstname;
        private String lastname;
        private String email;
        private String phone;
    
        public Contact() {
    
        }
     //getters and setters
    
    }
    

提交回复
热议问题