从数据库填充下拉菜单
Populate drop down menu from database
我的控制器中有这个方法来获取所有创建的楼层,以便我可以用它来创建房间记录。
@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=" />
上面的代码工作得很好。如何将这段代码翻译成这种格式:
<select>
<c:forEach>
<option></option>
</c:forEach>
</select>
觉得应该是这样的
<select name="floor">
<c:forEach items="floors" var="floor">
<option value="${floors.id}">${floors}</option>
</c:forEach>
</select>
我的控制器中有这个方法来获取所有创建的楼层,以便我可以用它来创建房间记录。
@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=" />
上面的代码工作得很好。如何将这段代码翻译成这种格式:
<select>
<c:forEach>
<option></option>
</c:forEach>
</select>
觉得应该是这样的
<select name="floor">
<c:forEach items="floors" var="floor">
<option value="${floors.id}">${floors}</option>
</c:forEach>
</select>