📄 day05_1.txt
字号:
新建表
create table product(
productid varchar2(16) primary key,
name varchar2(32) not null,
description varchar2(64),
baseprice double not null,
categoryid varchar2(16)
references category(categoryid)
)
==========================================================
刷新数据源
右键点击数据源 --> refresh from database
==========================================================
新建ProductEJB
右键点击数据源中的Product表 -->
create cmp 2.0 Entity Bean
bean name : ProductEJB
abstract schema name: Product
interface : Local
bean class : ProductBean
Local Home : ProductLocalHome
Local : ProductLocal
==========================================================
修改ProductEJB 中的成员变量
productid:
is primary key : true
type : String
in ejbCreate : true
getter : Local
setter : none
culomn name : PRODUCTID
name:
is primary key : false
is persisted : true
type : String
in ejbCreate : true
getter : Local
setter : Local
culomn name : NAME
description:
is primary key : false
is persisted : true
type : String
in ejbCreate : true
getter : Local
setter : Local
culomn name : DESCRIPTION
baseprice:
is primary key : false
is persisted : true
type : double
in ejbCreate : true
getter : Local
setter : Local
culomn name : BASEPRICE
==========================================================
为CategoryEJB 与 ProductEJB 添加关系
注意:在JBuilder中关系只能从主表对应的Entity Bean发起。
点击categoryid按右键选择Delete field,选择Yes确认。
选择CategoryEJB,按右键选择Add->Relationship,
将箭头指向ProductEJB,
点击CategoryEJB中的关系域,出现一对话框,修改属性:
Relationship Properties:
Relationship Name: <不要改动>
Multiplicity: one to many
Navigability: bidirectional
Cascade delete: true
db-cascade-delete: true
CMR field properties:
Field name: products
Return type: java.uil.Collection
Getters: local
Setters: local
点击Edit RDBMS Relation...出现一对话框,先点击按钮Clear,
再用鼠标选择Category(categoryid),按住鼠标左键连接到Product(categoryid)
->OK
选择ProductEJB中的关系域出现一对话框,修改:
CMR field properties:
Field name : category
Return type: Bean Instance
==========================================================
修改ProductMgmSessionFacadeBean 中的 deleteCategory 方法
public void deleteCategory(String id)
throws FinderException, RemoveException {
CategoryLocal local =
categoryHome.findByPrimaryKey(new Integer(id));
Collection col = local.getProducts();
Iterator i = col.iterator();
HashSet set= new HashSet();
while( i.hasNext() ){
ProductLocal pLocal = (ProductLocal)i.next();
set.add( pLocal );
}
Iterator iter = set.iterator();
while( iter.hasNext() ){
ProductLocal pLocal = (ProductLocal)iter.next();
pLocal.remove();
}
local.remove();
}
========================================================== *** 以下操作是为categoryMgm.jsp 实现修改和删除的功能 ***
==========================================================
创建ActionForm : DelModCategoryForm
file ---> new ---> web ----> action form
package : ec_port_web
Action Form : DelmodCategoryForm
--> next ---> add from jsp --> categoryMgm.jsp
--> finish
DelModCateForm 如下所示:
==========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class DelmodCategoryForm extends ActionForm {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ActionErrors validate(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
}
}
==========================================================
重写reset 和 validate 方法
==========================================================
7,创建 DelModCategory Action
file --> new ---> web --> action
package : ec_port_web
Action : DelModCategoryAction
--> next
Action Path : delmodCategory
formbean name : delmodCategoryForm
validate : true
scope : session
input jsp : categoryMgm.jsp
==========================================================
修改 perfrom() 方法如下 :
==========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import exceptions.*;
import delegates.*;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.CreateException;
import java.util.Collection;
import dto.CategoryDTO;
public class DelmodCategoryAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
DelmodCategoryForm form =
(DelmodCategoryForm)actionForm;
String type = request.getParameter( "Delete" );
try{
ProductMgmDelegate pd = new ProductMgmDelegate();
if (type != null) {
pd.deleteCategory( form.getId() );
Collection col = pd.getAllCategory();
request.getSession().
setAttribute( "ALL_CATEGORY" , col );
return actionMapping.findForward( "success" );
}
CategoryDTO category =
pd.getCategoryById( form.getId() );
request.setAttribute( "CATEGORY_INFO" , category);
return actionMapping.findForward( "viewCategory" );
}catch( FinderException fe ){
fe.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( RemoveException re ){
re.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( CreateException ce ){
ce.printStackTrace();
return actionMapping.findForward( "systemError" );
}
}
}
=========================================================
9 , 为DelmodCategoryAction添加forward:
a) when delete success
path: /categoryMgm.jsp
name: success
b) when exception be caught
path: /fail.jsp
name: systemError
c) when modify category
path: /viewCategory.jsp
name: viewCategory
=========================================================
10 , 创建viewCategory.jsp
<%@ page contentType="text/html; charset=GBK" import="dto.CategoryDTO"%>
<html>
<head>
<title>
viewCategory
</title>
<%
CategoryDTO category =(CategoryDTO)
request.getAttribute( "CATEGORY_INFO" );
%>
</head>
<body bgcolor="#ffffff">
<form action="modifyCategory.do" method="post">
<p align=center>
<table width="600" border="0" cellspacing="1"
cellpadding="4" bgcolor="3399ff">
<tr>
<td colspan="2" align="center">
<font color="#ffffff" class="title">
修改类别</font>
</td>
</tr>
<input type="hidden" name="id" value='<%=category.getId()%>'>
<tr>
<td bgcolor="#ececec" >类别名称:</td>
<td bgcolor="#ececec" >
<input type="text" name="name"
value='<%=category.getName()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" >详细信息:</td>
<td bgcolor="#ececec" >
<textarea name="desc">
<%=category.getDesc().trim()%>
</textarea>
</td>
</tr>
<tr>
<td bgcolor="#ececec" colspan="2"
align="center">
<input type="submit" value="修改">
</td>
</tr>
</table>
</form>
</body>
</html>
==========================================================
11, 为viewCategory.jsp 创建ActionForm
file --> new ---> web ---> actionForm
package : ec_port_web
Action Form : ModifyCategoryForm
---> next ---> add from jsp ---> viewCategory.jsp
---> finish
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class ModifyCategoryForm extends ActionForm {
private String desc;
private String id;
private String name;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ActionErrors validate(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(
ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
}
==========================================================
修改validate 和 reset 方法
==========================================================
13,为viewCategory.jsp 创建 Action
file --> new --> web --> Action
package : ec_port_web
Action : ModifyCategoryAction
---next
Action path : modifyCategory
formbean name: modifyCategoryForm
validate : true
scope : session
input jsp : viewCategory.jsp
==========================================================
14, 修改perform方法:
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import exceptions.*;
import javax.ejb.FinderException;
import javax.ejb.CreateException;
import delegates.*;
import dto.CategoryDTO;
import java.util.Collection;
public class ModifyCategoryAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
ModifyCategoryForm form =
(ModifyCategoryForm) actionForm;
try{
ProductMgmDelegate pd = new ProductMgmDelegate();
CategoryDTO category = new CategoryDTO(
new Integer( form.getId() ),
form.getName(),
form.getDesc() );
pd.updateCategory( category );
Collection col = pd.getAllCategory();
request.getSession().
setAttribute( "ALL_CATEGORY" , col );
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" );
}
}
}
==========================================================
15 为ModifyCategoryAction添加forward
a) when modify success
path : /categoryMgm.jsp
name : success
b) when some Exceptions be caught
path : /fail.jsp
name : systemError.jsp
==========================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -