How to bind data to a list in spring form

I have a spring form that has a helper object. The form is as follows:

<sf:form cssClass="form-horizontal" commandName="campaignModel" method="post"> <sf:input path="campaign.name" class="form-control" /> <sf:input path="landingPageModels.landingPage.url" class="form-control" /> </sf:form> 

Model class (form support object) -

CampaignModel.java

 public class CampaignModel { private Campaign campaign = new CampaignImpl(); private List<LandingPageModel> landingPageModels = new Arraylist<LandingPageModel>; public Campaign getCampaign() { return campaign; } public void setCampaign(Campaign campaign) { this.campaign = campaign; } public List<LandingPageModel> getLandingPageModels() { return landingPageModels; } public void setLandingPageModels(List<LandingPageModel> landingPageModels) { this.landingPageModels = landingPageModels; } 

LandingPageModel.java -

 public class LandingPageModel { private LandingPage landingPage = new LandingPageImpl(); private List<LandingPageParameterImpl> landingPageParameters = new ArrayList<LandingPageParameterImpl>(); public LandingPage getLandingPage() { return landingPage; } public void setLandingPage(LandingPage landingPage) { this.landingPage = landingPage; } public List<LandingPageParameterImpl> getLandingPageParameters() { return landingPageParameters; } public void setLandingPageParameters(List<LandingPageParameterImpl> landingPageParameters) { this.landingPageParameters = landingPageParameters; } } 

LandingPage.java -

 public class LandingPageImpl extends EntityImpl implements LandingPage { private String url; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } 

So, I want so that I can insert a lot of landPage objects (having their own url property) into the landingPageModels object. This means that I can have a mulitple input tag with a url property like this -

 <sf:input path="landingPageModels.landingPage.url" class="form-control" /> <sf:input path="landingPageModels.landingPage.url" class="form-control" /> <sf:input path="landingPageModels.landingPage.url" class="form-control" /> 

But when executing this code, spring gives me an error that the landingPage property for landPageModels does not have a set getter method. How to solve it and how to make this value somewhat?

+5
source share
4 answers

To associate a list model property with multiple input fields, you need this in a visualized 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" /> 

This is done:

 <c:forEach items="${campaignModel.landingPageModels}" varStatus="s"> <sf:input path="landingPageModels[${s.index}].landingPage.url" class="form-control" /> </c:forEach> 
+2
source

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

0
source
 <c:forEach items="${campaignModel.landingPageModels}" var="landingPage"> <sf:input path="landingPage.url" class="form- control" /> </c:forEach> 

Will the above not work for viewing data? To get them in the controller, you may need to use the dynamic concept of the table row in HTML or for each individual LandingPage record add a bean to the form by clicking the add button and put it back.

In my case, the object of the Person command with the List<Token> property to link the list of tokens, we designed the page as an attached screenshot below, clicking the Add button, click on the controller and add each List<Token> and return it back to that the same view also displays the added token in the form of a list; it facilitates the addition of several tokens for Person .

enter image description here

0
source

I don't know how to do this with the input of the spring form, but if you want to link using simple html input, you can link a 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 } 
-1
source

Source: https://habr.com/ru/post/1233239/


All Articles