📄 day08.txt
字号:
import ejbs.ShoppingCartLocal;
public class BuyProductAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
BuyProductForm form = (BuyProductForm) actionForm;
try{
ProductMgmDelegate pd = new ProductMgmDelegate();
ProductDTO product =
pd.getProductById( form.getProductId() );
OrderLine line= new OrderLine(
product,
Integer.parseInt( form.getAmount() ));
ShoppingCartLocal cart = ( ShoppingCartLocal) request.getSession().
getAttribute( ShoppingCartLocal.CART_ATTR);
cart.addLine( line );
return actionMapping.findForward( "success" );
}catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( CreateException ce ){
ce.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( FinderException fe ){
fe.printStackTrace();
return actionMapping.findForward( "systemError" );
}
}
}
=========================================================
10,为BuyProductAction 配置 forward
a) when success
path : /index.jsp
name : success
b) when some exceptions be caught
path : /fail.jsp
name : systemError
=========================================================
11, 为displayCart.do 创建Action
file --> new --> web --> Action
package : ec_port_web
Action : DisplayCartAction
--> next
Aciton Path : /displayCart
formbean name :
scope :
validate :
input jsp : index.jsp
----> finish
=========================================================
12 , 修改perform() 方法
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.Collection;
import dto.*;
import ejbs.ShoppingCartLocal;
public class DisplayCartAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
ShoppingCartLocal cart = ( ShoppingCartLocal ) request.getSession().
getAttribute( ShoppingCartLocal.CART_ATTR);
Collection col = cart.getAll();
String sum = cart.getCost() + "";
request.setAttribute( "CART" , col );
request.setAttribute( "SUM" , sum );
return actionMapping.findForward( "success" );
}
}
=========================================================
13, 为DisplayCartAction 配置forward
path : /displayCart.jsp
name : success
=========================================================
14, 创建 displayCart.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@page import="java.util.*,dto.*"%>
<%
Collection cart =
(Collection)request.getAttribute( "CART" );
String sum = ( String ) request.getAttribute( "SUM" );
%>
<html>
<head>
<title>我的购物车</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table width="760" border="0" cellspacing="0"
cellpadding="0" align="center" height="39"
bordercolor="#007cb9">
<tr>
<td height="400" align=center> <p><b>
您的购物车中有下列商品: </b></p>
<p><b>价格单位(人民币:元)</b> </p>
<table width="300" border="1" cellspacing="2"
cellpadding="2" align="center"
bordercolor="#999999">
<tr>
<td height="46">
<table width="420" border="0"
cellspacing="0" cellpadding="0"
align="center" height="27">
<tr align="center" bgcolor="#9999CC">
<td width="141" height="22">商品名</td>
<td width="65" height="22">单价</td>
<td width="76" height="22">订购数量</td>
<td width="95" height="22">合计</td>
</tr>
<%
Iterator i = cart.iterator();
while( i.hasNext() ){
OrderLine line = (OrderLine) i.next();
%>
<form action="deleteFormCart.do" method="post">
<input type="hidden" name="productId"
value='<%=line.getProduct().getProductId()%>'>
<tr align='center'>
<td><%=line.getProduct().getName()%></td>
<td><%=line.getProduct().getBasePrice()%></td>
<td><%=line.getAmount()%></td>
<td><%=line.getCost()%></td>
<td><input type="submit" name="DELETE" value="Delete"></td>
</tr>
</form>
<%
}
%>
<tr>
<td colspan=4 align="center" height="22" bgcolor="#CCCCCC">
您需要付的总钱数为:
</td>
<td width="95" align="center"
height="22" bgcolor="#CCCCCC">
<%=sum%>
</td>
</tr>
</table></td>
</tr>
</table>
<br>
<table width="400" border="0" cellspacing="0"
cellpadding="0" align="center" height="23">
<tr align="center">
<td width="148">
<a href="./index.jsp">继续购买</a></td>
<td width="135">
<a href="order.do">结账</a></td>
</tr>
</table>
<br> </td>
</tr>
</table>
</body>
</html>
=========================================================
15, 为deleteFormCart.do 创建Action form
file --> new --> web --> AcitonForm
package : ec_port_web
ActionFomr: DeleteFormCartForm
--> add from jsp --> displayCart.jsp --> finish
=========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class DeleteFormCartForm extends ActionForm {
private String productId;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public ActionErrors validate(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
}
}
=========================================================
16, 为 deleteFormCart.do 创建Action
package : ec_port_web
Action : DeleteFormCartAction
--> next
action path : /deleteFormCart
acitonForm name : deleteFormCartForm
scope : session
validate : true
input jsp : displayCart.jsp
--> finish
=========================================================
17, 修改perform() 方法
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import dto.*;
import ejbs.ShoppingCartLocal;
public class DeleteFormCartAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
DeleteFormCartForm form =
(DeleteFormCartForm) actionForm;
ShoppingCartLocal cart = ( ShoppingCartLocal)
request.getSession().
getAttribute(ShoppingCartLocal.CART_ATTR);
cart.removeLine(form.getProductId());
request.setAttribute( "CART" , cart.getAll());
request.setAttribute( "SUM" , cart.getCost()+"");
return actionMapping.findForward( "success" );
}
}
=========================================================
18,为DeleteFormCartAction 配置 forward
name : success
path : /display.jsp
=========================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -