how to bind data to list in spring form

后端 未结 4 1253
南笙
南笙 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:22
    <c:forEach items="${campaignModel.landingPageModels}" var="landingPage">
       <sf:input path="landingPage.url" class="form- control" />
    </c:forEach>
    

    Will the above not works for you to view data? To get them in controller you may have to use dynamic table row concept in HTML or for each single LandingPage entry add to form bean object by clicking add button and render back.

    In my case Person command object having List<Token> property, in order to bind the list of tokens we have designed page as attached screen shot below, clicking on Add button hits the controller and add the each token List<Token> and render back to same view and displays added token in list view, it facilitates to add multiple token for Person.

    0 讨论(0)
  • 2021-02-06 05:28

    In order to bind a list model property to multiple input fields, you need this in the rendered form:

    <input type="text" name="landingPageModels[0].landingPage.url" class="form-control" />
    <input type="text" name="landingPageModels[1].landingPage.url" class="form-control" />
    <input type="text" name="landingPageModels[2].landingPage.url" class="form-control" />
    

    Which is accomplished by:

    <c:forEach items="${campaignModel.landingPageModels}" varStatus="s">
        <sf:input path="landingPageModels[${s.index}].landingPage.url" class="form-control" />
    </c:forEach>
    
    0 讨论(0)
  • 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"%>
    <html>
    <head>
    </head>
    <body>
    
        <form:form method="post" action="save.html" modelAttribute="contactForm">
            <table>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
                <tr>
                   <td><input name="contacts[0].firstname" /></td>
                    <td><input name="contacts[0].lastname" /></td>
                 </tr>
                 <tr>   
                    <td><input name="contacts[1].firstname" /></td>
                    <td><input name="contacts[1].lastname" /></td>
                 </tr>   
    
        </table>    
        <br/>   
        <input type="submit" value="Save" />
    
        </form:form>
        </body>
        </html>
    

    @controller :

    @RequestMapping(value = "/save", method = RequestMethod.POST)
        public ModelAndView save(@ModelAttribute("contactForm") ContactForm contactForm) {
    
            List<Contact> 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<Contact> contacts;
    
        public List<Contact> getContacts() {
            return contacts;
        }
    
        public void setContacts(List<Contact> 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
    
    }
    
    0 讨论(0)
  • 2021-02-06 05:42

    The error you are getting is correct as landingPageModels is a list. You would need to use index access like this landingPageModels[0].landingPage.url. If you have dynamic number of input/url, then you can use <c:forEach> to create dynamic input variable names

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