2

JSP

<select name="requirements"  style="width:150px;" id="requirements1">
                                        <option selected="selected" value="o Special Assistance">No Special Assistance</option>
                                        <option value="Assistance climbing steps">Assistance climbing steps</option>
                                        <option value="Assistance within the plane">Assistance within the plane</option>
                                        <option value="Blind passenger">Blind passenger</option>
                                        <option value="Hearing impaired">Hearing impaired</option>
                                        <option value="Wheelchair within terminal">Wheelchair within terminal</option>
                                    </select>
<select name="requirements"  style="width:150px;" id="requirements2">
                                        <option selected="selected" value="o Special Assistance">No Special Assistance</option>
                                        <option value="Assistance climbing steps">Assistance climbing steps</option>
                                        <option value="Assistance within the plane">Assistance within the plane</option>
                                        <option value="Blind passenger">Blind passenger</option>
                                        <option value="Hearing impaired">Hearing impaired</option>
                                        <option value="Wheelchair within terminal">Wheelchair within terminal</option>
                                    </select><input type="text" name="specialrequest" id="specialrequest1" />
        </form>

Class

public class PassengerDetailDTO {

    private String[] requirements;
    private String specialrequest;
}

and in controller

@RequestMapping(value = "/manualbooking.htm", method = RequestMethod.POST)
    public String doManualBooking(HttpServletRequest httpServletRequest,
            PassengerDetailDTO passengerDetailDTO,  ModelMap map) {
        LOGGER.info("Manual Booking Request");

        LOGGER.info(passengerDetailDTO.toString());

        return "manualbooking";
    }

When there is more than one dropdown boxes, After submitting form requirements are always null. How to solve it ?

Vinit Prajapati
  • 1,593
  • 1
  • 17
  • 29
  • Is the number of selects defined (e.g. 2), or will this number be variable? – sp00m Dec 19 '12 at 13:21
  • 1
    This is quite hard to manage, you could have a look at [this answer](http://stackoverflow.com/a/9993144/1225328) for a possible solution. – sp00m Dec 19 '12 at 13:27

2 Answers2

1

Make sure your <select> box names represents array []. For example:

<select name="requirements[0]" ..>

<select name="requirements[1]" ..>

Check this demo: Spring MVC Multi Row Form.

Viral Patel
  • 8,506
  • 3
  • 32
  • 29
-1

You could add 2 different String fields to your backing bean, ie

private String requirement1;
private String requirement2;

and then use a construct akin to this

 <form:select path="requirement1" multiple="false" id="requirement1">
    <form:options items="${yourRequirementsAsACollection}" />
 </form:select>


 <form:select path="requirement2" multiple="false" id="requirement2">
    <form:options items="${yourRequirementsAsACollection}" />
 </form:select>

to write the selected value into the first or second field, respectivly.

On the other hand you could use a multi-select-box, in this case, all selected values would end up comma-separated in the single field.

 private String requirements; // Ends up comma-separated, ie "req1, req2"


 <form:select path="requirements" multiple="true" id="requirements">
    <form:options items="${yourRequirementsAsACollection}" />
 </form:select>
Scorpio
  • 2,309
  • 1
  • 27
  • 45