文章用途
本文内容代码为简单的单表CRUD代码模板替换成功后的Demo代码!
注意
本文只能用于CRUD代码分析和手工研究整套CRUD功能的代码实现,
因为OFBiz原项目中已包含 Order 模块, 所以不能重复建模块
替换说明
按下列注释指定的关键字替换,
然后放到每块代码上对应对应的注释内提到的xml文件中,
重启项目即可使用!
<!-- app-name[模块名称]: order; entityTitle[表描述]: 订单; entityModelName[表名]: Order; pkId[表主键名]: orderId; type[类型]: orderTypeId; name[名称]: orderName; date[日期时间]: orderDate; Screen/Menu/Form[文件前缀]: Order; webapp-site[app站点]: order -->
<!-- component://order/entitydef/entitymodel.xml #表结构定义 --> <entity entity-name="Order" package-name="org.ofbiz.orderxpp"> <description>订单xpp</description> <field name="orderId" type="id-ne"><description>ID</description></field> <field name="orderTypeId" type="id-ne"><description>类型ID</description></field> <field name="orderName" type="name"><description>名称</description></field> <field name="orderDate" type="date-time"><description>日期时间</description></field> <field name="description" type="description"><description>描述</description></field> <field name="enable" type="indicator"><description>是否可用(Y/N)</description></field> <prim-key field="orderId"/> </entity>
<!-- component://order/config/OrderUiLabels.xml #国际化资源配置xpp --> <property key="FindOrder"> <value xml:lang="en">FindOrderxpp</value> <value xml:lang="zh">管理订单</value> </property> <property key="EditOrder"> <value xml:lang="en">EditOrder</value> <value xml:lang="zh">编辑订单</value> </property> <property key="ViewOrder"> <value xml:lang="en">ViewOrder</value> <value xml:lang="zh">查看订单</value> </property> <property key="orderId"> <value xml:lang="en">ID</value> <value xml:lang="zh">编号</value> </property> <property key="orderTypeId"> <value xml:lang="en">Type</value> <value xml:lang="zh">类型</value> </property> <property key="orderName"> <value xml:lang="en">Name</value> <value xml:lang="zh">名称</value> </property> <property key="orderDate"> <value xml:lang="en">DateTime</value> <value xml:lang="zh">日期时间</value> </property> <property key="description"> <value xml:lang="en">description</value> <value xml:lang="zh">描述</value> </property> <property key="enable"> <value xml:lang="en">enable</value> <value xml:lang="zh">是否可用</value> </property> <property key="From"> <value xml:lang="en">From</value> <value xml:lang="zh">从</value> </property> <property key="To"> <value xml:lang="en">To</value> <value xml:lang="zh">到</value> </property> <property key="enableYes"> <value xml:lang="en">Yes</value> <value xml:lang="zh">是</value> </property> <property key="enableNo"> <value xml:lang="en">No</value> <value xml:lang="zh">否</value> </property> <property key="confirmDeleteMsg"> <value xml:lang="en">Confirm Delete xpp ?</value> <value xml:lang="zh">确认删除 ?</value> </property>
<!-- component://order/widget/OrderMenus.xml #xpp菜单配置 --> <menu-item name="FindOrder" title="${uiLabelMap.FindOrder}"> <link target="FindOrder" /> </menu-item>
<!-- component://order/webapp/order/WEB-INF/controller.xml #xpp请求配置 --> <!-- Order xpp Request --> <request-map uri="FindOrder"> <security auth="true" https="true"/> <response name="success" type="view" value="FindOrder" /> </request-map> <request-map uri="EditOrder"> <security auth="true" https="true"/> <response name="success" type="view" value="EditOrder" /> </request-map> <request-map uri="ViewOrder"> <security auth="true" https="true"/> <response name="success" type="view" value="ViewOrder" /> </request-map> <request-map uri="createOrder"> <security auth="true" https="true"/> <event type="service" invoke="createOrder" /> <response name="success" type="request-redirect-noparam" value="FindOrder" /> <response name="error" type="view" value="EditOrder"/> </request-map> <request-map uri="updateOrder"> <security auth="true" https="true"/> <event type="service" invoke="updateOrder" /> <response name="success" type="request-redirect-noparam" value="FindOrder"/> <response name="error" type="view" value="EditOrder"/> </request-map> <request-map uri="deleteOrder"> <security auth="true" https="true"/> <event type="service" invoke="deleteOrder" /> <response name="success" type="request-redirect-noparam" value="FindOrder"/> </request-map>
<!-- Order View xpp --> <view-map name="FindOrder" type="screen" page="component://order/widget/OrderScreens.xml#FindOrder" /> <view-map name="ViewOrder" type="screen" page="component://order/widget/OrderScreens.xml#ViewOrder" /> <view-map name="EditOrder" type="screen" page="component://order/widget/OrderScreens.xml#EditOrder" />
<!-- component://order/widget/OrderScreens.xml #xpp显示页面配置 --> <screen name="FindOrder"> <section> <actions> <!-- html.head.title 显示描述xpp --> <set field="titleProperty" value="FindOrder"/> <!-- 菜单xpp高亮 --> <set field="headerItem" value="FindOrder"/> </actions> <widgets> <decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}"> <decorator-section name="body"> <include-form name="FindOrder" location="component://order/widget/OrderForms.xml" /> <link target="EditOrder" text="${uiLabelMap.CommonCreate}" style="buttontext"/> <include-form name="ListOrder" location="component://order/widget/OrderForms.xml" /> </decorator-section> </decorator-screen> </widgets> </section> </screen> <screen name="EditOrder"> <section> <actions> <!-- html.head.xpp 显示描述 --> <set field="titleProperty" value="FindOrder"/> <!-- 菜单高亮xpp --> <set field="headerItem" value="FindOrder"/> <!-- xpp业务数据 --> <set field="orderId" from-field="parameters.orderId"/> <entity-one entity-name="Order" value-field="entity" /> </actions> <widgets> <decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}"> <decorator-section name="body"> <include-form name="EditOrder" location="component://order/widget/OrderForms.xml" /> </decorator-section> </decorator-screen> </widgets> </section> </screen> <screen name="ViewOrder"> <section> <actions> <!-- html.head.title 显示描述xpp --> <set field="titleProperty" value="FindOrder"/> <!-- xpp菜单高亮 --> <set field="headerItem" value="FindOrder"/> <!-- 业务数据xpp --> <set field="orderId" from-field="parameters.orderId"/> <entity-one entity-name="Order" value-field="entity" /> </actions> <widgets> <decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}"> <decorator-section name="body"> <include-form name="ViewOrder" location="component://order/widget/OrderForms.xml" /> </decorator-section> </decorator-screen> </widgets> </section> </screen>
<!-- component://order/widget/OrderForms.xml #xpp显示页面内表单配置 --> <!-- Forms xpp Order --> <form name="FindOrder" type="single" target="FindOrder" default-map-name="search" default-entity-name="Order" header-row-style="header-row" default-table-style="basic-table"> <field name="orderId" title="${uiLabelMap.orderId}"><text-find hide-options="true" /></field> <field name="orderName" title="${uiLabelMap.orderName}" position="2"><text-find hide-options="true" /></field> <field name="enable" title="${uiLabelMap.enable}"> <drop-down allow-empty="true"> <option key="Y" description="${uiLabelMap.enableYes}"/> <option key="N" description="${uiLabelMap.enableNo}"/> </drop-down> </field> <field name="orderDate_fld0_value" title="${uiLabelMap.orderDate}(${uiLabelMap.From})" widget-style="dateISO" position="2" > <date-time type="date" /> </field> <field name="orderDate_fld0_op"><hidden value="greaterThanEqualTo"/></field> <field name="orderTypeId" title="${uiLabelMap.orderTypeId}"> <drop-down allow-empty="true"> <entity-options description="${description}" entity-name="Enumeration" key-field-name="enumId"> <entity-constraint name="enumTypeId" value="_NA_"/> <entity-order-by field-name="sequenceId"/> </entity-options> </drop-down> </field> <field name="orderDate_fld1_value" title="(${uiLabelMap.To})" widget-style="dateISO" position="2"> <date-time type="date" /> </field> <field name="orderDate_fld1_op"><hidden value="lessThanEqualTo"/></field> <field name="searchButton" title="${uiLabelMap.CommonFind}" widget-style="smallSubmit"><submit/></field> </form> <form name="ListOrder" list-name="listIt" type="list" paginate-target="FindOrder" separate-columns="true" odd-row-style="alternate-row" default-table-style="basic-table"> <actions> <set field="parameters.noConditionFind" value="Y"/> <set field="entityNameSearch" value="Order"/> <set field="orderBySearch" value="+orderId"/> <service service-name="performFind" result-map="result" result-map-list="listIt"> <field-map field-name="inputFields" from-field="parameters"/> <field-map field-name="entityName" from-field="entityNameSearch"/> <field-map field-name="orderBy" from-field="orderBySearch"/> <field-map field-name="viewIndex" from-field="viewIndex"/> <field-map field-name="viewSize" from-field="viewSize"/> </service> </actions> <field name="orderId" title="${uiLabelMap.orderId}" widget-style="buttontext" sort-field="true"> <hyperlink target="ViewOrder" description="${orderId}"> <parameter param-name="orderId"/> </hyperlink> </field> <field name="orderName" title="${uiLabelMap.orderName}" sort-field="true"><display/></field> <field name="orderDate" title="${uiLabelMap.orderDate}" sort-field="true"><display type="date-time"/></field> <field name="orderTypeId" title="${uiLabelMap.orderTypeId}" sort-field="true"> <display-entity entity-name="Enumeration" description="${description}[${orderTypeId}]" key-field-name="enumId"/> </field> <field use-when=""Y".equals(enable)" name="enable" title="${uiLabelMap.enable}" sort-field="true"> <display description="${uiLabelMap.enableYes}[Y]"/> </field> <field use-when="!"Y".equals(enable)" name="enable" title="${uiLabelMap.enable}" sort-field="true"> <display description="${uiLabelMap.enableNo}[N]"/> </field> <field name="EditOrder" title="${uiLabelMap.CommonEdit}" widget-style="buttontext"> <hyperlink target="EditOrder" description="${uiLabelMap.CommonEdit}"> <parameter param-name="orderId"/> </hyperlink> </field> <field name="deleteOrder" title="${uiLabelMap.CommonDelete}" widget-style="buttontext"> <hyperlink target="deleteOrder" confirmation-message="${uiLabelMap.confirmDeleteMsg}" target-type="intra-app" description="${uiLabelMap.CommonDelete}"> <parameter param-name="orderId"/> </hyperlink> </field> </form> <form name="EditOrder" type="single" target="updateOrder" default-map-name="entity" header-row-style="header-row" default-table-style="basic-table"> <alt-target use-when="entity==null" target="createOrder" /> <field name="orderId"> <hidden /> </field> <field name="orderTypeId" title="${uiLabelMap.orderTypeId}"> <drop-down allow-empty="true"> <entity-options description="${description}" entity-name="Enumeration" key-field-name="enumId"> <entity-constraint name="enumTypeId" value="_NA_"/> <entity-order-by field-name="sequenceId"/> </entity-options> </drop-down> </field> <field name="orderName" title="${uiLabelMap.orderName}"><text/></field> <field name="description" title="${uiLabelMap.description}"><textarea/></field> <field name="orderDate" title="${uiLabelMap.orderDate}"><date-time type="date" default-value="${nowTimestamp}"/></field> <field name="enable" title="${uiLabelMap.enable}" widget-style="required"> <drop-down allow-empty="false" no-current-selected-key="Y"> <option key="Y" description="${uiLabelMap.enableYes}"/> <option key="N" description="${uiLabelMap.enableNo}"/> </drop-down> </field> <field use-when="entity==null" name="submitButton" title="${uiLabelMap.CommonCreate}" widget-style="smallSubmit"><submit button-type="button"/></field> <field use-when="entity!=null" name="submitButton" title="${uiLabelMap.CommonUpdate}" widget-style="smallSubmit"><submit button-type="button"/></field> <field name="GobackOrder" title=" " widget-style="buttontext"> <hyperlink target="FindOrder" description="${uiLabelMap.CommonGoBack}"/> </field> </form> <form name="ViewOrder" type="single" default-map-name="entity" header-row-style="header-row" default-table-style="basic-table"> <actions> <set field="enable" from-field="entity.enable"/> </actions> <field name="orderId" title="${uiLabelMap.orderId}"><display/></field> <field name="orderName" title="${uiLabelMap.orderName}"><display/></field> <field name="description"><display/></field> <field name="orderDate" title="${uiLabelMap.orderDate}"><display type="date-time"/></field> <field name="orderTypeId" title="${uiLabelMap.orderTypeId}"> <display-entity entity-name="Enumeration" description="${description}" key-field-name="enumId"/> </field> <field use-when=""Y".equals(enable)" name="enable" title="${uiLabelMap.enable}"> <display description="${uiLabelMap.enableYes}"/> </field> <field use-when="!"Y".equals(enable)" name="enable" title="${uiLabelMap.enable}"> <display description="${uiLabelMap.enableNo}"/> </field> <field name="GobackOrder" title=" " widget-style="buttontext"> <hyperlink target="FindOrder" description="${uiLabelMap.CommonGoBack}"/> </field> </form>
<!-- component://order/servicedef/services.xml #xpp服务配置 --> <!-- entity-auto Order Services xpp --> <service name="createOrder" default-entity-name="Order" engine="entity-auto" invoke="create" auth="true"> <description>Create a Orderxpp</description> <auto-attributes include="pk" mode="INOUT" optional="true"/> <auto-attributes include="nonpk" mode="IN" optional="true"/> </service> <service name="updateOrder" default-entity-name="Order" engine="entity-auto" invoke="update" auth="true"> <description>Update a Orderxpp</description> <auto-attributes include="pk" mode="IN" optional="false"/> <auto-attributes include="nonpk" mode="IN" optional="true"/> </service> <service name="deleteOrder" default-entity-name="Order" engine="entity-auto" invoke="delete" auth="true"> <description>Delete a Order</description> <auto-attributes include="pk" mode="IN" optional="false"/> </service>
到此, 一个简单的CRUD 代码以及涉及到的模块文件路径都列出来了!
希望能对广大新人有帮助!
后续
附送 java 版实现代码
<!-- Java Orderxpp Services --> <service name="createOrderJava" default-entity-name="Order" engine="java" auth="true" invoke="createOrder" location="org.ofbiz.order.OrderServices"> <description>Create a Order</description> <auto-attributes include="pk" mode="INOUT" optional="true"/> <auto-attributes include="nonpk" mode="IN" optional="true"/> </service> <service name="updateOrderJava" default-entity-name="Order" engine="java" auth="true" invoke="updateOrder" location="org.ofbiz.order.OrderServices"> <description>Update a Orderxpp</description> <auto-attributes include="pk" mode="INOUT" optional="false"/> <auto-attributes include="nonpk" mode="IN" optional="true"/> </service> <service name="deleteOrderJava" default-entity-name="Order" engine="java" auth="true" invoke="deleteOrder" location="org.ofbiz.order.OrderServices"> <description>Delete axpp Order</description> <auto-attributes include="pk" mode="IN" optional="false"/> </service>
/** * Order Servicesxpp * @author mf1389004071 xpp <br/> * 2014年12月12日-|-上午11:59:07 */ public class OrderServices { public static final String module = OrderServices.class.getName(); public static final String resource = "OrderUiLabels"; /** * create xpp Order * @param dctx * @param context * @return */ public static Map<String, Object> createOrder(DispatchContext dctx, Map<String, ? extends Object> context) { Locale locale = (Locale) context.get("locale"); Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); String orderId = (String) context.get("orderId"); if(UtilValidate.isEmpty(orderId)){ orderId = delegator.getNextSeqId("Order"); } result.put("orderId", orderId); GenericValue gv = delegator.makeValue("Order", UtilMisc.toMap("orderId", orderId)); gv.setNonPKFields(context); try { gv.create(); } catch (GenericEntityException e) { Debug.logError(e, e.getMessage(), module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "Order.create.error", new Object[] { e.getMessage() }, locale)); } return result; } /** * update Order xpp * @param dctx * @param context * @return */ public static Map<String, Object> updateOrder(DispatchContext dctx, Map<String, ? extends Object> context) { Locale locale = (Locale) context.get("locale"); Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); String orderId = (String) context.get("orderId"); result.put("orderId", orderId); try { GenericValue gv = delegator.findByPrimaryKey("Order", UtilMisc.toMap("orderId", orderId)); if(UtilValidate.isEmpty(gv)){ return ServiceUtil.returnError(UtilProperties.getMessage(resource, "Order.update.error.entity.notFound", locale)); } gv.setNonPKFields(context); gv.store(); }catch (GenericEntityException e) { Debug.logError(e, e.getMessage(), module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "Order.update.error", new Object[] { e.getMessage() }, locale)); } return result; } /** * delete xppOrder * @param dctx * @param context * @return */ public static Map<String, Object> deleteOrder(DispatchContext dctx, Map<String, ? extends Object> context) { Locale locale = (Locale) context.get("locale"); Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); String orderId = (String) context.get("orderId"); try { GenericValue gv = delegator.findByPrimaryKey("Order", UtilMisc.toMap("orderId", orderId)); if(UtilValidate.isEmpty(gv)){ return ServiceUtil.returnError(UtilProperties.getMessage(resource, "Order.delete.error.entity.notFound", locale)); } gv.remove(); }catch (GenericEntityException e) { Debug.logError(e, e.getMessage(), module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "Order.delete.error", new Object[] { e.getMessage() }, locale)); } return result; } }
==================================关于本文===================================
作者: mf1389004071
上海_香飘飘
博客: http://ofbizer.iteye.com/有此标记, 均为原创, 禁止盗用, 转载请注明来源
技术无上限, 能力有上限, 欢迎参与交流讨论和提出建议
本文永久地址: http://ofbizer.iteye.com/blog/2272549