我正在做餐厅菜单分配。对于此任务,我需要让客户下订单。根据老师的要求,我需要使用<forEach:>遍历我的菜单项,设置有限数量的选项,使用"${item.itemNo}"识别要订购的项目并计算订单。

         <table>
                <tr>
                    <th><b>Number</b></th>
                    <th><b>Description</b></th>
                    <th><b>Price</b></th>
                    <th><b>Quantity</b></th>
                </tr>
                <c:forEach var="item" items="${sessionScope.menuList}">
                    <%-- <c:set var="quantity" scope = "session" value = ""/> --%>
                    <tr>
                        <td><c:out value="${item.itemNo}" /></td>
                        <td><c:out value="${item.itemDesc}" /></td>
                        <td><fmt:formatNumber type="currency">
                                <c:out value="${item.itemPrice}" />
                            </fmt:formatNumber></td>
                        <td><select name="${item.itemNo}">
                                <option value="0"></option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8">8</option>
                                <option value="9">9</option>
                                <option value="10">10</option>
                        </select></td>
                    </tr>
                </c:forEach>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td><input type="submit" name="submitOrder" value="Order"></td>
                </tr>
        </table>


我的菜单订购页面如下所示
java - JSP如何将选定的值传递给Servlet?-LMLPHP

我的MenuItem类的一部分

public class MenuItem implements Comparable<MenuItem>, Serializable{
    private static final long serialVersionUID = 1L;

    private int itemNo;
    private String itemDesc;
    private double itemPrice;
    private int quantity;

    public MenuItem() {
        super();
    }
    public MenuItem(int itemNo, String itemDesc, double itemPrice ) {
        this.setItemNo(itemNo);
        this.setItemDesc(itemDesc);
        this.setItemPrice(itemPrice);
        this.setQuantity(0);
    }


我的MenuManager,它生成所有菜单项,采用硬编码:

public class MenuManager {
    private static MenuManager theMenu = null;
    private Map<Integer, MenuItem> menu = null;
    private List<MenuItem> sortedMenu = null;

    synchronized public static MenuManager getInstance() {
        if ( theMenu == null ) {
            theMenu = new MenuManager();
        }
        return theMenu;
    }
    private MenuManager() {
        menu = new ConcurrentHashMap<Integer, MenuItem>();
        menu.put( 22, new MenuItem( 22, "Egg Drop Soup", 3.00));
        menu.put( 14, new MenuItem( 14, "WonTon Noodle Soup", 5.00));
        menu.put(  6, new MenuItem(  6, "Egg Roll", 1.20));
        menu.put( 17, new MenuItem( 17, "Spring Roll", 1.70));
        menu.put(207, new MenuItem(207, "Roast Duck", 16.00));
        menu.put( 77, new MenuItem( 77, "Shrimp with Snow Pea", 10.50));
        menu.put( 98, new MenuItem( 98, "Beef with Brocolli", 8.40));
        menu.put(108, new MenuItem(108, "Beef Ginger", 9.00));
        menu.put(210, new MenuItem(210, "Moo Goo Stir Fry", 7.50));
        menu.put(123, new MenuItem(123, "Lemon Chicken", 9.25));
        menu.put(114, new MenuItem(114, "Sweet&Sour Ribs", 6.50));
        menu.put(132, new MenuItem(132, "Sweet&Sour Chicken Balls", 8.60));
        menu.put( 50, new MenuItem( 50, "Boiled Rice", 1.50));
        menu.put( 63, new MenuItem( 63, "Vegetable Fried Rice", 3.00));
        sortedMenu = new LinkedList<MenuItem>();
        sortedMenu.addAll( menu.values() );
        Collections.sort( sortedMenu );
    }


    public List<MenuItem> getMenu() {
        return sortedMenu;
    }

    public MenuItem getMenuItem( Integer key ) {
        return menu.get(key);
    }

  public static void main( String[] args) {
      MenuManager mm = MenuManager.getInstance();
      for ( MenuItem mi : mm.getMenu() ) {
          System.out.println( mi);
      }
  }
}


客户下订单后,下一页应如下所示:
java - JSP如何将选定的值传递给Servlet?-LMLPHP

但是在我的Serlvet中,每当我调用request.getParameter("${item.itemNo}")时,它始终为null。我该如何存储用户订购的商品及其数量?

最佳答案

而不是request.getParameter("${item.itemNo}"),您应该使用MenuManager中的某个实数进行检查。您要在itemNo(即数字)之后命名所选字段。

要检查所有响应,您应该按照以下方式进行:

for(MenuItem menuItem : MenuManager.getInstance().getMenu()){
   String value = request.getParameter("" + menuItem.getItemNo());
   //do some processing with value
}

10-04 10:07