OFBiz进阶--OFBiz培训之[新手]单表CRUD代码模板
文章用途
本文内容代码为简单的单表CRUD代码模板替换成功后的Demo代码!
替换说明
按下列注释指定的关键字替换,
然后放到每块代码上对应对应的注释内提到的xml文件中,
重启项目即可使用!
<!--
app-name[模块名称]: learn;
entityTitle[表描述]: 学习;
entityModelName[表名]: Learn;
pkId[表主键名]: learnId;
type[类型]: learnTypeId;
name[名称]: learnName;
date[日期时间]: learnDate;
Screen/Menu/Form[文件前缀]: Learn;
webapp-site[app站点]: learn
-->
<!-- component://learn/entitydef/entitymodel.xml #表结构定义 -->
<entity entity-name="Learn" package-name="org.ofbiz.learnxpp">
<description>学习xpp</description>
<field name="learnId" type="id-ne"><description>ID</description></field>
<field name="learnTypeId" type="id-ne"><description>类型ID</description></field>
<field name="learnName" type="name"><description>名称</description></field>
<field name="learnDate" 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="learnId"/>
</entity>
<!-- component://learn/config/LearnUiLabels.xml #国际化资源配置xpp -->
<property key="FindLearn">
<value xml:lang="en">FindLearnxpp</value>
<value xml:lang="zh">管理学习</value>
</property>
<property key="EditLearn">
<value xml:lang="en">EditLearn</value>
<value xml:lang="zh">编辑学习</value>
</property>
<property key="ViewLearn">
<value xml:lang="en">ViewLearn</value>
<value xml:lang="zh">查看学习</value>
</property>
<property key="learnId">
<value xml:lang="en">ID</value>
<value xml:lang="zh">编号</value>
</property>
<property key="learnTypeId">
<value xml:lang="en">Type</value>
<value xml:lang="zh">类型</value>
</property>
<property key="learnName">
<value xml:lang="en">Name</value>
<value xml:lang="zh">名称</value>
</property>
<property key="learnDate">
<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://learn/widget/LearnMenus.xml #xpp菜单配置 -->
<menu-item name="FindLearn" title="${uiLabelMap.FindLearn}">
<link target="FindLearn" />
</menu-item>
<!-- component://learn/webapp/learn/WEB-INF/controller.xml #xpp请求配置 -->
<!-- Learn xpp Request -->
<request-map uri="FindLearn">
<security auth="true" https="true"/>
<response name="success" type="view" value="FindLearn" />
</request-map>
<request-map uri="EditLearn">
<security auth="true" https="true"/>
<response name="success" type="view" value="EditLearn" />
</request-map>
<request-map uri="ViewLearn">
<security auth="true" https="true"/>
<response name="success" type="view" value="ViewLearn" />
</request-map>
<request-map uri="createLearn">
<security auth="true" https="true"/>
<event type="service" invoke="createLearn" />
<response name="success" type="request-redirect-noparam" value="FindLearn" />
<response name="error" type="view" value="EditLearn"/>
</request-map>
<request-map uri="updateLearn">
<security auth="true" https="true"/>
<event type="service" invoke="updateLearn" />
<response name="success" type="request-redirect-noparam" value="FindLearn"/>
<response name="error" type="view" value="EditLearn"/>
</request-map>
<request-map uri="deleteLearn">
<security auth="true" https="true"/>
<event type="service" invoke="deleteLearn" />
<response name="success" type="request-redirect-noparam" value="FindLearn"/>
</request-map>
<!-- Learn View xpp -->
<view-map name="FindLearn" type="screen" page="component://learn/widget/LearnScreens.xml#FindLearn" />
<view-map name="ViewLearn" type="screen" page="component://learn/widget/LearnScreens.xml#ViewLearn" />
<view-map name="EditLearn" type="screen" page="component://learn/widget/LearnScreens.xml#EditLearn" />
<!-- component://learn/widget/LearnScreens.xml #xpp显示页面配置 -->
<screen name="FindLearn">
<section>
<actions>
<!-- html.head.title 显示描述xpp -->
<set field="titleProperty" value="FindLearn"/>
<!-- 菜单xpp高亮 -->
<set field="headerItem" value="FindLearn"/>
</actions>
<widgets>
<decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}">
<decorator-section name="body">
<include-form name="FindLearn" location="component://learn/widget/LearnForms.xml" />
<link target="EditLearn" text="${uiLabelMap.CommonCreate}" style="buttontext"/>
<include-form name="ListLearn" location="component://learn/widget/LearnForms.xml" />
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
<screen name="EditLearn">
<section>
<actions>
<!-- html.head.xpp 显示描述 -->
<set field="titleProperty" value="FindLearn"/>
<!-- 菜单高亮xpp -->
<set field="headerItem" value="FindLearn"/>
<!-- xpp业务数据 -->
<set field="learnId" from-field="parameters.learnId"/>
<entity-one entity-name="Learn" value-field="entity" />
</actions>
<widgets>
<decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}">
<decorator-section name="body">
<include-form name="EditLearn" location="component://learn/widget/LearnForms.xml" />
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
<screen name="ViewLearn">
<section>
<actions>
<!-- html.head.title 显示描述xpp -->
<set field="titleProperty" value="FindLearn"/>
<!-- xpp菜单高亮 -->
<set field="headerItem" value="FindLearn"/>
<!-- 业务数据xpp -->
<set field="learnId" from-field="parameters.learnId"/>
<entity-one entity-name="Learn" value-field="entity" />
</actions>
<widgets>
<decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}">
<decorator-section name="body">
<include-form name="ViewLearn" location="component://learn/widget/LearnForms.xml" />
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
<!-- component://learn/widget/LearnForms.xml #xpp显示页面内表单配置 -->
<!-- Forms xpp Learn -->
<form name="FindLearn" type="single" target="FindLearn" default-map-name="search"
default-entity-name="Learn" header-row-style="header-row" default-table-style="basic-table">
<field name="learnId" title="${uiLabelMap.learnId}"><text-find hide-options="true" /></field>
<field name="learnName" title="${uiLabelMap.learnName}" 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="learnDate_fld0_value" title="${uiLabelMap.learnDate}(${uiLabelMap.From})" widget-style="dateISO" position="2" >
<date-time type="date" />
</field>
<field name="learnDate_fld0_op"><hidden value="greaterThanEqualTo"/></field>
<field name="learnTypeId" title="${uiLabelMap.learnTypeId}">
<drop-down allow-empty="true">
<entity-options description="${description}" entity-name="Enumeration" key-field-name="enumId">
<entity-constraint name="enumTypeId" value="_NA_"/>
<entity-learn-by field-name="sequenceId"/>
</entity-options>
</drop-down>
</field>
<field name="learnDate_fld1_value" title="(${uiLabelMap.To})" widget-style="dateISO" position="2">
<date-time type="date" />
</field>
<field name="learnDate_fld1_op"><hidden value="lessThanEqualTo"/></field>
<field name="searchButton" title="${uiLabelMap.CommonFind}" widget-style="smallSubmit"><submit/></field>
</form>
<form name="ListLearn" list-name="listIt" type="list" paginate-target="FindLearn"
separate-columns="true" odd-row-style="alternate-row" default-table-style="basic-table">
<actions>
<set field="parameters.noConditionFind" value="Y"/>
<set field="entityNameSearch" value="Learn"/>
<set field="learnBySearch" value="+learnId"/>
<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="learnBy" from-field="learnBySearch"/>
<field-map field-name="viewIndex" from-field="viewIndex"/>
<field-map field-name="viewSize" from-field="viewSize"/>
</service>
</actions>
<field name="learnId" title="${uiLabelMap.learnId}" widget-style="buttontext" sort-field="true">
<hyperlink target="ViewLearn" description="${learnId}">
<parameter param-name="learnId"/>
</hyperlink>
</field>
<field name="learnName" title="${uiLabelMap.learnName}" sort-field="true"><display/></field>
<field name="learnDate" title="${uiLabelMap.learnDate}" sort-field="true"><display type="date-time"/></field>
<field name="learnTypeId" title="${uiLabelMap.learnTypeId}" sort-field="true">
<display-entity entity-name="Enumeration" description="${description}[${learnTypeId}]" 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="EditLearn" title="${uiLabelMap.CommonEdit}" widget-style="buttontext">
<hyperlink target="EditLearn" description="${uiLabelMap.CommonEdit}">
<parameter param-name="learnId"/>
</hyperlink>
</field>
<field name="deleteLearn" title="${uiLabelMap.CommonDelete}" widget-style="buttontext">
<hyperlink target="deleteLearn" confirmation-message="${uiLabelMap.confirmDeleteMsg}" target-type="intra-app" description="${uiLabelMap.CommonDelete}">
<parameter param-name="learnId"/>
</hyperlink>
</field>
</form>
<form name="EditLearn" type="single" target="updateLearn" default-map-name="entity"
header-row-style="header-row" default-table-style="basic-table">
<alt-target use-when="entity==null" target="createLearn" />
<field name="learnId"> <hidden /> </field>
<field name="learnTypeId" title="${uiLabelMap.learnTypeId}">
<drop-down allow-empty="true">
<entity-options description="${description}" entity-name="Enumeration" key-field-name="enumId">
<entity-constraint name="enumTypeId" value="_NA_"/>
<entity-learn-by field-name="sequenceId"/>
</entity-options>
</drop-down>
</field>
<field name="learnName" title="${uiLabelMap.learnName}"><text/></field>
<field name="description" title="${uiLabelMap.description}"><textarea/></field>
<field name="learnDate" title="${uiLabelMap.learnDate}"><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="GobackLearn" title=" " widget-style="buttontext">
<hyperlink target="FindLearn" description="${uiLabelMap.CommonGoBack}"/>
</field>
</form>
<form name="ViewLearn" 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="learnId" title="${uiLabelMap.learnId}"><display/></field>
<field name="learnName" title="${uiLabelMap.learnName}"><display/></field>
<field name="description"><display/></field>
<field name="learnDate" title="${uiLabelMap.learnDate}"><display type="date-time"/></field>
<field name="learnTypeId" title="${uiLabelMap.learnTypeId}">
<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="GobackLearn" title=" " widget-style="buttontext">
<hyperlink target="FindLearn" description="${uiLabelMap.CommonGoBack}"/>
</field>
</form>
<!-- component://learn/servicedef/services.xml #xpp服务配置 -->
<!-- entity-auto Learn Services xpp -->
<service name="createLearn" default-entity-name="Learn" engine="entity-auto"
invoke="create" auth="true">
<description>Create a Learnxpp</description>
<auto-attributes include="pk" mode="INOUT" optional="true"/>
<auto-attributes include="nonpk" mode="IN" optional="true"/>
</service>
<service name="updateLearn" default-entity-name="Learn" engine="entity-auto"
invoke="update" auth="true">
<description>Update a Learnxpp</description>
<auto-attributes include="pk" mode="IN" optional="false"/>
<auto-attributes include="nonpk" mode="IN" optional="true"/>
</service>
<service name="deleteLearn" default-entity-name="Learn" engine="entity-auto"
invoke="delete" auth="true">
<description>Delete a Learn</description>
<auto-attributes include="pk" mode="IN" optional="false"/>
</service>
到此, 一个简单的CRUD 代码以及涉及到的模块文件路径都列出来了!
希望能对广大新人有帮助!
后续
附送 Minilang 版实现代码
<!-- Simple Learn Services -->
<service name="createLearnSimple" default-entity-name="Learn" engine="simple" auth="true"
invoke="createLearn" location="component://learn/script/org/ofbiz/learn/LearnServices.xml">
<description>Create a Learn</description>
<auto-attributes include="pk" mode="INOUT" optional="true"/>
<auto-attributes include="nonpk" mode="IN" optional="true"/>
</service>
<service name="updateLearnSimple" default-entity-name="Learn" engine="simple" auth="true"
invoke="updateLearn" location="component://learn/script/org/ofbiz/learn/LearnServices.xml">
<description>Update a Learn</description>
<auto-attributes include="pk" mode="IN" optional="false"/>
<auto-attributes include="nonpk" mode="IN" optional="true"/>
</service>
<service name="deleteLearnSimple" default-entity-name="Learn" engine="simple" auth="true"
invoke="deleteLearn" location="component://learn/script/org/ofbiz/learn/LearnServices.xml">
<description>Delete a Learn</description>
<auto-attributes include="pk" mode="IN" optional="false"/>
</service>
<!-- component://learn/script/org/ofbiz/learn/LearnServices.xml #服务的 Simple 实现 -->
<simple-method method-name="createLearn" short-description="create Learn" login-required="true">
<make-value value-field="entity" entity-name="Learn"/>
<set-pk-fields value-field="entity" map="parameters"/>
<if>
<condition><if-empty field="parameters.learnId"></if-empty></condition>
<then>
<sequenced-id sequence-name="Learn" field="entity.learnId"/>
</then>
</if>
<set-nonpk-fields value-field="entity" map="parameters"/>
<create-value value-field="entity"/>
</simple-method>
<simple-method method-name="updateLearn" short-description="Update an Learn" login-required="true">
<entity-one entity-name="Learn" value-field="entity">
<field-map field-name="learnId" from-field="parameters.learnId"/>
</entity-one>
<set-nonpk-fields map="parameters" value-field="entity"/>
<store-value value-field="entity"/>
</simple-method>
<simple-method method-name="deleteLearn" short-description="Delete an Learn" login-required="true">
<entity-one entity-name="Learn" value-field="entity">
<field-map field-name="learnId" from-field="parameters.learnId"/>
</entity-one>
<remove-value value-field="entity"/>
</simple-method>
附送 java 版实现代码
<!-- Java Learnxpp Services -->
<service name="createLearnJava" default-entity-name="Learn" engine="java" auth="true"
invoke="createLearn" location="org.ofbiz.learn.LearnServices">
<description>Create a Learn</description>
<auto-attributes include="pk" mode="INOUT" optional="true"/>
<auto-attributes include="nonpk" mode="IN" optional="true"/>
</service>
<service name="updateLearnJava" default-entity-name="Learn" engine="java" auth="true"
invoke="updateLearn" location="org.ofbiz.learn.LearnServices">
<description>Update a Learnxpp</description>
<auto-attributes include="pk" mode="INOUT" optional="false"/>
<auto-attributes include="nonpk" mode="IN" optional="true"/>
</service>
<service name="deleteLearnJava" default-entity-name="Learn" engine="java" auth="true"
invoke="deleteLearn" location="org.ofbiz.learn.LearnServices">
<description>Delete axpp Learn</description>
<auto-attributes include="pk" mode="IN" optional="false"/>
</service>
/**
* Learn Servicesxpp
* @author mf1389004071 xpp <br/>
* 2014年12月12日-|-上午11:59:07
*/
public class LearnServices {
public static final String module = LearnServices.class.getName();
public static final String resource = "LearnUiLabels";
/**
* create xpp Learn
* @param dctx
* @param context
* @return
*/
public static Map<String, Object> createLearn(DispatchContext dctx,
Map<String, ? extends Object> context) {
Locale locale = (Locale) context.get("locale");
Map<String, Object> result = ServiceUtil.returnSuccess();
Delegator delegator = dctx.getDelegator();
String learnId = (String) context.get("learnId");
if(UtilValidate.isEmpty(learnId)){
learnId = delegator.getNextSeqId("Learn");
}
result.put("learnId", learnId);
GenericValue gv = delegator.makeValue("Learn", UtilMisc.toMap("learnId", learnId));
gv.setNonPKFields(context);
try { gv.create(); }
catch (GenericEntityException e) {
Debug.logError(e, e.getMessage(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"Learn.create.error", new Object[] { e.getMessage() }, locale));
}
return result;
}
/**
* update Learn xpp
* @param dctx
* @param context
* @return
*/
public static Map<String, Object> updateLearn(DispatchContext dctx,
Map<String, ? extends Object> context) {
Locale locale = (Locale) context.get("locale");
Map<String, Object> result = ServiceUtil.returnSuccess();
Delegator delegator = dctx.getDelegator();
String learnId = (String) context.get("learnId");
result.put("learnId", learnId);
try {
GenericValue gv = delegator.findByPrimaryKey("Learn", UtilMisc.toMap("learnId", learnId));
if(UtilValidate.isEmpty(gv)){
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"Learn.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,
"Learn.update.error", new Object[] { e.getMessage() }, locale));
}
return result;
}
/**
* delete xppLearn
* @param dctx
* @param context
* @return
*/
public static Map<String, Object> deleteLearn(DispatchContext dctx,
Map<String, ? extends Object> context) {
Locale locale = (Locale) context.get("locale");
Map<String, Object> result = ServiceUtil.returnSuccess();
Delegator delegator = dctx.getDelegator();
String learnId = (String) context.get("learnId");
try {
GenericValue gv = delegator.findByPrimaryKey("Learn", UtilMisc.toMap("learnId", learnId));
if(UtilValidate.isEmpty(gv)){
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"Learn.delete.error.entity.notFound", locale));
}
gv.remove();
}catch (GenericEntityException e) {
Debug.logError(e, e.getMessage(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"Learn.delete.error", new Object[] { e.getMessage() }, locale));
}
return result;
}
}
==================================关于本文===================================
作者: mf1389004071
上海_香飘飘
有此标记, 均为原创, 禁止盗用, 转载请注明来源
技术无上限, 能力有上限, 欢迎参与交流讨论和提出建议
learnBy 应该是orderby吧