ofbiz中常见的一些用法及操作总结

1.一个组件下各目录或文件的用途:

 

build目录是已编译的java代码和任何Java库。它们能够使这个应用程序运行。(但是不能够仅仅靠它们运行。可以仅靠webapp目录下的一个或者多个web应用程序运行)

build.xml文件是ant文件,用于测试和构建这个应用程序。

config目录包含配置文件,例如多语言下的国际化UI标签的配置文件。

data目录包含种子和演示数据,xml格式。

entitydef目录,包含这个应用程序的数据模型定义。

script脚本目录,包含业务逻辑的脚本文件。

servicedef目录,包含services服务,它们是细粒度的业务逻辑(类似于方法或函数)。

src源文件目录,包含实现业务逻辑的Java类文件。

webapp目录,是web接口,是应用程序的前端。一个OFBIZ应用程序能有多个webapp应用程序。

-------------------------------------------------------------------------------------------------------------------------------------

2.ofbiz-component.xml文件中的几项配置

 

<resource-loader name="main" type="component"/>

<classpath type="jar" location="build/lib/*"/>

<classpath type="dir" location="script"/>

<classpath type="dir" location="config"/>

 

<entity-resource type="model" reader-name="main" loader="main" location="entitydef/entityModel.xml"/>

<entity-resource type="group" reader-name="main" loader="main" location="entitydef/entityGroup.xml"/>

<entity-resource type="eca" reader-name="main" loader="main" location="entitydef/eecas.xml"/> 

<entity-resource type="data" reader-name="seed" loader="main" location="data/HobbiesData.xml"/>

 

<service-resource type="model" loader="main" location="servicedef/services.xml"/>

<service-resource type="eca" loader="main" location="servicedef/secas.xml"/>

-------------------------------------------------------------------------------------------------------------------------------------

3.controller.xml文件中的请求资源路径配置

 

<request-map uri="createPerson">

  <event type="service" invoke="createHelloPerson"/>

  <response name="success" type="view" value="guestbook"/>

  <response name="error" type="view" value="guestbook"/>

</request-map>

 

<view-map name="guestbook" type="screen" page="component://hello/widget/HelloScreens.xml#guestbook"/>

-------------------------------------------------------------------------------------------------------------------------------------

4.数据模型定义xml文件

 

<entity entity-name="Department" package-name="org.ofbiz.practice">

  <field name="departmentId" type="id-ne" col-name="departmentId"><description></description>  </field>

  <field name="departmentName" type="name" col-name="departmentName"></field>

  <prim-key field="departmentId"/>

</entity>

-------------------------------------------------------------------------------------------------------------------------------------

5.细粒度的业务逻辑定义xml文件

 

<service name="createHelloPerson" engine="java" location="org.ofbiz.hello.HelloServices" invoke="createHelloPerson">

  <description>Create a HelloPerson</description>

  <auto-attributes entity-name="HelloPerson" mode="IN" include="nonpk" optional="true"/>

  <attribute name="helloPersonId" type="String" mode="OUT" optional="false"/>

</service>

 

<service name="createHelloPersonHobby" engine="simple" location="org/ofbiz/hello/HelloServices.xml" invoke="createHelloPersonHobby">

  <description>Create a HelloPersonHobby which links a person and a hobby</description>

  <auto-attributes entity-name="HelloPersonHobby" mode="IN" include="pk" optional="false"/>

</service>

-------------------------------------------------------------------------------------------------------------------------------------

6.decorator-screen的一般用法

 

<screens>

 <screen name="CommonDecorator">

  <section>

  <widgets>

   <platform-specific><html><html-template location="component://hello/webapp/hello/includes/header.ftl"/></html></platform-specific>

   <decorator-section-include name="body"/>

   <platform-specific><html><html-template location="component://hello/webapp/hello/includes/footer.ftl"/></html></platform-specific>

  </widgets>

  </section>

 </screen>

 <screen name="main">

  <section>

  <widgets>

   <decorator-screen name="CommonDecorator">

    <decorator-section name="body">

    <platform-specific><html><html-template location="component://hello/webapp/hello/main.ftl"/></html></platform-specific>

    </decorator-section>

   </decorator-screen>

  </widgets>

  </section>

 </screen>

</screens>

-------------------------------------------------------------------------------------------------------------------------------------

7.people.bsh

 

import org.ofbiz.entity.*;

 

delegator = request.getAttribute("delegator");

 

persons = delegator.findAll("Person");

 

context.put("persons", persons);

-------------------------------------------------------------------------------------------------------------------------------------

8.people.groovy

 

context.persons = delegator.findList("HelloPerson", null, null, null, null, false);

-------------------------------------------------------------------------------------------------------------------------------------

9.people.ftl

 

<#if persons?has_content>

Some of the famous celebrities who have visited our site:

<ul>

<#list persons as person>

<li>${person.firstName?if_exists} ${person.lastName?if_exists}</li>

</#list>

</ul>

</#if>

-------------------------------------------------------------------------------------------------------------------------------------

10.在widget-screen前用actions的方式来为form准备数据

 

<actions>

 <!-- find HelloPerson by condition,since there is no conditions,all values of HelloPerson are returned -->

 <entity-condition entity-name="HelloPerson" list-name="allGuests">

  <order-by field-name="helloPersonId"/>

 </entity-condition>

</actions>

 

<actions>

 <set field="helloPersonId" from-field="parameters.helloPersonId"/>

 <!-- find HelloPersonHobby from helloPersonId, equivalent to a delegator.findByAnd(...) -->

 <entity-and entity-name="HelloPersonHobby" list-name="allHobbies">

  <field-map env-name="helloPersonId"/>

  <order-by field-name="helloHobbyId"/>

 </entity-and>

 

 <!-- find HelloPerson from helloPersonId, equivalent to a delegator.findByPrimaryKey(...) -->

 <entity-one entity-name="HelloPerson" value-name="person">

  <field-map env-name="helloPersonId"/>

 </entity-one>

</actions>

-------------------------------------------------------------------------------------------------------------------------------------

11.在form中用action准备好的数据来显示页面

 

<form name="GuestBookList" type="list" list-name="allGuests">

 <auto-field-entity entity-name="HelloPerson" default-field-type="display"/>

 <field name="hobbies">

  <hyperlink target="hobbies?helloPersonId=${helloPersonId}" description="See Hobbies"/>

 </field>

</form>

<form name="AddGuest" type="single" target="createPerson">

 <auto-field-entity entity-name="HelloPerson"/>

 <field name="helloPersonId"><hidden/></field>

 <field name="submitButton" title="Add a Guest" widget-style="standardSubmit">

  <submit button-type="button"/>

 </field>

</form>

 

<form name="HobbiesList" type="list" list-name="allHobbies">

 <field name="helloHobbyId" title="Hobby">

  <display-entity entity-name="HelloHobby" description="${description}"/>

 </field>

</form>

 

<!--使用ofbiz自带的查询服务-->

<form name="ListDepartment" type="list" list-name="listIt" paginate-target="findDept" default-entity-name="Department">

<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="Department"/>

 </service>

</actions>

</form>

-------------------------------------------------------------------------------------------------------------------------------------

12.HelloServices(Java)服务的第一种表现方式

 

public static final String module = HelloServices.class.getName();// used for debugging

 

public static Map createHelloPerson(DispatchContext dctx, Map context){

 GenericDelegator delegator = dctx.getDelegator();

 try{

  String helloPersonId = delegator.getNextSeqId("HelloPerson");

  Debug.logInfo("helloPersonId = " + helloPersonId, module); // prints to the console or console.log

  GenericValue helloPerson = delegator.makeValue("HelloPerson", UtilMisc.toMap("helloPersonId", helloPersonId));

  helloPerson.setNonPKFields(context);

  delegator.create(helloPerson);

 

  Map result = ServiceUtil.returnSuccess();

  result.put("helloPersonId", helloPersonId);

  return result;

 }catch(GenericEntityException ex){

  return ServiceUtil.returnError(ex.getMessage());

 }

}

