⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shopping_cart_doc_2.txt

📁 购物车简单教材
💻 TXT
字号:
------------------------------学会创建动态的Action Form和使用DispatchAction1.创建index.jsp   在cart(web应用名)-->Root directory右键,new->jsp,在出现的界面中输入:   name:index   然后[next]->[next]->[next]->finish------------------------index.jsp------------------------------------<%@ page contentType="text/html; charset=GBK" %><%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><html><head><title>index</title></head><body bgcolor="#ffffff"><h1 align="center">本系统的所有商品列表如下:<br><br><table width="300" border="1"><tr height="25"><td>产品名称</td><td>添加到购物车</td></tr><c:forEach var="product" items="${allProduct}"><tr><td><c:out value="${product.value.name}"/></td><td><a href="/cart/cart.do?method=add&productId=<c:out value="${product.value.id}"/>">添加到购物车</td></tr></c:forEach></table><br><a href="cart.jsp">查看购物车</a></h1></body></html>编译,运行,测试2.创建cart.jsp源文件如下:--------------------------------------cart.jsp------------------------------------------------------<%@ page contentType="text/html; charset=GBK" %><%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><%@ taglib uri="WEB-INF/struts-html.tld" prefix="html"%><html><head><title>我的购物车</title><script language="javascript" type="text/javascript">	function deleteCartItem(formname){                formname.action = "/cart/cart.do?method=delete";		formname.submit();	}	function modifyNumber(formname){		formname.action = "/cart/cart.do?method=modify";		formname.submit();	}</script></head><body bgcolor="#ffffff"><h1 align="center"> 购物车 </h1><html:form action="/cart.do?method=clear" >  <table width="75%" border="1" align="center" cellpadding="0" cellspacing="0">    <tr align="center">    <td width="7%">选择</td>      <td width="21%" height="30">商品名称</td>      <td width="14%" height="30">单价</td>      <td width="25%" height="30">数量</td>      <td width="33%" height="30">合计</td>  </tr>  <c:forEach var="cartItem" items="${cart.cartItem}">     <tr align="center">      <td height="30">	<input type="checkbox" name="item" value='<c:out value="${cartItem.key}"/>'>      </td>      <td height="30"><c:out value="${cartItem.value.product.name}"/></td>      <td height="30"><c:out value="${cartItem.value.product.price}"/></td>      <td height="30"><input type="text" name="number" value='<c:out value="${cartItem.value.number}"/>' maxlength="10" size="10"></td>      <td height="30"><c:out value="${cartItem.value.cost}"/></td>     </tr>  </c:forEach>  <tr>      <td height="30" colspan="5">您的购物车中所有商品总金额:<c:out value="${cart.cost}"/></td>  </tr></table>  <br><br>  <table width="450" border="0" align="center" cellpadding="0" cellspacing="0">    <tr align="center">      <td width="22%"> <html:button property="modify" value="修改数量" onclick="modifyNumber(this.form)"/></td>      <td width="20%"><html:button property="delete" value="删除所选项" onclick="deleteCartItem(this.form)"/></td>      <td width="26%"><html:submit value="清空购物车"/></td>      <td width="20%"><html:button value="继续购物" property="continue" onclick="javascript:location.href='index.jsp'"/></td>    </tr>  </table>  <p align="center">&nbsp;</p></html:form></body></html>3.创建动态的ActionForm    双击界面左上方的<cart>(web应用名)--〉<struts1.1>-->struts-config.xml文件,    这时在界面的左下方出现的框中点开Form Beans选项,    在右边的界面中点[Add],输入ActionForm的名称:cartActionForm,    然后再看左下方的界面中Form Beans下边出现了一个cartActionForm,双击它,在右边的界面中进行如下配置:        Name  :   cartActionForm    Implementation class   :    org.apache.struts.action.DynaActionForm    is dynabean     :    true(Jbuilder2005没有这个选项)    在Form Properties框中点击[Add]添加属性:    name:productId            type:java.lang.String     name:item                     type:java.lang.String[]    name:number                type:java.lang.Integer[]4.创建CartAction.java    选择file->new->web->action,在出现的界面中输入如下:    WebApp   :  cart    struts config  :   WEB-INF/struts-config.xml    package  :  cart.action    Action   :   CartAction注意:****    Base class   :    org.apache.struts.actions.DispatchAction   点击[next],在出现的界面中输入:   Action path   :   /cart   FormBean name  :   cartActionForm    Scope     :     request    Validate FormBean    :     false    input  JSP     :     /cart.jsp然后[finish]修改struts-config.xml文件双击界面左上方的<cart>(web应用名)--〉<struts1.1>-->struts-config.xml文件,这时在界面的左下方出现的框中点开Action Mappings选项,看到下面包含一个/cart,双击它,在界面的右方出现/cart的配置界面,在此界面中找到Parameter一项,在其后的文本框中输入method,在界面下方的几个标签中选择《forwards》,点击[Add],	path    :    /cart.jsp                     name   :     cartIndex  然后保存。5.打开CartAction.java    选择perform方法的所有代码,将其删除,拷贝如下代码放入CartAction.java中。  public ActionForward clear(ActionMapping actionMapping, ActionForm actionForm,                             HttpServletRequest httpServletRequest,                             HttpServletResponse httpServletResponse) {    HttpSession session = httpServletRequest.getSession();    Cart cart = new Cart();    session.removeAttribute("cart");    session.setAttribute("cart", cart);    return actionMapping.findForward("cartIndex");  }  public ActionForward delete(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {    DynaActionForm cartActionForm = (DynaActionForm) actionForm;    String[] item = (String[])cartActionForm.get("item");    if (item != null) {      Integer[] itemPid = new Integer[item.length];      for (int i = 0; i < itemPid.length; i++) {        itemPid[i] = new Integer(item[i]);      }      HttpSession session = httpServletRequest.getSession();      Cart cart = (Cart) session.getAttribute("cart");      cart.deleteItemsByProductId(itemPid);      session.removeAttribute("cart");      session.setAttribute("cart", cart);    }    return actionMapping.findForward("cartIndex");  }  public ActionForward modify(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {    DynaActionForm cartActionForm = (DynaActionForm) actionForm;    String[] item = (String[])cartActionForm.get("item");    Integer[] numbers = (Integer[])cartActionForm.get("number");    Integer productId = null;    int number = 0;    if (item != null) {      HttpSession session = httpServletRequest.getSession();      Cart cart = (Cart) session.getAttribute("cart");      for (int i = 0; i < item.length; i++) {        productId = new Integer(item[i]);        number = numbers[i].intValue();        cart.modifyNumberByProductId(productId, number);      }      session.removeAttribute("cart");      session.setAttribute("cart", cart);    }    return actionMapping.findForward("cartIndex");  }  public ActionForward add(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {    System.out.println("in add");    DynaActionForm cartActionForm = (DynaActionForm) actionForm;    Integer productId = new Integer((String)cartActionForm.get("productId"));    ServletContext sc = httpServletRequest.getSession().getServletContext();    Product product = (Product) ( (HashMap) sc.getAttribute("allProduct")).get(productId);    HttpSession session = httpServletRequest.getSession();    Cart cart = (Cart) session.getAttribute("cart");    cart.addItem(product, 1);    session.removeAttribute("cart");    session.setAttribute("cart", cart);    return actionMapping.findForward("cartIndex");  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -