Question

This is my list mealPlanDescription - [23, Bed & Breakfast, 24, Half Board, 26, Full Board].

I have to assign option value = 23, option description, Bed & Breakfast, eg:

<option value="23">Bed & Breakfast</option> 

by this way i have to assign all thing from list

my jsp code

                           <select id="mealPlan" name="mealPlan">
                                <option value="N/A" selected="selected">--Select--</option>
                                <c:forEach var="mealPlan" items="${mealPlansRequestNow}">
                                    <option value="${mealPlan}">${mealPlan}</option>
                                </c:forEach>
                            </select>   

                            <label class="select-arrow"></label>
                        </div>

${mealPlansRequestNow} it's map a model in backend with that list model.addAttribute("mealPlansRequestNow", mealPlanDescription);

but these code given options value as dropdown- 23, Bed & Breakfast, 24, Half Board, 26, Full Board

i need value, description separately.

Was it helpful?

Solution

Do like this

Use Map instead of list.

Server side change

Map<String, String> mealPlans = new HashMap<String, String>();
mealPlans.put("23", "Bed & Breakfast");
mealPlans.put("24", "Half Board");
mealPlans.put("26", "Full Board");

model.addAttribute("mealPlans ", mealPlans);

JSP changes

<c:forEach var="mealPlan" items="${mealPlans}">
     <option value="${mealPlan.key}">${mealPlan.value}</option>
</c:forEach>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top