简体   繁体   中英

Spring MVC Form binding a group of two or more fields into a list of objects

I am trying to bind a bunch of fields into a list of objects in my form using Spring MVC 3.1 and validating using Bean Validation.

But I don't know how to bind multiple fields into a single object and into a list of these objects.

HTML Form:

<form method="post" action="#" id="phoneForm">
    <table>
        <tr>
            <td><label for="mobilePhone">Mobile</label></td>
            <td>
                <input type="text" class="mp_phone" name="mobilePhone" id="mobilePhone" value="" />
            </td>
            <td><label for="mobilePhone_p">Preferred phone number</label> <input type="radio" name="num_p" id="mobilePhone_p" value="mobilePhone_p" /></td>
        </tr>
        <tr>
            <td><label for="personalPhone">Personal</label></td>
            <td>
                <input type="text" class="mp_phone" name="personalPhone" id="personalPhone" value="" />
            </td>
            <td><label for="personalPhone_p">Preferred phone number</label> <input type="radio" name="num_p" id="personalPhone_p" value="personalPhone_p" /></td>
        </tr>
        <tr>
            <td><label for="workPhone">Work</label></td>
            <td>
                <input type="text" class="mp_phone" name="workPhone" id="workPhone" value="" />
            </td>
            <td><label for="workPhone_p">Preferred phone number</label> <input type="radio" name="num_p" id="workPhone_p" value="workPhone_p" /></td>
        </tr>
    </table>
    <button type="submit" id="validateFormButton">Submit</button>
</form>

My Form :

public class PhoneForm {    
    @NotEmpty
    @Valid
    private List<Phone> phonesList = new LinkedList<Phone>();

    // getter/setter...
}

Phone class:

public class Phone {        
    @PhoneNumber
    protected String phoneNumber;

    @NotNull
    protected Boolean prefferedNumber;

    // getter/setter...
}

I would like to create :

  • an instance of Phone with mobilePhone and mobilePhone_p form params.
  • an instance of Phone with personalPhone and personalPhone_p form params.
  • an instance of Phone with workPhone and workPhone_p form params.

Each one of this instances should be added to PhoneForm 's list.

How to do that ?

I do this form structure to allow validating my bean just with an annotation : @Valid PhoneForm phoneForm in my Spring MVC Controller. I am using a list because I would like to validate at least one phone set by user.

This may not be the best technique for this, but here is how I would implement a solution for your question.

  1. Create a field for each of the phone fields in the PhoneForm. This will be useful, if for nothing else, to set the value on your page.

  2. Create a method in the PhoneForm class (perhaps called getMobilePhoneList()) that creates a list of Phone objects for the mobile phones.

  3. Create a method in the PhoneForm class that creates a list of Phone objects for the personal phones.

  4. Create a method in the PhoneForm class that creates a list of Phone objects for the work phones.

  5. Use the PhoneForm getters to populate fields on the page and during validation.

  6. Use the getxxxPhoneList() methods in java when you need them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM