📄 day06.txt
字号:
*** 为产品管理添加修改,删除和按照类别查询的功能 ***
1,为ProductMgmSessionFacadeEJB 添加4个方法:
a) method name : getProductByCategory
return type : java.util.Collection
input parameter : String categoryId
interface : Local
b) method name : deleteProduct
return type : void
input parameter : String productId
interface : Local
c) method name : updateProduct
return type : void
input parameter : dto.ProductDTO product
interface : Local
d) method name : getProductById
return type : dto.ProductDTO
input parameter : String productId
interface : Local
==========================================================
2, 为上述方法添加函数体
右键点击ProductMgmSessionFacadeEJB ---> view bean source
public Collection getProductByCategory(String id)
throws FinderException {
CategoryLocal cLocal =
categoryHome.findByPrimaryKey(new Integer(id));
Collection col = cLocal.getProducts();
HashSet set = new HashSet();
Iterator i = col.iterator();
CategoryDTO category = new CategoryDTO(
(Integer)cLocal.getPrimaryKey(),
cLocal.getName(),
cLocal.getDescription());
while( i.hasNext() ){
ProductLocal pLocal = (ProductLocal)i.next();
set.add( new ProductDTO(
(String)pLocal.getPrimaryKey(),
pLocal.getName(),
pLocal.getBaseprice(),
pLocal.getDescription(),
category));
}
return set;
}
==========================================================
public void deleteProduct(String id)
throws FinderException, RemoveException {
ProductLocal pLocal =
productHome.findByPrimaryKey( id );
pLocal.remove();
}
==========================================================
public void updateProduct(dto.ProductDTO product)
throws FinderException {
CategoryLocal cLocal = categoryHome.findByPrimaryKey(
product.getCategory().getId());
ProductLocal pLocal =
productHome.findByPrimaryKey(
product.getProductId());
pLocal.setName( product.getName());
pLocal.setBaseprice( product.getBasePrice() );
pLocal.setDescription( product.getDesc() );
pLocal.setCategory(cLocal);
}
==========================================================
public dto.ProductDTO getProductById(String id)
throws FinderException{
ProductLocal pLocal =
productHome.findByPrimaryKey( id );
CategoryLocal cLocal = pLocal.getCategory();
CategoryDTO category = new CategoryDTO(
cLocal.getCategoryid(),
cLocal.getName(),
cLocal.getDescription());
ProductDTO product = new ProductDTO(
pLocal.getProductid(),
pLocal.getName(),
pLocal.getBaseprice(),
pLocal.getDescription(),
category);
return product;
}
==========================================================
3,修改ProductMgmSessionFacadeLocal 中相应的4个函数声明, 使之与ProductMgmSessionFacadeBean
中的声明一致:
package ejbs;
import javax.ejb.*;
import java.util.*;
import dto.*;
public interface ProductMgmSessionFacadeLocal extends javax.ejb.EJBLocalObject {
...............
public Collection getProductByCategory(String id)
throws FinderException;
public void deleteProduct(String id)
throws FinderException, RemoveException;
public void updateProduct(ProductDTO product)
throws FinderException;
public dto.ProductDTO getProductById(String id)
throws FinderException;
..............
}
==========================================================
4, 编译ejb-module
==========================================================
5, 为ProductMgmDelegate 添加四个相应的方法:
public Collection getProductsByCategory( String id )
throws FinderException{
return sessionLocal.getProductByCategory( id );
}
==========================================================
public void deleteProduct( String id )
throws FinderException,RemoveException {
sessionLocal.deleteProduct( id );
}
==========================================================
public void updateProduct( ProductDTO product )
throws FinderException{
sessionLocal.updateProduct( product );
}
==========================================================
public ProductDTO getProductById( String id )
throws FinderException {
return sessionLocal.getProductById( id );
}
==========================================================
7 ,为searchProduct.do创建ActionForm
file --> new --> web --> Action From
package : ec_port_web
ActionForm : SearchProductForm
---> next --> add
name: category
type: java.lang.String
---> finish
==========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class SearchProductForm extends ActionForm {
private String category;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public ActionErrors validate(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
}
}
=========================================================
8,为searchProduct.do 创建Action
package : ec_port_web
Action : SearchProductAction
---> next
aciton path : searchProduct.do
formBean name: SearchProductAction
validate : false
scope : session
input jsp : productMgm.jsp
==========================================================
9, 修改perform() 方法 ( 如下所示 )
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import delegates.ProductMgmDelegate;
import exceptions.ServiceLocatorException;
import java.util.Collection;
import javax.ejb.FinderException;
import javax.ejb.CreateException;
public class SearchProductAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
SearchProductForm form =
(SearchProductForm) actionForm;
try{
ProductMgmDelegate pd = new ProductMgmDelegate();
Collection categoryCol = pd.getAllCategory();
request.getSession().
setAttribute( "ALL_CATEGORY" ,categoryCol);
String categoryId = form.getCategory() ;
Collection products = null;
if( categoryId.equals( "all" ) ){
products = pd.getAllProduct();
request.getSession().
setAttribute( "ALL_PRODUCT" , products );
return actionMapping.findForward( "success" );
}
products = pd.getProductsByCategory( categoryId ) ;
request.getSession().
setAttribute( "ALL_PRODUCT" , products);
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" );
}
}
}
==========================================================
10, 为SearchProductAction 配置forward
a) when query success
path : /productMgm.jsp
name : success
b) when some exceptions be caught
path : /fail.jsp
name : systemError
==========================================================
11, 为delModProduct.do 创建ActionForm
file --> new --> web --> ActionForm
package : ec_port_web
Action Form : DelModProductForm
---> next --> add
name : prductId
type : java.lang.String ---> finish
==========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class DelModProductForm extends ActionForm {
private String prductId;
public String getPrductId() {
return prductId;
}
public void setPrductId(String prductId) {
this.prductId = prductId;
}
public ActionErrors validate(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(
ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
}
==========================================================
12, 为delModProduct.do 创建Action
file --> new --> web --> Action
package : ec_port_web
Action : DelModProductAction
---> next
action path : /delModProduct
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -