📄 day08.txt
字号:
========== 为index.jsp 添加购物功能 =============
1, 在dto中创建 OrderLine 类
package dto;
import dto.ProductDTO;
import java.io.Serializable;
public class OrderLine implements Serializable {
private ProductDTO product= null;
private int amount = 0 ;
public OrderLine() {
}
public OrderLine( ProductDTO product,int amount ){
this.product = product;
this.amount = amount;
}
public ProductDTO getProduct(){
return this.product;
}
public void setProduct( ProductDTO product ){
this.product = product;
}
public int getAmount(){
return this.amount;
}
public void setAmmount( int i ){
this.amount = i;
}
public double getCost(){
return product.getBasePrice() * amount;
}
}
=========================================================
2,创建 ShoppingCartEJB:
a)双击ejb module 进入 ejb designer
b)在ejb designer中点击右键 -->create ejb-->Session Bean
c)修改session bean 属性:
bean name :ShoppingCartEJB
interface : local
session type : stateful
session sychronization: true
transaction type :Container
--> class and package
修改:
package : ejbs
bean name : ShoppingCartEJB
bean class : ejbs.ShoppingCartBean
lcoal home interface: ejbs.ShoppingCartLocalHome
local interface : ejbs.ShoppingCartLocal
d) 添加属性cart 用于保存OrderLine
右键点击ShoppingCartEJB --> add --> field
field name : cart
type : java.util.Collection
getter : none
setter : none
e) 右键点击ShoppingCartEJB --> add --> Method
依次添加五个方法:
1)
method name : addLine
return type : void
input parameter: dto.OrderLine line
interface : local
2)
method name : getAll
return type : java.util.Collection
input parameter:
interface : local
3)
method name : removeLine
return type : void
input parameter: String productId
interface : local
4)
method name : getCost
return type : double
input parameter:
interface : local
5)
method name : getItemNumber
return type : int
input parameter:
interface : local
f) 右键点击ShoppingCartEJB --> view bean source
1) 在ejbCreate方法中初始化cart属性
public void ejbCreate()
throws CreateException {
cart = new ArrayList();
}
2) 为上述五个方法提供函数体:
public void addLine(dto.OrderLine line) {
cart.add( line );
}
public java.util.Collection getAll() {
return cart;
}
public void removeLine(String productId) {
Iterator i = cart.iterator();
while( i.hasNext() ){
OrderLine line = (OrderLine)i.next();
if(line.getProduct().getProductId()
.equals( productId) ){
cart.remove(line);
break;
}
}
}
public double getCost() {
double cost = 0;
Iterator i = cart.iterator();
while( i.hasNext() ){
OrderLine line = ( OrderLine ) i.next();
cost += line.getCost();
}
return cost;
}
public int getItemNumber() {
return cart.size();
}
g) 编译 OrderLine , ShoppingCartEJB
=========================================================
3, 为ShppingCartEJB 配置local ejb reference
a) 双击web.xml ,进入 webapp dd editor
b) 点击左下角的local ejb reference
c) 选择 add
reference name : ejb/cartEJB
type : session
link : ShoppingCartEJB
local home interface: ejbs.ShoppingCartLocalHome
local interface : ejbs.ShoppingCartLocal
d) 保存
==========================================================
4,创建SessionListener 为每个会话初始化shoppingcart
package listeners;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
import util.User;
import util.ServiceLocator;
import exceptions.ServiceLocatorException;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import ejbs.*;
import delegates.ProductMgmDelegate;
import java.util.Collection;
public class MySessionListener
implements HttpSessionListener {
public MySessionListener() {
}
public void sessionCreated(HttpSessionEvent hse) {
try{
ServiceLocator locator = ServiceLocator.getInstance();
ShoppingCartLocalHome home =(ShoppingCartLocalHome )
locator.getEJBLocalHome( "java:comp/env/ejb/cartEJB" );
ShoppingCartLocal local = home.create();
hse.getSession().
setAttribute( ShoppingCartLocal.CART_ATTR, local );
}catch( ServiceLocatorException se ){
throw new EJBException(se.getMessage() );
}catch( CreateException ce ){
throw new EJBException( ce.getMessage() );
}
public void sessionDestroyed(HttpSessionEvent parm1) {
}
}
==========================================================
5, 为index.jsp 中 buyProduct.do 创建ActionForm
file --> new --> web --> ActionForm
package : ec_port_web
ActionForm : BuyProductForm
--> add from jsp --> index.jsp
---> 删除多余的字段,只保留 productId 和 amount --> finish
=========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class BuyProductForm extends ActionForm {
private String amount;
private String productId;
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public ActionErrors validate(ActionMapping ationMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
}
}
=========================================================
6, 修改reset 方法 和 validate 方法
=========================================================
7,为buyProduct.do 创建Action
file --> new --> web --> Action
package : ec_port_web
Action : BuyProductAction
action path : /buyProduct
formbean name : buyProductForm
scope : session
validate : true
input jsp : index.jsp
---> finish
=========================================================
9, 修改 perform() 方法:
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import delegates.ProductMgmDelegate;
import exceptions.*;
import dto.*;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -