ofbiz开发入门之CmsBackEnd实现的CRUD

昨晚快12点的时候看到耀哥回复我附件里面有相应的下载,于是我就下载完就睡了。今早上不到七点起来洗漱完开始着手练习任务。虽然我之前也已经自己在网上下载到耀哥去年上传的那两压缩包,但是比较发现昨晚下载的那个版本文件最小,我想那这个版本应该是最轻量级的吧,所以就干脆拿这个版本以CmsRegister为例做ofbiz的CRUD练习演示。
下面是我的步骤:

1.下载附件并解压,删除__MACOSX目录(猜想应该是MAC操作系统里面的),保留cmsbackend文件夹

 

2.将cmsbackend文件夹拷贝到之前搭建好的ofbiz环境中的specialpurpose目录下

 

3.展开cmsbackend目录,选择src目录,点击右键Build Path-->User As Source Folder

 

4.打开specialpurpose目录下的component-load.xml,在component-loader节点后面添加一行<load-component component-location="cmsbackend"/>将cmsbackend组件挂上,这样下次再load-demo的时候实体引擎就会解析该组件下的实体创建表并将种子数据加入到数据库表中

 

5.打开cmsbackend\entitydef\entitymodel.xml,在最下方添加实体定义代码,内容如下:
 <entity entity-name="CmsRegister" packge-name="org.ofbiz.cms.register" title="CmsRegister Entity">
  <field name="id" type="id-ne"></field>
  <field name="username" type="name"></field>
  <field name="realname" type="name"></field>
  <field name="email" type="name"></field>
  <field name="description" type="very-long"></field>
  <prim-key field="id"/>
 </entity>
 
6.在cmsbackend目录下的config目录下新建CmsRegisterUILabels.xml用于控制UI的title属性,内容如下:
 <?xml version="1.0" encoding="UTF-8"?>
 <resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <property key="CmsRegister">
   <value xml:lang="en">register</value>
   <value xml:lang="zh">注册管理</value>
  </property>
  <property key="CmsRegisterId">
   <value xml:lang="en">id</value>
   <value xml:lang="zh">注册ID</value>
  </property>
  <property key="CmsRegisterUsername">
   <value xml:lang="en">username</value>
   <value xml:lang="zh">用户名</value>
  </property>
  <property key="CmsRegisterRealname">
   <value xml:lang="en">realname</value>
   <value xml:lang="zh">真实姓名</value>
  </property>
  <property key="CmsRegisterEmail">
   <value xml:lang="en">email</value>
   <value xml:lang="zh">电子邮箱</value>
  </property>
  <property key="CmsRegisterDescription">
   <value xml:lang="en">description</value>
   <value xml:lang="zh">自我介绍</value>
  </property>
  <property key="CmsRegisterCreate">
   <value xml:lang="en">create</value>
   <value xml:lang="zh">新建</value>
  </property>
 </resource>

 

7.在cmsbackend\widget\CommonScreens.xml下的main-decorator节点中注入CmsRegisterUiLabels,即<property-map resource="CmsRegisterUiLabels" map-name="uiLabelMap" global="true"/>

 

8.在CmsBackendMenus.xml文件中添加“注册管理”的菜单项,即
 <menu-item name="register" title="${uiLabelMap.CmsRegister}">
   <link target="FindCmsRegister" />
 </menu-item>
 
9.根据菜单“注册管理”的链接FindCmsRegister找到cmsbackend应用下的controller.xml配置文件,给其中添加相应的配置
 <request-map uri="FindCmsRegister">
  <security https="true" auth="true"/>
  <response name="success" type="view" value="FindCmsRegister"/>
 </request-map>
 
 <view-map name="FindCmsRegister" type="screen" page="component://cmsbackend/widget/CmsBackendScreens.xml#FindCmsRegister"/>

 

10.在cmsbackend\widget\CmsBackendScreens.xml中添加一个name="FindCmsRegister"的screen,内容如下:
 <screen name="FindCmsRegister">
        <section>
            <actions>
                <set field="headerItem" value="CmsRegister"/>
                <set field="titleProperty" value="CmsRegister"/>
                <set field="pCtx" from-field="parameters"/>
            </actions>
            <widgets>
                <decorator-screen name="CmsBackendCommonDecorator" location="${parameters.mainDecoratorLocation}">
                    <decorator-section name="body">
                                <decorator-screen name="FindScreenDecorator" location="component://common/widget/CommonScreens.xml">
                                    <decorator-section name="menu-bar">
                                        <container style="button-bar">
                                            <link target="EditCmsRegister" link-type="anchor" text="${uiLabelMap.CmsRegisterEdit}" style="buttontext create"/>
                                        </container>
                                    </decorator-section>
                                    <decorator-section name="search-options">
                                        <include-form name="FindCmsRegister" location="component://cmsbackend/widget/CmsBackendForms.xml"/>
                                    </decorator-section>
                                    <decorator-section name="search-results">
                                        <include-form name="ListCmsRegister" location="component://cmsbackend/widget/CmsBackendForms.xml"/>
                                    </decorator-section>
                                </decorator-screen>
                    </decorator-section>
                </decorator-screen>
            </widgets>
        </section>
    </screen>

 

11.上一步中涉及到注入CmsBackendForms.xml中两个表单,一个用于条件查询,一个用于列表展示,内容如下:
 <!--用于查询的表单-->
 <form name="FindCmsRegister" type="single" target="FindCmsRegister" default-entity-name="CmsRegister">
     <field name="noConditionFind"><hidden value="Y"/></field>
   <field name="username" title="${uiLabelMap.CmsRegisterUsername}" >
         <text-find hide-options="true"/>
     </field>
   <field name="realname" title="${uiLabelMap.CmsRegisterRealname}" >
         <text-find hide-options="true"/>
     </field>
   <field name="email" title="${uiLabelMap.CmsRegisterEmail}" >
         <text-find hide-options="true"/>
     </field>
     <field name="description" title="${uiLabelMap.CmsRegisterDescription}" >
         <text-find hide-options="true"/>
     </field>   
   <field name="searchButton" title="${uiLabelMap.CommonFind}" widget-style="smallSubmit">
       <submit button-type="button"/>
   </field>
   </form>
   <!--用于列表展示的表单-->
    <form name="ListCmsRegister" type="list" list-name="listIt" paginate-target="FindCmsRegister" default-entity-name="CmsRegister" separate-columns="true"
        odd-row-style="alternate-row" header-row-style="header-row-2" default-table-style="basic-table hover-bar">
      <actions>
          <service service-name="performFind" result-map="result" result-map-list="listIt">
              <field-map field-name="inputFields" from-field="pCtx"/>
              <field-map field-name="entityName" value="CmsRegister"/>
              <field-map field-name="orderBy" from-field="parameters.sortField"/>
              <field-map field-name="viewIndex" from-field="viewIndex"/>
              <field-map field-name="viewSize" from-field="viewSize"/>
          </service>
      </actions>     
      <field name="id" title="${uiLabelMap.CmsRegisterId}" sort-field="true">
        <display/>
    </field>
    <field name="username" title="${uiLabelMap.CmsRegisterUsername}" sort-field="true">
        <display/>
    </field>
    <field name="realname" title="${uiLabelMap.CmsRegisterRealname}" sort-field="true">
        <display/>
    </field>
    <field name="email" title="${uiLabelMap.CmsRegisterEmail}" sort-field="true">
        <display/>
    </field>
    <field name="edit" title="${uiLabelMap.CommonEdit}">
            <hyperlink also-hidden="false" description="${uiLabelMap.CommonEdit}" target="EditCmsRegister">
                <parameter param-name="id"/>
            </hyperlink>
      </field>
   <field name="delete" title="${uiLabelMap.CommonDelete}">
            <hyperlink also-hidden="false" description="${uiLabelMap.CommonDelete}" target="deleteCmsRegister">
                <parameter param-name="id"/>
            </hyperlink>
      </field>
    </form>
   
  说明:查询功能室借助ofbiz自带的查询功能来实现的,在ListCmsRegister表单中的<actions>中注入ofbiz自带的service

 

12.为了实现表单页面的充分利用让创建和修改公用同一个表单,在CmsBackendForms.xml中添加表单,内容如下:
  <form name="EditCmsRegister" type="single" target="updateCmsRegister" title="" default-map-name="entity" default-entity-name="CmsRegister">
     <alt-target use-when="entity==null" target="createCmsRegister"/>
     <field name="id" title="${uiLabelMap.CmsRegisterId}" required-field="true" widget-style="required">
      <text/>
   </field>
     <field name="username" title="${uiLabelMap.CmsRegisterUsername}" required-field="true" widget-style="required">
       <text/>
    </field>
    <field name="realname" title="${uiLabelMap.CmsRegisterRealname}" required-field="true" widget-style="required">
       <text/>
    </field>
    <field name="email" title="${uiLabelMap.CmsRegisterEmail}" >
       <text/>
    </field>
    <field name="description" title="${uiLabelMap.CmsRegisterDescription}" >
       <textarea />
    </field>
     
   <field name="submitButton" use-when="entity==null" title="${uiLabelMap.CommonCreate}"><submit button-type="button"/></field>
     <field name="submitButton" use-when="entity!=null" title="${uiLabelMap.CommonUpdate}"><submit button-type="button"/></field>
  </form>
 
13.删除的话就不用表单了,接下来需要做的就是在cmsbackend应用下的controller.xml配置文件中为相应的请求资源路径注入配置,内容如下:
  <request-map uri="EditCmsRegister"><security https="true" auth="true"/><response name="success" type="view" value="EditCmsRegister"/></request-map>
    <request-map uri="createCmsRegister">
        <event type="service" invoke="createCmsRegister"/>
        <response name="success" type="request-redirect-noparam" value="FindCmsRegister"></response>
    </request-map>
    <request-map uri="updateCmsRegister">
        <event type="service" invoke="updateCmsRegister"/>
        <response name="success" type="request-redirect" value="FindCmsRegister">
    </response>
    </request-map>
    <request-map uri="deleteCmsRegister">
        <event type="service" invoke="deleteCmsRegister"/>
        <response name="success" type="request-redirect-noparam" value="FindCmsRegister"></response>
    </request-map>
   
    <view-map name="EditCmsRegister" type="screen" page="component://cmsbackend/widget/CmsBackendScreens.xml#EditCmsRegister"/>
 

14.在CmsBackendScreens.xml配置文件中添加一个name="EditCmsRegister"的screen,内容如下:
  <screen name="EditCmsRegister">
        <section>
            <actions>
                <set field="titleProperty" value="CmsRegister"/>
                <set field="tabButtonItem" value="CmsRegister"/>
                <entity-one entity-name="CmsRegister" value-field="entity"/>
            </actions>
            <widgets>
                <decorator-screen name="CmsBackendCommonDecorator" location="${parameters.mainDecoratorLocation}">
                    <decorator-section name="body">
                        <include-form name="EditCmsRegister" location="component://cmsbackend/widget/CmsBackendForms.xml"/>
                    </decorator-section>
                </decorator-screen>
            </widgets>
        </section>
    </screen>

  
15.在cmsbackend应用下的servicedef目录中为services.xml配置文件配置相应的服务,内容如下:
  <service name="createCmsRegister" default-entity-name="CmsRegister" engine="simple" location="component://cmsbackend/script/org/ofbiz/cmsbackend/CmsBackendServices.xml" invoke="createCmsRegister" auth="true">
        <description>Create a CmsRegisterEvents</description>
        <auto-attributes include="pk" mode="IN" optional="false"/>
        <auto-attributes include="nonpk" mode="IN" optional="true"/>
    </service>

    <service name="deleteCmsRegister" default-entity-name="CmsRegister" engine="simple" location="component://cmsbackend/script/org/ofbiz/cmsbackend/CmsBackendServices.xml" invoke="deleteCmsRegister" auth="true">
        <description>Delete a CmsRegisterEvents</description>
        <auto-attributes include="pk" mode="IN" optional="false"/>
    </service>
    <service name="updateCmsRegister" default-entity-name="CmsRegister" engine="simple" location="component://cmsbackend/script/org/ofbiz/cmsbackend/CmsBackendServices.xml" invoke="updateCmsRegister" auth="true">
        <description>Update a CmsRegisterEvents</description>
        <auto-attributes include="pk" mode="IN" optional="false"/>
        <auto-attributes include="nonpk" mode="IN" optional="true"/>
    </service>
   
16.根据每个服务的location找到相对应的CmsBackendServices.xml配置文件,并在其中根据相应调用的方法名添加相应的服务,内容如下:
  <simple-method method-name="createCmsRegister" short-description="" login-required="true">
        <make-value entity-name="CmsRegister" value-field="newEntity" />
        <sequenced-id sequence-name="CmsRegister" field="newEntity.id"/>
        <set-nonpk-fields map="parameters" value-field="newEntity"/>
        <create-value value-field="newEntity"/>
    </simple-method>
   <simple-method method-name="updateCmsRegister" short-description="" login-required="true">
        <entity-one entity-name="CmsRegister" value-field="newEntity"><field-map field-name="id" from-field="parameters.id"/></entity-one>
        <set-nonpk-fields map="parameters" value-field="newEntity"/>
        <store-value value-field="newEntity"/>
   </simple-method>       
  <simple-method method-name="deleteCmsRegister" short-description="" login-required="true">
        <entity-one entity-name="CmsRegister" value-field="newEntity"><field-map field-name="id" from-field="parameters.id"/></entity-one>
        <remove-value value-field="newEntity"/>
   </simple-method>

 

17.将整个ofbiz的build.xml文件拖到ant窗口中,clean-all  -->  load-demo  -->  start

 

18.在浏览器中输入http://localhost:8080/cmsbackend

 

说明:以上这些步骤中涉及的代码大部分都是模仿example组件中的思路而弄得,所以大部分都是复制粘贴而来,时间也仅仅用了一个多小时就搞定了,接下来我会亲自用手写的方式做第二个版本,相信思路会更清晰,效率会更高些。

你可能感兴趣的:(ofbiz,crud,CmsBackend)