jspdrop-down-menujstljspx

Populate drop down menu from database


I have this method in my controller to get all the created floors so that I can use it in creating a room record.

	@RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid Room room, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, room);
            return "rooms/create";
        }
        uiModel.asMap().clear();
        roomService.saveRoom(room);
        return "redirect:/rooms/" + encodeUrlPathSegment(room.getId().toString(), httpServletRequest);
    }

	@RequestMapping(params = "form", produces = "text/html")
    public String createForm(Model uiModel) {
        populateEditForm(uiModel, new Room());
        return "rooms/create";
    }





	void populateEditForm(Model uiModel, Room room) {
        uiModel.addAttribute("room", room);
        uiModel.addAttribute("floors", Floor.findAllFloors());
    }

Then, I originally have this code in my views page to display the floors in a dropdown list:

<field:select field="floor"
id="c_ph_com_smesoft_hms_domain_Room_floor" itemValue="id"
items="${floors}" path="/floors" z="BO2RLJSaIYxNwRbKJMRipi883S8=" />
							

The code above works perfectly fine. How to translate this code in to this format:

<select>
<c:forEach>
<option></option>
</c:forEach>
</select> 


Solution

  • It think it should be like this.

    <select name="floor">
    <c:forEach items="floors" var="floor">
    <option value="${floors.id}">${floors}</option>
    </c:forEach>
    </select>