📄 day05_2.txt
字号:
======================产品管理=====================
1,创建ProductDTO
package : dto
class name : ProudctDTO
package dto;
import java.io.Serializable;
public class ProductDTO implements Serializable{
private String productId;
private String name;
private double basePrice;
private String desc;
private CategoryDTO category = null;
public ProductDTO(String id,String name,
double basePrice,String desc,
CategoryDTO category) {
productId = id;
this.name = name;
this.basePrice = basePrice;
this.desc = desc;
this.category = category;
}
public String getProductId(){
return this.productId;
}
public void setProductId( String name ){
this.name = name;
}
public String getName(){
return this.name;
}
public void setName( String name ){
this.name = name;
}
public double getBasePrice(){
return this.basePrice;
}
public void setBasePrice( double basePrice ){
this.basePrice = basePrice;
}
public String getDesc(){
return this.desc;
}
public void setDesc( String desc ){
this.desc = desc;
}
public CategoryDTO getCategory(){
return this.category;
}
public void setCategory( CategoryDTO category ){
this.category = category;
}
}
==========================================================
2, 为ProductEJB添加find方法:
右键点击ProductEJB ---> add ---> finder
Finder Name : findAll
return type : java.util.Collection
input parameter :
home interface : Local Home
query : select object( p ) from Product p
==========================================================
3, 为ProductMgmSessionFacadeEJB添加商业方法:
method name : getAllProduct
return type : java.util.Collection
input parameter :
interface : Local
=========================================================
4, 编辑getAllProduct方法
右键点击ProductMgmSessionFacadeEJB ---> view bean source
public java.util.Collection getAllProduct()
throws FinderException {
Collection col = productHome.findAll();
HashSet set = new HashSet();
Iterator i = col.iterator();
while( i.hasNext() ){
ProductLocal local = (ProductLocal)i.next();
CategoryLocal cLocal = local.getCategory();
CategoryDTO category = new CategoryDTO(
cLocal.getCategoryid(),
cLocal.getName(),
cLocal.getDescription());
set.add( new ProductDTO(
local.getProductid(),
local.getName(),
local.getBaseprice(),
local.getDescription(),
category
));
}
return set;
}
==========================================================
5, 修改 ProductMgmSessionFacadeLocal , 使其 getAllProduct
方法的声明与 ProductMgmSessionFacadeBean中的一致
( 如下所示 )
public java.util.Collection getAllProduct()
throws FinderException;
==========================================================
6, 为 ProductMgmDelegate 类添加方法 getAllProduct()
public Collection getAllProduct( )
throws FinderException{
return sessionLocal.getAllProduct();
}
==========================================================
7,为 ProductMgmSessionFacadeEJB 添加 addProduct 方法
右键点击 ProductMgmSessionFacadeEJB ---> add ---> method
method name : addProduct
return type : void
input parameter : dto.ProductDTO product
interface : local
==========================================================
8, 为 ProductMgmSessionFacadeEJB 的 addProduct方法提供实现
右键点击 ProductMgmSessionFacadeEJB --> view bean source
实现addProduct 方法:( 如下所示 )
public void addProduct(dto.ProductDTO product)
throws FinderException, CreateException {
SequenceLocal sLocal =
seqHome.findByPrimaryKey( new Integer(1) );
int seq = sLocal.getSequence();
CategoryLocal cLocal = categoryHome.findByPrimaryKey(
product.getCategory().getId() );
ProductLocal pLocal = productHome.create(
""+seq,
product.getName(),
product.getDesc(),
product.getBasePrice());
pLocal.setCategory( cLocal );
}
==========================================================
9,修改 ProductMgmSessionFacadeLocal 中的 addProduct 方法声
明,使之同 ProductMgmSessionFacadeBean 中的声明一致:
( 如下所示 )
public void addProduct(ProductDTO product)
throws FinderException, CreateException;
==========================================================
10, 为ProductMgmDelegate 添加商业方法 addProduct() ( 如下所示 )
public void addProduct( ProductDTO product )
throws FinderException,CreateException{
sessionLocal.addProduct( product );
}
==========================================================
*****以下操作为产品管理提供web 应用 ( struts )****
==========================================================
11, 为 advinceMgm.jsp 中的连接 productMgm.do 创建Action
file ---> new ---> web ---> Action
package : ec_port_web
Action : ProductMgmAction
---> next
Action Path : /prductMgm
BeanForm name:
scope :
validate :
input jsp :
---> finish
==========================================================
12,修改perform方法,查询所有的产品和类别,将得到的集合设置到
request Attribute中 ,转向 productMgm.jsp页面:
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import exceptions.*;
import javax.ejb.FinderException;
import javax.ejb.CreateException;
import dto.CategoryDTO;
import dto.ProductDTO;
import delegates.ProductMgmDelegate;
import java.util.Collection;
public class ProductMgmAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) {
try{
ProductMgmDelegate pd = new ProductMgmDelegate();
Collection categoryCol = pd.getAllCategory();
Collection productCol = pd.getAllProduct();
request.getSession().
setAttribute( "ALL_CATEGORY" , categoryCol);
request.getSession().
setAttribute( "ALL_PRODUCT" , productCol);
return actionMapping.findForward( "success" );
}catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( FinderException fe ){
fe.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( CreateException ce ){
ce.printStackTrace();
return actionMapping.findForward( "systemError" );
}
}
}
==========================================================
13,为 ProductMgmAction 添加forward
a) when query products and category success
path : /productMgm.jsp
name :success
b) when some exceptions be caught
path : /fail.jsp
name : systemError
==========================================================
14, 创建productMgm.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import = "java.util.*"%>
<%@ page import = "dto.*"%>
<html>
<head>
<title>
productMgm
</title>
</head>
<%
Collection productCol =
(Collection)session.getAttribute( "ALL_PRODUCT" );
Collection categoryCol =
(Collection)session.getAttribute( "ALL_CATEGORY" );
%>
<body bgcolor="#ffffff">
<p align="center">
<table width="600" border="0" cellspacing="1"
cellpadding="4" bgcolor="3399ff">
<tr>
<td colspan="3" align="center">
<font color="#ffffff" class="title">
产品管理</font>
</td>
</tr>
<tr>
<td bgcolor="#ececec">类别:</td>
<form action="searchProduct.do" method="post">
<td bgcolor="#ececec" colspan="3">
<select name="category">
<option value="all" selected>ALL</option>
<%
Iterator i = categoryCol.iterator();
while( i.hasNext() ){
CategoryDTO category =
(CategoryDTO)i.next();
%>
<option value='<%=category.getId()%>'>
<%=category.getName()%>
</option>
<%
}
%>
</select>
</td>
<td bgcolor="#ececec">
<input type="submit" name="search" alue="search">
</td>
</tr>
</form>
<%
if( productCol.size() == 0 ){
%>
<tr>
<td bgcolor="#ececec">
<tr><td bgcolor="#ececec" colspan="5">
目前没有任何产品</td></tr>
</td></tr></table>
<%
}
else{
Iterator iter = productCol.iterator();
%>
<%
while( iter.hasNext() ){
ProductDTO p =
(ProductDTO)iter.next();
%>
<form action="delModProduct.do" method="post">
<tr>
<td bgcolor="#ececec" ><%=p.getName()%></td>
<td bgcolor="#ececec"> <%=p.getCategory().getName()%></td>
<td bgcolor="#ececec" ><%=p.getBasePrice()%></td>
<td bgcolor="#ececec" >
<input type="submit" name="modify"
value="modify">
</td>
<td bgcolor="#ececec" >
<input type="submit" name="delete" value="delete">
</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -