Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind data to list in spring form

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

<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 backing 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 is -

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 is -

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 that i can insert many objects of landingPage (having their own url property) in landingPageModels list. That means i can have mulitple input tag having 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 error that landingPage property of landingPageModels has not getter setter method. How to solve it and how to take multiple value like this ?

like image 795
Amit Das Avatar asked Oct 08 '15 12:10

Amit Das


People also ask

What is data binder in spring?

Data binding is useful for allowing user input to be dynamically bound to the domain model of an application (or whatever objects you use to process user input). Spring provides the so-called DataBinder to do exactly that.

What is form binding?

Form binding is like a buffer. It automatically creates a middle object. Before saving to ViewModel all input data is saved to the middle object. This way we can keep dirty data from saving into the ViewModel before user confirmation.


1 Answers

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>
like image 116
sh0rug0ru Avatar answered Oct 19 '22 11:10

sh0rug0ru