JSP中的超链接将项添加到购物篮

时间:2016-03-11 00:59:42

标签: java html jsp

我正在尝试将产品(viewProduct.jsp)添加到购物篮(basket.jsp)。 我是JSP的新手。我知道这是通过使用basket.jspviewProduct.jsp的适当超链接来完成的。我在<p>中的viewProduct.jsp标签中写了一个长行,我想要超链接。谢谢

viewProduct.jsp

<%@ page import="shop.Product"%>

<jsp:useBean id='db'
             scope='session'
             class='shop.ShopDB' />

<html>
<head>
<title>My Shop</title>
</head>
<body>
<%
    String pid = request.getParameter("pid");
    Product product = db.getProduct(pid);
    // out.println("pid = " + pid);
    if (product == null) {
        // do something sensible!!!
        out.println( product );
    }
    else {
        %>
        <div align="center">
        <h2> <%= product.title %>  by <%= product.artist %> </h2>
        <img src="<%= product.fullimage %>" />
        <p> <%= product.description %> </p>
        <p> -------------------------- link goes here -------------------</p>
        </div>
        <%
    }
%>
</body>
</html>

basket.jsp

<%@ page import="java.util.Collection,
                 java.util.Iterator"%>

<jsp:useBean id='basket'
             scope='session'
             class='shop.Basket'
        />

<%
  String empty = request.getParameter("emptyBasket");
  if (empty!=null) {
    basket.clearBasket();
  }
  String item = request.getParameter("addItem");
  basket.addItem(item);
%>

<html>

<body>

<% Collection items = basket.getItems();
  for (Iterator i = items.iterator(); i.hasNext(); ) {
    out.println( "<p>" + i.next() + "</p>" );
  }

%>

<p> Order total = <%= basket.getTotalString() %>

    <%
    if ( basket.getTotal() > 0) {
        %>
<form action="order.jsp" method="post">
  <input type="text" name="name" size="20">
  <input type="submit" value="Place Order" />
</form>

<form action="basket.jsp" method="get">
  <input type="hidden" name="emptyBasket" value="yes">
  <input type="submit" value="Empty Basket" />
</form>
<%
  }
%>



</body>

</html>

1 个答案:

答案 0 :(得分:0)

正如您在basket.jsp中看到的,如果您有一个名为addItem的参数,那么它将被添加到basket

那怎么样?

<p> <%= product.description %> </p>
<!-- use pid -->
<a href="basket.jsp?additem="<%=pid%>><%= product.description %></a>

虽然使用JSTL标签会更好。 见http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm