10

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 ?

4 Answers 4

7

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>
Sign up to request clarification or add additional context in comments.

1 Comment

in my case i need to give index dynamically...not in for loop...then how can we do it ?
0

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

3 Comments

landingPageModels is a list not landingPageModels.landingPage, then how can we write like this - landingPageModels.landingPage[0] ?
Then it would be landingPageModels[0].landingPage.url and iterate through indexes like @shaleindra suggested.
Corrected the expression.
0
<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.

enter image description here

3 Comments

That won't work you will have to use the varStatus to fill the path. It will work for displaying not for binding back on a submit.
@M.Deinum Can you please answer my question that how can i do solve the problem. I am stuck in this from 6 days.
i dont want to use for loop....how can i bind data in list element without for loop ?
-2

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

}

2 Comments

then how can i get them in controller ?
after doing that its giving me this error - HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'landingPageModels[0]' of bean class [com.mogae.AdTag.model.CampaignModel]: Index of out of bounds in property path 'landingPageModels[0]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.