数据操作通用框架的使用
作者:silvermew 日期:2007-12-26 09:19:02
举例如下,以网上商店中的“商品”为数据对象,看看使用本框架系统是如何被方便地使用?
3.1 数据建模:
首先是数据建模,这是最重要的起点,确定商品的数据结构,以建立商品数据模型,如下:
其次,建立商品数据表,如下:
3.2 EJB开发配置
第一:创建Session Bean ProductManager,接口如下:
第二:EJB配置,如果使用JBuilder图形化开发,这个过程是自动的)
3.3 Web模板式开发
首选是开发商品数据模型的“处理器”,专门实现EJB和前台界面之间的操作传递,这些传递功能的代码可以拷贝,不同数据模型,这个处理器功能代码基本相同,主要完成下面两个功能:
1. 根据主键从EJB中获得存在的数据模型。
2. 接受用户界面的操作,调用EJB的的新增、修改、删除等功能。
下面以Product模型为实例:
//接受用户界面的数据操作
程序开发完成后,需要在web.xml中配置对EJB的JNDI调用,如下:
以Item模型为实例:
对比ItemHandler和ProductHandler,两者代码几乎一致,这种模板式开发减少了琐碎工作的差错,节省开发时间。无需遵循Struts标准实现很多Action类。
3.4 Struts-config.xml和modelmapping.xml配置
struts-config.xml配置为struts的标准配置文件,每个数据模型需要三行配置:
1. ActionForm的配置
2. 界面输出配置
3. 接受界面输入
这三行配置对于不同数据模型都是相同的,代码可以拷贝,不同的就是相应的ActionForm值。
<!-- 本action实现界面输出,不同数据模型,可以拷贝这段代码,只要修改
productForm为新的数据模型的ModelForm,以及Jsp文件名修改 -->
<!-- 本action接受界面输入,不同数据模型,可以拷贝这段代码,只要修改
productForm为新的数据模型的ModelForm以及Jsp文件名修改 -->
struts-config.xml的配置也是模板化,拷贝粘贴就可以,不同的是ActionForm和Jsp名称不一样。
为了能使用Web层的增删改查框架,需要实现modelmapping.xml配置,如下:
其中的formName是struts-config.xml的ActionForm名称。
key是数据模型的主键。
Model是数据模型类,本类在前面已经列出。
Handler是数据模型处理器,本类已经在前面列出。
3.5 Jsp的编写
Jsp是纯Html语法编写,适合非专业人员,如美工设计等。
商品数据的增删改查界面是在一个JSP里实现,如product.jsp:
优点
1. 实际开发过程中,数据模型字段不断变化,使用本框架,只要轻松更改三次就可实现全部修改:数据库字段修改;Product数据模型字段修改;Jsp显示字段修改。这三种修改工作非常容易和轻松。
2. 最大化省略了数据增删改查等琐碎开发工作,如果一个系统数据模型达到数百之多,使用本框架效率和质量就非常明显。
3. 由于降低了琐碎工作,因此可以将更多精力集中在Jsp页面显示,客户要求的显示或打印格式可是五花八门,有本框架在后面做支撑,那些又苦又累的活变成了轻松、富有创造力的配置工作了。
4. 可以快速开发出系统原型,由于数据增删改查是软件系统最基础的功能,使用本框架快速开发完成后,基本就完成了系统的原型,在短时间内能够给客户看到真正的互动的原型界面(不是Html做的假界面噢!),从而能真正摸清客户所需要,再也不怕客户反反复复地修改了。
5. 客户需求变化有多快,我就有多快!
3.1 数据建模:
首先是数据建模,这是最重要的起点,确定商品的数据结构,以建立商品数据模型,如下:
1 | public class Product implements Model {
private String productId; //商品ID
private String catId; //商品目录ID
private String name; //商品名称
private String description; //商品描述
private Collection items = new ArrayList(); //商品细目
public Product(){ }
//构造方法
public Product(String productId, String name, String description){
this.productId = productId;
this.name = name;
this.description = description;
}
//set或get方法
public String getProductId() { return productId; }
public void setProductId(String productId) {
this.productId = productId;
}
……
}
|
其次,建立商品数据表,如下:
1 | CREATE TABLE product( productId char(10) NOT NULL, name varchar(100) DEFAULT '' , #产品名称 imagePath varchar(200) DEFAULT '' , #图片 description text DEFAULT '' , #详细描述 可html PRIMARY KEY (productId) )TYPE=InnoDB; |
3.2 EJB开发配置
第一:创建Session Bean ProductManager,接口如下:
1 | public interface ProductManagerLocal extends javax.ejb.EJBLocalObject {
//商品数据增加
public void createProduct(EventModel em) throws Exception;
//商品数据修改
public void updateProduct(EventModel em) throws Exception;
//商品数据删除
public void deleteProduct(EventModel em) throws Exception;
//商品查询
public Product getProductById(String Id);
。。。。
}
|
第二:EJB配置,如果使用JBuilder图形化开发,这个过程是自动的)
3.3 Web模板式开发
首选是开发商品数据模型的“处理器”,专门实现EJB和前台界面之间的操作传递,这些传递功能的代码可以拷贝,不同数据模型,这个处理器功能代码基本相同,主要完成下面两个功能:
1. 根据主键从EJB中获得存在的数据模型。
2. 接受用户界面的操作,调用EJB的的新增、修改、删除等功能。
下面以Product模型为实例:
1 | public class ProductHandler implements ModelHandler {
private final static String module = ProductHandler.class.getName();
private final static ServiceServerFactory sf = ServiceServerFactory.getInstance();
protected final static CacheFactory cacheFactory = CacheFactory.getInstance();
//初始化ModelForm实例
public ModelForm initForm(HttpServletRequest request) throws Exception{
ProductForm form = new ProductForm();
return form;
}
//通过EJB从数据库中根据主键获得商品数据
public Model findModelByKey(String keyValue, HttpServletRequest request) throws
Exception{
ProductManagerLocal productManager = (ProductManagerLocal) sf.getService(
FrameworkServices.ProductEJB, request);
return productManager.getProductById(keyValue);
}
|
//接受用户界面的数据操作
1 | public void serviceAction(EventModel em, HttpServletRequest request) throws
java.lang.Exception {
ProductManagerLocal productManager = (ProductManagerLocal) sf.getService(
FrameworkServices.ProductEJB, request);
try {
switch (em.getActionType()) {
case Event.CREATE:
productManager.createProduct(em);
break;
case Event.EDIT:
productManager.updateProduct(em);
break;
case Event.DELETE:
productManager.deleteProduct(em);
break;
}
} catch (Exception ex) {
throw new Exception("System operation Error:" + ex);
}
}
|
程序开发完成后,需要在web.xml中配置对EJB的JNDI调用,如下:
1 | <ejb-local-ref> <ejb-ref-name>ejb/ProductManager</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local-home>com.jdon.estore.catalog.ProductManagerLocalHome</local-home> <local>com.jdon.estore.catalog.ProductManagerLocal</local> <ejb-link>ProductManager</ejb-link> </ejb-local-ref> |
以Item模型为实例:
1 | public class ItemHandler extends ModelHandler {
private final static String module = ItemHandler.class.getName();
private final static ServiceServerFactory sf = ServiceServerFactory.
getInstance();
public ModelForm initForm(HttpServletRequest request) throws Exception {
ItemForm form = new ItemForm();
String productId = request.getParameter("productId");
if ( (productId == null) || (productId.length() == 0))
throw new Exception("productId is null");
form.setProductId(productId);
return form;
}
public Model findModelByKey(String keyValue, HttpServletRequest request) throws
Exception {
ProductManagerLocal productManager = (ProductManagerLocal) sf.getService(
FrameworkServices.ProductEJB, request);
return productManager.getItemById(keyValue);
}
public void serviceAction(EventModel em, HttpServletRequest request) throws
java.lang.Exception {
ProductManagerLocal productManager = (ProductManagerLocal) sf.getService(
FrameworkServices.ProductEJB, request);
try {
switch (em.getActionType()) {
case Event.CREATE:
productManager.createItem(em);
break;
case Event.EDIT:
productManager.updateItem(em);
break;
case Event.DELETE:
productManager.deleteItem(em);
break;
}
} catch (Exception ex) {
throw new Exception("System operation Error:" + ex);
}
}
}
|
对比ItemHandler和ProductHandler,两者代码几乎一致,这种模板式开发减少了琐碎工作的差错,节省开发时间。无需遵循Struts标准实现很多Action类。
3.4 Struts-config.xml和modelmapping.xml配置
struts-config.xml配置为struts的标准配置文件,每个数据模型需要三行配置:
1. ActionForm的配置
2. 界面输出配置
3. 接受界面输入
这三行配置对于不同数据模型都是相同的,代码可以拷贝,不同的就是相应的ActionForm值。
1 | <struts-config> <form-beans> <form-bean name="productForm" type="com.jdon.estore.web.catalog.ProductForm" /> <form-bean name="productForm" type="com.jdon.estore.web.catalog.Product2Form" /> </form-beans> <action-mappings> |
<!-- 本action实现界面输出,不同数据模型,可以拷贝这段代码,只要修改
productForm为新的数据模型的ModelForm,以及Jsp文件名修改 -->
1 | <action attribute="productForm" type="com.jdon.strutsutil.ModelViewAction" validate="false" scope="request" path="/admin/productAction"> <forward name="create" path="/admin/product.jsp" /> <forward name="edit" path="/admin/product.jsp" /> </action> |
<!-- 本action接受界面输入,不同数据模型,可以拷贝这段代码,只要修改
productForm为新的数据模型的ModelForm以及Jsp文件名修改 -->
1 | <action name="productForm" type="com.jdon.strutsutil.ModelSaveAction" input="/admin/product.jsp" scope="request" path="/admin/saveProductAction"> <forward name="success" path="/admin/productOk.jsp" /> <forward name="failure" path="/admin/productOk.jsp" /> </action> </action-mappings> </struts-config> |
struts-config.xml的配置也是模板化,拷贝粘贴就可以,不同的是ActionForm和Jsp名称不一样。
为了能使用Web层的增删改查框架,需要实现modelmapping.xml配置,如下:
1 | <modelmappings> <modelmapping formName = "productForm" key="productId" model="com.jdon.estore.model.Product" handler = "com.jdon.estore.web.catalog.ProductHandler" /> </modelmappings> |
其中的formName是struts-config.xml的ActionForm名称。
key是数据模型的主键。
Model是数据模型类,本类在前面已经列出。
Handler是数据模型处理器,本类已经在前面列出。
3.5 Jsp的编写
Jsp是纯Html语法编写,适合非专业人员,如美工设计等。
商品数据的增删改查界面是在一个JSP里实现,如product.jsp:
1 | <html:form action="/admin/saveProductAction.do" method="POST" > <html:hidden property="action" /> <!- - 本行是关键,表明是新增还是删除或修改 -- > <html:hidden property="productId"/> 商品名称:<html:text property="name" size="20" /> 商品说明:<html:textarea rows="4" cols="32" property="description"/> <html:submit property="submit" value="保存"/> <html:reset value ="复位"/> </html:form> |
优点
1. 实际开发过程中,数据模型字段不断变化,使用本框架,只要轻松更改三次就可实现全部修改:数据库字段修改;Product数据模型字段修改;Jsp显示字段修改。这三种修改工作非常容易和轻松。
2. 最大化省略了数据增删改查等琐碎开发工作,如果一个系统数据模型达到数百之多,使用本框架效率和质量就非常明显。
3. 由于降低了琐碎工作,因此可以将更多精力集中在Jsp页面显示,客户要求的显示或打印格式可是五花八门,有本框架在后面做支撑,那些又苦又累的活变成了轻松、富有创造力的配置工作了。
4. 可以快速开发出系统原型,由于数据增删改查是软件系统最基础的功能,使用本框架快速开发完成后,基本就完成了系统的原型,在短时间内能够给客户看到真正的互动的原型界面(不是Html做的假界面噢!),从而能真正摸清客户所需要,再也不怕客户反反复复地修改了。
5. 客户需求变化有多快,我就有多快!
平均得分
(1 次评分)
评论: 7 | 查看次数: 802
- 共有 7 条评论
- 共有 7 条评论
发表评论
订阅
上一篇
|

文章来自:
标签: 
balance . mp3 mp4 player plays a part in the outcome . mp3 mp4 player of combat, gear is a major differentiator that makes up for shortcomings in other areas. mp3 mp4 player In fact, with . mp3 player kaufen the introduction of Resilience, gear more than mp4 ever plays a more substantial part in PvP. power level In an Arena match, the very first thing we . power level scope out is gear. wow level Through quick tab-selection viewing .wow leveling of character portraits, we generally wow lvl have a good idea of the classes we're up against if they keep their helm graphic on. wow lvl If they are wearing Season 3 shoulders, then we know . wow lvl 60 their relative experience. wow lvl 70 This is why the visual i.wow power leveling mpact of Arena shoulders is so important.wow powerlevel It immediately gives you a general idea of how tough the match will be. wow powerlevel Players in full S3 will likely have over 10k hp and over. wow powerleveling 400 Resilience, depending on the class and spec. A full S3 SL/SL Warlock, for example, will easily have. about 12-13k hp and over 400 Resilience. Identifying weapons is slightly more . difficult but will also give a general idea of an enemy's strength. Season 1 and 2 weapons share the same graphics, so it's harder to identify. Season 3 weapons, on the other hand, are distinctive and share models with Black Temple and Mount Hyjal weapons. which 最新免费网络游戏 have relatively the same power. A review of Brutal Gladiator weapons will come in handy because these will be the most common way to identify opponents of relative skill. With the new mechanics. in place for Arenas, Season 4 will more or less weed out the chaff from the grain.
wow gold
wow gold
wow gold
wow gold
wow gold
item4sale
item4sale
item4sale
item4sale
item4sale
buy age of conan gold
buy age of conan gold
buy age of conan gold
buy age of conan gold
buy age of conan gold
连锁洗衣店加盟
连锁洗衣店加盟
加盟干洗店连锁
加盟干洗店连锁
干洗机价格
干洗机价格
洗涤机械
洗涤机械
洗鞋
洗鞋
擦鞋连锁
擦鞋连锁
擦鞋店加盟
擦鞋店加盟
鞋机
鞋机
擦鞋修鞋
擦鞋修鞋
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold