从数据库填充下拉菜单

时间:2016-09-24 12:26:52

标签: jsp drop-down-menu jstl jspx

我在我的控制器中有这个方法来获取所有创建的楼层,以便我可以用它来创建房间记录。



	@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());
    }




然后,我最初在我的视图页面中显示此代码,以在下拉列表中显示楼层:



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

上面的代码非常好用。如何将此代码翻译成以下格式:

&#13;
&#13;
<select>
<c:forEach>
<option></option>
</c:forEach>
</select> 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

它认为它应该是这样的。

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