JSP如何将选定的值传递给Servlet?

时间:2018-10-28 18:54:25

标签: java jsp servlets

我正在处理餐厅菜单分配。对于此任务,我需要让客户下订单。根据老师的要求,我需要使用<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>

我的菜单顺序页面如下所示 enter image description here

我的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);
      }
  }
}

客户下订单后,下一页应如下所示: enter image description here

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

1 个答案:

答案 0 :(得分:1)

您应该使用MenuManager中的某些实数代替printf("caller - address of tmp1:%p \n", (void*)&tmp1);进行检查。您正在将所选字段命名为itemNo,即数字。

要检查所有回复,请按照以下方式进行操作:

request.getParameter("${item.itemNo}")