-------------------------------------------------------------------------------------------------------------------------------------

13.HelloServices(Minilang)服务的第二种表现方式

 

Minilang比较起来是简单的。 简单的minilang service在script/目录里面并且是一个XML文件。 由于它是专门为共同的OFBiz应用任务设计,例如 查询数据,存放数据,检查premissions,并且与现有的实体一起使用,并且执行业务逻辑,它使那些任务工作非常容易:

 

<simple-method method-name="createHelloPersonHobby" short-description="create a Hello-Person relationship" login-required="false">

 <make-value entity-name="HelloPersonHobby" value-name="newEntity"/>

 <set-nonpk-fields map-name="parameters" value-name="newEntity"/>

 <set-pk-fields map-name="parameters" value-name="newEntity"/>

 <create-value value-name="newEntity"/>

</simple-method>

-------------------------------------------------------------------------------------------------------------------------------------

14.ofbiz form中下拉列表的代码

 

<field name="agreementTypeId" title="${uiLabelMap.AccountingAgreementTypeId}">

    <drop-down allow-empty="true">

        <entity-options description="${description}" entity-name="AgreementType" key-field-name="agreementTypeId"/>

    </drop-down>

</field>

以上是在form中显示下拉列表的代码示例,title是下拉列表前的说明文字,entity-name是下拉列表表项的取值实体,description是下拉列表显示的表项,此处,下拉列表的表项从实体AgreementType中的description域取值.另外,标签中的allow-empty如果为ture则允许该下拉菜单为空,如果为false则必须在下拉列表中选择其一.

-------------------------------------------------------------------------------------------------------------------------------------

15.ofbiz form表头汉化示例

 

<form name="ContactList" type="list" list- name="allContacts">        

   <auto-fields-entity entity-name="Contact" default-field-type="display"/> 

   <field name="contactId" title="联系人ID"></field>

   <field name="name" title="姓名"></field>

   <field name="duty" title="职位"></field>

   <field name="responsibility" title="职责"></field>

   <field name="corporation" title="单位"></field>

   <field name="email" title="E-mail"></field>

   <field name="tel" title="电话"></field>

   <field name="msn" title="MSN"></field>

   <field name="qq" title="QQ"></field>

</form>

首先,<auto-fields-entity entity-name="Contact" default-field-type="display"/> 先将实体Contact的所有域取出来,如果下面不对各域作具体指定则直接根据display的格式显示各域.其次,下面的每一个条<field name="contactId" title="联系人ID"></field>语句都将对应域的表头进行汉化.

-------------------------------------------------------------------------------------------------------------------------------------

16.ofbiz查找功能关键代码

 

其中FindTest表单是用于输入查询条件的表单,ResultTest表单是用于显示查询结果的表单.两张表单在同一页面上显示.其中,Test是实体名.

<form name="FindTest" target="main" type="single">

   <auto-fields-entity entity-name="Test" default-field-type="find"/>

   <field name="submitButton" title="查找" widget-style="smallSubmit">

      <submit button-type="button"/>

   </field>

</form>

 

<form name="ResultTest" list-iterator-name="listIt" target="" paginate-target="main" title="" type="list">

  <actions>

    <set field="entityName" value="Test"/>

    <service service-name="performFind" result-map-name="result" result-map-list-iterator-name="listIt">

      <field-map field-name="inputFields" env-name="requestParameters"/>

      <field-map field-name="entityName" env-name="entityName"/>

    </service>

  </actions>

  <auto-fields-entity entity-name="Test" default-field-type="display"/>

</form>

 

小结:

    查询功能不需要minilanguage或java来实现.输入查询条件的表单type为single,target指向的是当前页面,auto-fields-entity元素的type为find.

    显示查询结果的表单比较特别,该表单中有<action>部分,其中的代码就是实现查询功能的代码,具体使用时修改实体名即可.和其它表单一样,可以指定具体域有特殊的显示效果或隐藏.

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