第9章 JSP标记库----补充-实验与训练指导

创建一个基于JSTL的运如图行结果9.25所示。

image

在图9.25中,单击Books超链接,如图9.26所示

image

在图9.26中,单击BUY超链接,如图9.27所示

image

在图9.27中,单周Clear the cart 超链接,如图9.28所示

image

在图9.28中,单击return to Shopping 超链接,如图9.25所示。

image

1.创建4个类文件

package com.wrox.begjsp.ch03;

public class Product
{
  private String sku;
  private String name;
  private String desc;
  private long price;

  public Product()
  {
  }

  public Product(String sku, String name, String desc, long price)
  {
    this.sku = sku;//sku 类似于产品ID
    this.name = name;
    this.desc = desc;
    this.price = price;
  }
  public String getSku() {
    return this.sku;
  }

  public void setSku(String sku) {
    this.sku = sku;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getDesc() {
    return this.desc;
  }

  public void setDesc(String desc) {
    this.desc = desc;
  }

  public long getPrice() {
    return this.price;
  }

  public void setPrice(long price) {
    this.price = price;
  }
}

2.Category.java

package com.wrox.begjsp.ch03;

public class Category
{
  private String id;
  private String name;

  public Category()
  {
  }

  public Category(String id, String name)
  {
    this.id = id;
    this.name = name;
  }
  public String getId() {
    return this.id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

3.LineItem.java

package com.wrox.begjsp.ch03;

public class LineItem
{
  private int quantity;
  private String sku;
  private String desc;
  private long price;

  public LineItem()
  {
  }

  public LineItem(int quantity, String sku, String desc, long price)
  {
    this.quantity = quantity;
    this.sku = sku;
    this.desc = desc;
    this.price = price;
  }

  public int getQuantity() {
    return this.quantity;
  }

  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }

  public String getSku() {
    return this.sku;
  }

  public void setSku(String sku) {
    this.sku = sku;
  }

  public String getDesc() {
    return this.desc;
  }

  public void setDesc(String desc) {
    this.desc = desc;
  }

  public long getPrice() {
    return this.price;
  }

  public void setPrice(long price) {
    this.price = price;
  }
}

4.EShop.java

package com.wrox.begjsp.ch03;

import java.util.ArrayList;
import java.util.List;

public class EShop
{
  public static ArrayList getCats()
  {
    ArrayList values = new ArrayList();

    values.add(new Category("1", "Systems"));
    values.add(new Category("2", "Software"));
    values.add(new Category("3", "Books"));
    return values;
  }

  public static ArrayList getItems(String catid) {
    ArrayList values = new ArrayList();
    if (catid.equals("1")) {
      values.add(new Product("232", "Pentium 4 - 4 GHz, 512 MB, 300 GB", "", 98999L));

      values.add(new Product("238", "AMD Opteron - 4 GHz, 1 GB, 300 GB", "", 120099L));
    }
    else if (catid.equals("2")) {
      values.add(new Product("872", "Tomcat 5 Server for Windows", "", 9900L));

      values.add(new Product("758", "Tomcat 5 Server for Linux", "", 9900L));
    }
    else if (catid.equals("3")) {
      values.add(new Product("511", "Beginning JavaServer Pages", "", 3999L));

      values.add(new Product("188", "Professional Apache Tomcat 5", "", 4999L));

      values.add(new Product("148", "Apache Tomcat Bible", "", 4999L));
    }

    return values;
  }

  public static Product getItem(String sku) {
    ArrayList cats = getCats();
    Product foundProd = null;
    for (int i = 0; i < cats.size(); ++i) {
      Category curCat = (Category)cats.get(i);
      ArrayList items = getItems(curCat.getId());
      for (int j = 0; j < items.size(); ++j) {
        Product curProd = (Product)items.get(j);
        if (curProd.getSku().equals(sku)) {
          foundProd = curProd;
          break;
        }
      }
      if (foundProd != null) {
        break;
      }
    }

    return foundProd;
  }

  public static void clearList(List list)
  {
    list.clear();
  }
  public static void addList(List list, Object item) {
    list.add(item);
  }
}

2.创建\WEB-INF\jsp\eshop-taglib.tld文件

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>A taglib for eshop functions.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>EShopunctionTaglib</short-name>
    <uri>EShopFunctionTagLibrary</uri>
    <function>
        <description>Obtain the catalog categories</description>
        <name>getCats</name>
    <function-class>com.wrox.begjsp.ch03.EShop</function-class>
    <function-signature>java.util.ArrayList getCats()</function-signature>
    </function>

   

<function>
        <description>Obtain the items in a category</description>
        <name>getItems</name>
    <function-class>com.wrox.begjsp.ch03.EShop</function-class>
    <function-signature>java.util.ArrayList getItems(java.lang.String)</function-signature>
    </function>


<function>
        <description>Obtain an item given an sku</description>
        <name>getItem</name>
    <function-class>com.wrox.begjsp.ch03.EShop</function-class>
    <function-signature>com.wrox.begjsp.ch03.Product getItem(java.lang.String)</function-signature>
    </function>

 

<function>
        <description>Clear a list</description>
        <name>clearList</name>
    <function-class>com.wrox.begjsp.ch03.EShop</function-class>
    <function-signature>void clearList(java.util.List)</function-signature>
    </function>


<function>
        <description>Add an item to a list</description>
        <name>addList</name>
    <function-class>com.wrox.begjsp.ch03.EShop</function-class>
    <function-signature>void addList(java.util.List, java.lang.Object)</function-signature>
    </function>

</taglib>

3.创建JSP文件

  (1)estore.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="wxshop" uri="EShopFunctionTagLibrary" %>
<%@ page pageEncoding="GBK"%>
<%@ page  session="true" %>
<c:if test="${empty cats}">
  <c:set var="cats" value="${wxshop:getCats()}" scope="application"/>
</c:if>

<html>
<head>
<title>PFC Shopping Mall</title>
<link rel=stylesheet type="text/css" href="store.css">
</head>
<body>
<table width="600">
<tr><td colspan="2" class="mainHead">PFC JSTL Web Store</td></tr>

<tr>
<td width="20%">
<!-- left three category -->
<c:forEach var="curCat" items="${cats}">
<c:url value="/example1/estore.jsp" var="localURL">
   <c:param name="catid" value="${curCat.id}"/>
</c:url>
<a href="${localURL}" class="category">${curCat.name}</a>
</br>
</c:forEach>
</td>
<td width="*">
<h1></h1>
<table border="1" width="100%">
<tr><th align="left">Item</th><th align="left">Price</th><th align="left">Order</th></tr>
<c:set var="selectedCat"  value="${param.catid}"/>
<c:if test="${empty selectedCat}">
  <c:set var="selectedCat"  value="1"/>
</c:if>
<c:forEach var="curItem" items="${wxshop:getItems(selectedCat)}">
  <tr>
   <td>${curItem.name}</td>
   <td align="right">
       <fmt:formatNumber value="${curItem.price / 100}" type="currency"/>
   </td>
   <td>
     <c:url value="/example1/shopcart.jsp" var="localURL">
          <c:param name="action" value="buy"/>
          <c:param name="sku" value="${curItem.sku}"/>
     </c:url>
   <a href="${localURL}"><b>BUY</b></a>
  </td>
</tr>
</c:forEach>
</table>
</td>
</tr>
</table>

</body>
</html>

2.shopcart.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="wxshop" uri="EShopFunctionTagLibrary" %>
<%@ page pageEncoding="GBK"%>
<%@ page session="true" %>

<c:set var="EXAMPLE" value="/example1"/>
<c:set var="SHOP_PAGE" value="/estore.jsp"/>
<c:set var="CART_PAGE" value="/shopcart.jsp"/>

<html>
<head>
<title>PFC Shopping Mall - Shopping Cart</title>
<link rel=stylesheet type="text/css" href="store.css">
</head>
<body>
<c:if test="${!(empty param.sku)}">
  <c:set var="prod" value="${wxshop:getItem(param.sku)}"/>
</c:if>

<jsp:useBean id="lineitems" class="java.util.ArrayList" scope="session"/>

<c:choose>
    <c:when test="${param.action == 'clear'}">
       ${wxshop:clearList(lineitems)}
    </c:when>

    <c:when test="${param.action == 'inc' || param.action=='buy'}">
       <c:set var="found" value="false"/>
  
       <c:forEach var="curItem" items="${lineitems}">

            <c:if test="${(curItem.sku) == (prod.sku)}">
              <jsp:setProperty name="curItem" property="quantity"
value="${curItem.quantity + 1}"/>
              <c:set var="found" value="true" />
            </c:if>       
       </c:forEach>
       <c:if test="${!found}">
             <c:remove var="tmpitem"/>
             <jsp:useBean id="tmpitem" class="com.wrox.begjsp.ch03.LineItem">
             <jsp:setProperty name="tmpitem" property="quantity" value="1"/>
             <jsp:setProperty name="tmpitem" property="sku" value="${prod.sku}"/>
             <jsp:setProperty name="tmpitem" property="desc" value="${prod.name}"/>
             <jsp:setProperty name="tmpitem" property="price" value="${prod.price}"/>
              </jsp:useBean>
         ${wxshop:addList(lineitems, tmpitem)}
       </c:if>
      </c:when>
     </c:choose> 

<c:set var="total" value="0"/>
<table width="640">
    <tr><td class="mainHead">PFC JSTL Web Store</td></tr>
<tr>
<td>
<h1></h1>
<table border="1" width="640">

<tr><th colspan="5" class="shopCart">Your Shopping Cart</th></tr>
<tr><th align="left">Quantity</th><th align="left">Item</th><th align="right">Price</th>
<th align="right">Extended</th>
<th align="left">Add</th></tr>
<c:forEach var="curItem" items="${lineitems}">
<c:set var="extended" value="${curItem.quantity * curItem.price}"/>
<c:set var="total" value="${total + extended}"/>  
<tr>
   <td>${curItem.quantity}</td>
   <td>${curItem.desc}</td>
   <td align="right">
     <fmt:formatNumber value="${curItem.price / 100}" type="currency"/>
   </td>
   <td align="right">
     <fmt:formatNumber value="${extended / 100}" type="currency"/>
   </td>
   <td>

<c:url value="${EXAMPLE}${CART_PAGE}" var="localURL">
   <c:param name="action" value="inc"/>
   <c:param name="sku" value="${curItem.sku}"/>
</c:url>
<a href="${localURL}"><b>Add 1</b></a>
   </td>
</tr>
</c:forEach>
<tr>
<td colspan="5"> &nbsp;
</td>
</tr>
<tr>
<td colspan="3" align="right"><b>Total:</b></td>
<td align="right" class="grandTotal">
<fmt:formatNumber value="${total / 100}" type="currency"/>
</td>
<td>&nbsp;</td>
</tr>

<tr>
<td colspan="5">
<c:url value="${EXAMPLE}${CART_PAGE}" var="localURL">
   <c:param name="action" value="clear"/>
</c:url>
<a href="${localURL}">Clear the cart</a>
</td>
</tr>


<tr>
<td colspan="5">
<c:url value="${EXAMPLE}${SHOP_PAGE}" var="localURL"/>
<a href="${localURL}">Return to Shopping</a>
</td>
</tr>
</table>
</td></tr>
</table>
</body>
</html>

posted @ 2014-12-10 22:27  elite_2012  阅读(187)  评论(0编辑  收藏  举报