在下拉列表中显示数据库中的选定数据

时间:2014-02-03 09:34:51

标签: java database servlets drop-down-menu foreach

我的目标是在数据库中选择数据并在下拉列表中显示。

例如,见下图显示fbMenuId = M001(烤宽面条)。

database1

因此,在下拉列表中将选择M001选项。我还需要显示其他菜单,如M002,M003,M004,M005,M006和M007。例如,请参见下图

database

然而,我的结果是

ddl

以下是我的代码

<select class="form-control" name="menu" id="menu">
                                            <option value="${order.fbMenuId}" selected>${order.fbMenuName}</option>
                                            <c:forEach var="menu" items="${menu}">
                                                <option value="${menu.fbMenuId}">${menu.fbMenuName}</option>
                                            </c:forEach>

</select>

我可以展示M001烤宽面条。但是,我不想要2个烤宽面条。有人请帮助我。帮助将是欣赏。提前谢谢!

下面是servlet和数据访问对象的代码。

Servlet

OrderDAO dao = new OrderDAO();
request.setAttribute("order", dao.getOrder(fbOrderId));
request.setAttribute("menu", dao.getMenu(restaurant));

OrderDAO

public OrderBean getOrder(Integer fbOrderId) {

            OrderBean ob = new OrderBean();
            try {
                currentCon = ConnectionManager.getConnection();
                Statement statement = currentCon.createStatement();
                ResultSet rs = statement.executeQuery("SELECT fborders.fbMenuId, fbMenuName FROM fborders INNER JOIN fbmenu ON fborders.fbMenuId = fbmenu.fbMenuId WHERE fbOrderId='"+ fbOrderId + "'");

                while (rs.next()) {
                    ob.setFbMenuId(rs.getString("fbMenuId"));
                    ob.setFbMenuName(rs.getString("fbMenuName"));
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

            return ob;
        }



public ArrayList getMenu(String restaurant) {

        ArrayList<OrderBean> am = new ArrayList<OrderBean>();
        try {
            currentCon = ConnectionManager.getConnection();
            Statement statement = currentCon.createStatement();
            ResultSet rs = statement
                    .executeQuery("SELECT fbMenuId, fbMenuName FROM fbmenu WHERE fbRestaurantId='"
                            + restaurant + "'");

            while (rs.next()) {
                OrderBean ob = new OrderBean();
                ob.setFbMenuId(rs.getString("fbMenuId"));
                ob.setFbMenuName(rs.getString("fbMenuName"));
                am.add(ob);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return am;
    }

2 个答案:

答案 0 :(得分:1)

根据我的理解,您将显示所选值两次。一次附加

  <option value="${order.fbMenuId}" selected>${order.fbMenuName}</option>

和另一次迭代列表。而不是填充下拉列表中的所有值,并将所需的值设置为选中。只需写下如下的简单条件。

   <select class="form-control" name="menu" id="menu">         
      <c:forEach var="menu" items="${menu}">
          <option value="${menu.fbMenuId}">${menu.fbMenuName} 
          <c:if test="${menu.fbMenuId == order.fbMenuId}">
           selected
           </c:if>
          </option>
      </c:forEach>
   </select>

答案 1 :(得分:0)

<select class="form-control" name="menu" id="menu">
    <c:forEach var="menu" items="${menu}">
        <c:choose>
            <c:when test="${menu.fbMenuId == order.fbMenuId}">
                <option value="${order.fbMenuId}" selected>${order.fbMenuName}</option> 
            </c:when>
            <c:otherwise>
                <option value="${menu.fbMenuId}">${menu.fbMenuName} </option>
            </c:otherwise>
        </c:choose>
    </c:forEach>
</select>