📄 day10.txt
字号:
1,在dto表中创建PaywayDTO
package dto;
import java.io.Serializable;
public class PaywayDTO implements Serializable{
private Integer paywayId;
private String payStyle;
public PaywayDTO() {
}
public PaywayDTO( Integer paywayId,String payStyle){
this.paywayId = paywayId;
this.payStyle = payStyle;
}
public PaywayDTO( int paywayId,String payStyle){
Integer temp = new Integer( paywayId );
this.paywayId = temp;
this.payStyle = payStyle;
}
public Integer getPaywayId(){
return this.paywayId;
}
public void setPaywayId( Integer id ){
this.paywayId = id;
}
public String getPayStyle(){
return this.payStyle;
}
public void setPayStyle( String style ){
this.payStyle = style;
}
}
=========================================================
2,在dto包中创建OrderDTO
package dto;
import ejbs.ShoppingCartLocal;
import java.util.Collection;
import java.io.Serializable;
public class OrderDTO implements Serializable{
private Collection items;
private String cardNo;
private String userid;
private PaywayDTO payway;
private double cost;
public OrderDTO() {
}
public OrderDTO( Collection items,PaywayDTO ayway,
String cardNo,String userid,double cost){
this.items = items;
this.payway = payway;
this.cardNo = cardNo;
this.userid = userid;
this.cost = cost;
}
public Collection getItems(){
return this.items;
}
public void setItems( Collection items ){
this.items = items;
}
public PaywayDTO getPayway(){
return this.payway;
}
public void setPayway( PaywayDTO payway ){
this.payway = payway;
}
public String getCardNo(){
return this.cardNo;
}
public void setCardNo( String cardNo ){
this.cardNo = cardNo;
}
public String getUserid(){
return this.userid;
}
public void setUserid( String userid ){
this.userid = userid;
}
public double getCost(){
return this.cost;
}
public void setCost( double cost ){
this.cost = cost;
}
}
=========================================================
3,为PaywayEJB添加商业方法,
双击ejb-module --> 右键点击 PaywayEJB --> add --> finder
finder name : findAll
return type : java.util.Collection
home interface : local home
query : select object( p ) from Payway p
=========================================================
4, 在 ShoppingSessionFacadeEJB 中添加方法
右键点击ShoppingSessionFacade --> add --> method
mehtod name : getAllPayway
return type : java.util.Collection
input parameter :
interface : local
=========================================================
5, 右键点击ShoppingSessionFacade --> view bean source
为 getAllPayway 添加函数体
public java.util.Collection getAllPayway()
throws OrderException{
HashSet set = new HashSet();
try{
Collection col = paywayLocalHome.findAll();
Iterator i = col.iterator();
PaywayDTO payway = null;
while( i.hasNext() ){
PaywayLocal pLocal = (PaywayLocal) i.next();
payway = new PaywayDTO(
pLocal.getPaywayid(),
pLocal.getPaystyle());
set.add( payway );
}
return set;
}catch( FinderException fe ){
fe.printStackTrace();
throw new OrderException( fe.getMessage() );
}
}
=========================================================
5,修改ShoppingSessionFacadeLocal , 使其中的函数声明与bean class 中的一致:
package ejbs;
import javax.ejb.*;
import java.util.*;
import exceptions.OrderException;
public interface ShoppingSessionFacadeLocal
extends javax.ejb.EJBLocalObject {
public void addOrder(double cost, String no,
String uid, String payway,
Collection lines)
throws OrderException;
public java.util.Collection getAllPayway()
throws OrderException;
}
=========================================================
6,在web.xml中配置对ShoppingSessionFacade的引用
==========================================================
7,在delegates包中创建ShoppingDelegate类:
package delegates;
import ejbs.ShoppingSessionFacadeLocalHome;
import ejbs.ShoppingSessionFacadeLocal;
import util.ServiceLocator;
import exceptions.ServiceLocatorException;
import exceptions.OrderException;
import javax.ejb.CreateException;
import java.util.Collection;
public class ShoppingDelegate {
private ShoppingSessionFacadeLocal sessionLocal = null;
public ShoppingDelegate()
throws ServiceLocatorException{
try{
ServiceLocator locator = ServiceLocator.getInstance();
ShoppingSessionFacadeLocalHome home = (ShoppingSessionFacadeLocalHome)
locator.getEJBLocalHome(
"java:comp/env/ejb/shoppingSessionFacade");
sessionLocal = home.create();
}catch( CreateException ce ){
ce.printStackTrace();
throw new ServiceLocatorException(
ce.getMessage());
}
}
public Collection getAllPayway() throws OrderException{
return sessionLocal.getAllPayway();
}
}
==========================================================
8,修改index.jsp,在页面上添加
<a href="order.do">order</a>
========================================================
9,为order.do 创建Action
file --> new ---> web --> Action
package : ec_port_web
Action : OrderAction
--> next
Action Path : /order
---> finish
=========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import dto.*;
import exceptions.*;
import java.util.Collection;
import delegates.ShoppingDelegate;
import ejbs.ShoppingCartLocal;
import util.Errors;
import util.User;
public class OrderAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
try{
ShoppingDelegate sd = new ShoppingDelegate();
Collection col = sd.getAllPayway();
request.setAttribute( "ALL_PAYWAY",col );
return actionMapping.findForward("success");
}catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "error" );
}
catch( OrderException oe ){
oe.printStackTrace();
return actionMapping.findForward( "error" );
}
}
}
==========================================================
10, 为 OrderAction 配置 forwarde
a) when user have login yet and
shopping cart is not empty
path : /order.jsp
name : success
c) when some exceptions be caught
path : /fail.jsp
name : error
==========================================================
11,创建 order.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@page import="java.util.*,dto.*"%>
<%
Collection col =
(Collection)request.getAttribute( "ALL_PAYWAY" );
%>
<html>
<head>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -