本教程是ofbiz 基本应用,它涵盖了OFBiz应用程序开发过程的基本原理。目标是使开发人员熟悉最佳实践,编码惯例,基本控制流程以及开发人员对OFBiz定制所需的所有其他方面。
本教程将帮助您在OFBiz中构建您的第一个“演示应用程序”。
· 概述(OFBiz简介)
· 设置和运行OFBiz
· 下载Apache OFBiz框架
· 运行Apache OFBiz
· 创建您的第一个应用程序(Hello World ...)
· 组件简介
· 创建组件
· 运行你的第一个应用程序
· 创建第一个数据库实体(表)
· 定义实体
· 在实体中加载数据
· 表格和服务
· 创建服务
· 创建添加表单
· 控制器输入表单
· 创建查找表单
· 使用UI标签
· 使用其他引擎的服务
· Java中的服务
· 服务在Groovy
· 活动
· 活动示范
· 服务与事件之间的区别
· 自定义用户界面
· 使用FreeMarker模板和Groovy脚本
·
· 创建自定义装饰器
· 下一步是什么?
1.1 概述(OFBiz简介)
开放业务(OFBiz)是一套基于通用架构的企业应用程序,采用通用数据,逻辑和流程组件。松散耦合的应用程序使得这些组件易于理解,扩展和定制。
OFBiz的工具和架构使得开发和维护企业应用程序变得容易。这使得我们作为项目的创建者和维护者有可能快速发布新功能,并在没有广泛的努力的情况下维护现有的功能。当您有特定需要时,还可以轻松定制和扩展现有功能。
单独的架构使您可以更轻松地根据需要自定义应用程序,但如果系统未作为开源软件分发,系统中许多最佳的灵活性点将是无意义的,甚至是不可能的。OFBiz是根据Apache许可证版本2.0授权的,它授予您自定义,扩展,修改,重新包装,转售和系统许多其他潜在用途的权利。
对这些活动没有限制,因为我们认为这些活动对于有效使用此类软件是必要的。与其他开源许可证(如GPL)不同,您的更改不必作为开放源代码发布。对核心项目进行某些改进,修复和补充有明显的好处,但一些更改将涉及不得向公众发布的专有或机密信息。因此,OFBiz使用不需要此功能的Apache许可证版本2.0。有关开源许可的更多信息,请参阅开源计划(OSI)网站 www.opensource.org。
这个开源模式的另一个好处是,我们收到来自使用该软件的用户的不断反馈。我们收到了OFBiz的用户和潜在用户的无数错误修复,改进建议和最佳实践业务建议。该项目中许多最大的功能都受到与发送到与项目相关联的邮件列表的一些评论或建议的启发。有几十个组织使用该软件,并且可能使用该项目中的一个或另一个部署了数百个部署的站点,我们通常每天会收到20-30封电子邮件关于该项目。
为了确保我们的功能是及时和有用的,我们始终从研究我们正在开发的任何组件的公共标准和常见用法开始。这有助于我们支持和使用常用的词汇,并为我们提供了只能通过标准流程和其他团队努力来实现的即时广泛的选项和功能。它也打开了未来的门,与您的组织内部和合作伙伴或其他组织中围绕相同标准构建的其他系统进行灵活通信。
系统附带的应用程序和应用程序组件为您提供了一个广泛而灵活的基础,可以与最佳实践基础设计一起使用,或根据您自己的特殊需求进行定制。应用程序便于从各方和产品到会计,客户服务以及内部资源和资产管理的一切管理。
参考:http : //ofbiz.apache.org/apache-ofbiz-project-overview.html
1.2 设置和运行OFBiz
1.2.1 下载Apache OFBiz框架
如果您还没有在您的机器上签出Apache OFBiz Framework,那么我们来做。任何人都可以在OFBiz公共Subversion(SVN)存储库中检出或浏览源代码。如果您没有Subversion,要安装它,您可以去 这里 获取说明。
要检查源代码,只需使用以下命令(如果您正在使用GUI客户端,请适当配置):
· 发布 :$ svnco http://svn.apache.org/repos/asf/ofbiz/branches/release15.12 ofbiz.15.12
有关更多详细信息,请参阅 Apache OFBiz Source Repository 页面。
1.2.2 运行Apache OFBiz
· 使用命令行,构建和启动OFBiz(使用演示数据),使用命令:
$ ./ant load-demo start (For linux/mac it will work, for Windows $OFBIZ_HOME/ant.bat come bundled with OFBiz for Windows) |
以上命令将加载演示数据(样品数据运行应用程序)自带的OFBiz在 Derby 数据库,它配置了OFBiz。
· 将您的浏览器指向 http:// localhost:8080 / webtools, 并使用用户名“admin”和密码“ofbiz”登录,并查看一下。
就是这样,Apache OFBiz现在在你的系统上运行!
1.3 创建您的第一个应用程序(HelloWorld ...)
1.3.1 组件简介
OFBiz组件是一个文件夹,包含一个名为“ofbiz-component.xml”的特殊xml文件,它描述了组件要加载和要求的资源。
OFBiz本身是一组组件。
· 框架组件:这些是向应用程序组件提供技术层和工具的较低级别组件; 这些组件提供的功能通常是任何其他开发框架(数据层,业务逻辑层,事务处理,数据源池等)提供的功能。
· 应用程序组件:这些是可扩展/定制(产品,订单,派对,制造,会计等)的ERP应用程序所需的通用业务组件; 应用程序组件可以访问由框架组件提供的服务和工具以及由其他应用程序组件发布的服务。
· 专用组件:这些组件与应用程序组件类似,但适用于电子商务,谷歌基础集成,eBay集成等专用应用程序。
· 热部署组件:这是一个可用于自定义开发的空组件。自定义组件可以扩展/覆盖,所有其他组件发布的资源。
1.3.2 创建组件
在Hot-deploy目录中的OFBiz中设置新的自定义组件非常简单。使用命令行,您只需运行以下命令并回答有关命名您的自定义组件,组件资源,webapp和base权限的问题,如下所示:
$ ./ant create-component Buildfile: /home/ofbiz_dev/Apache_OFBiz/ofbiz-15.12/build.xml create-component: [input] Component name: (e.g. mycomponent) [Mandatory] ofbizdemo [input] Component resource name: (e.g. MyComponent) [Mandatory] OfbizDemo [input] Webapp name: (e.g. mycomponent) [Mandatory] ofbizdemo [input] Base permission: (e.g. MYCOMPONENT) [Mandatory] OFBIZDEMO [echo] The following hot-deploy component will be created: [echo] Name: ofbizdemo [echo] Resource Name: OfbizDemo [echo] Webapp Name: ofbizdemo [echo] Base permission: OFBIZDEMO [echo] Folder: /home/ofbiz_dev/Apache_OFBiz/ofbiz-15.12/hot-deploy/ofbizdemo [echo] [input] Confirm: (Y, [N], y, n) y |
1.3.3 运行你的第一个应用程序
在运行我们的第一个组件之前,让我们说“Hello to the World”
1. 只需从ofbizdemo组件(刚才创建)中打开$ OFBIZ_HOME /hot-deploy / ofbizdemo / widget / OfbizDemoScreens.xml文件,
<?xmlversion="1.0"encoding="UTF-8"?> <screens xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="<a href="http://ofbiz.apache.org/dtds/widget-screen.xsd" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://ofbiz.apache.org/dtds/widget-screen.xsd"> <screen name="main"> <section> <actions> <set field="headerItem" value="main"/><!-- this highlights the selected menu-item with name "main" --> </actions> <widgets> <decorator-screen name="OfbizDemoCommonDecorator" location="${parameters.mainDecoratorLocation}"> <decorator-section name="body"> <label text="Hello World!! :)"/> </decorator-section> </decorator-screen> </widgets> </section> </screen> </screens> |
我们只添加了 <label text =“Hello World !! :)”/>
2. 现在,您将需要重新启动OFBiz($ ./ant load-demo start)。这是必需的,因为您已经为组件创建了一些新的组件(默认情况下,您的组件数据目录中为OfbizDemoSecurityGroupDemoData.xml),并且您将重新启动它,还将加载ofbizdemo组件。
3. 由于OFBiz重新启动,直接将您的浏览器转到您的应用程序 http:// localhost:8080 / ofbizdemo
4. 您将被要求登录。用户登录:admin密码:ofbiz。
5. 当您登录时,您将看到bizdemo应用程序与您在屏幕中显示的hello world消息,如下图所示。
就是这样,恭喜你的第一个组件是安装和运行的。
1.3.4 创建第一个数据库实体(表)
1.3.4.1 定义实体
要在数据库中创建自定义实体/表,您只需要在 yourbizdemo应用程序的$ OFBIZ_HOME / hot-deploy / ofbizdemo / entitydef /entitymodel.xml文件中提供实体定义 。当您使用ant目标设置组件时,此文件结构已被设置。您只需要进入并提供实体定义,如下所示。在这里,我们将为forbizdemo应用程序添加两个新实体。
<?xml version="1.0" encoding="UTF-8"?> <entitymodel xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="<a href="http://ofbiz.apache.org/dtds/entitymodel.xsd" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://ofbiz.apache.org/dtds/entitymodel.xsd"> <title>Entity of an Open For Business Project Component</title> <description>None</description> <version>1.0</version> <entity entity-name="OfbizDemoType" package-name="org.ofbiz.ofbizdemo" title="OfbizDemo Type Entity"> <field name="ofbizDemoTypeId" type="id-ne"><description>primary sequenced ID</description></field> <field name="description" type="description"></field> <prim-key field="ofbizDemoTypeId"/> </entity> <entity entity-name="OfbizDemo" package-name="org.ofbiz.ofbizdemo" title="OfbizDemo Entity"> <field name="ofbizDemoId" type="id-ne"><description>primary sequenced ID</description></field> <field name="ofbizDemoTypeId" type="id-ne"></field> <field name="firstName" type="name"></field> <field name="lastName" type="name"></field> <field name="comments" type="comment"></field> <prim-key field="ofbizDemoId"/> <relation type="one" fk-name="ODEM_OD_TYPE_ID" rel-entity-name="OfbizDemoType"> <key-map field-name="ofbizDemoTypeId"/> </relation> </entity> </entitymodel> |
现在看看$OFBIZ_HOME / hot-deploy / ofbizdemo / ofbiz-component.xml文件。您已经拥有资源条目,用于在组件加载时将这些实体从定义加载到数据库。如下所示:
<entity-resource type="model" reader-name="main" loader="main" location="entitydef/entitymodel.xml"/> |
要检查简单地重新启动OFBiz(Ctrl + C后跟“./ant start”),并将浏览器引导到实体数据维护工具,请执行以下操作: https:// localhost:8443 / webtools / control / entitymaint, 并搜索实体OfbizDemoType和OfbizDemo。你会看到它如下图给出的图像。
为实体准备数据
当您设置自定义实体时,现在是准备一些示例数据的时候了。您可以在$ OFBIZ_HOME / hot-deploy/ ofbizdemo / data / OfbizDemoTypeData.xml 和$ OFBIZ_HOME /hot-deploy / ofbizdemo / data / OfbizDemoDemoData.xml中的数据目录下的数据XML文件中执行此操作。设置如下所示:
OfbizDemoTypeData.xml
<?xml version="1.0" encoding="UTF-8"?> <entity-engine-xml> <OfbizDemoType ofbizDemoTypeId="INTERNAL" description="Internal Demo - Office"/> <OfbizDemoType ofbizDemoTypeId="EXTERNAL" description="External Demo - On Site"/> </entity-engine-xml> |
OfbizDemoDemoData.xml
<?xml version="1.0" encoding="UTF-8"?> <entity-engine-xml> <OfbizDemo ofbizDemoId="SAMPLE_DEMO_1" ofbizDemoTypeId="INTERNAL" firstName="Sample First 1" lastName="Sample Last 1" comments="This is test comment for first record."/> <OfbizDemo ofbizDemoId="SAMPLE_DEMO_2" ofbizDemoTypeId="INTERNAL" firstName="Sample First 2" lastName="Sample last 2" comments="This is test comment for second record."/> <OfbizDemo ofbizDemoId="SAMPLE_DEMO_3" ofbizDemoTypeId="EXTERNAL" firstName="Sample First 3" lastName="Sample last 3" comments="This is test comment for third record."/> <OfbizDemo ofbizDemoId="SAMPLE_DEMO_4" ofbizDemoTypeId="EXTERNAL" firstName="Sample First 4" lastName="Sample last 4" comments="This is test comment for fourth record."/> </entity-engine-xml> |
现在再来看看 $ OFBIZ_HOME / hot-deploy / ofbizdemo /ofbiz-component.xml 文件。您已经有资源条目用于将这些文件中准备的数据加载为:
inbiz-component.xml中的条目
<entity-resource type="data" reader-name="seed" loader="main" location="data/OfbizDemoTypeData.xml"/> <entity-resource type="data" reader-name="demo" loader="main" location="data/OfbizDemoDemoData.xml"/> |
1.3.4.2 在实体中加载数据
此时将此样本数据加载到定义的实体/表中,您可以在控制台上运行“ant load-demo”,也可以直接在webtools中加载实体xml https:// localhost:8443 / webtools / control /EntityImport。
简单地将你的xml数据放在“完整的XML文档(根标签:entity-engine-xml)”中:“文本区域并点击”导入文本“,如下图所示
当您点击导入文本时,它将加载数据,并显示如下所示的结果
完成数据加载过程后,再次访问实体数据维护(https:// localhost:8443 / webtools / control / entitymaint)并检查您的实体,您将在这里找到刚刚加载的数据。
就是这样,你已经成功地将数据导入数据库表,超级容易,对!
1.4 表格和服务
在上一节中,我们已经看到了如何创建实体(表),现在是创建一个表单的时候了,该表单将允许您在该实体中创建条目。
1.4.1 创建服务
在准备表单之前,让我们 在服务定义xml文件($ OFBIZ_HOME /hot-deploy / ofbizdemo / servicedef / services.xml)中为OfbizDemo实体编写一个 服务来创建记录。
的services.xml
<?xml version="1.0" encoding="UTF-8"?> <services xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="<a href="http://ofbiz.apache.org/dtds/services.xsd" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://ofbiz.apache.org/dtds/services.xsd"> <description>OfbizDemo Services</description> <vendor></vendor> <version>1.0</version> <service name="createOfbizDemo" default-entity-name="OfbizDemo" engine="entity-auto" invoke="create" auth="true"> <description>Create an Ofbiz Demo record</description> <auto-attributes include="pk" mode="OUT" optional="false"/> <auto-attributes include="nonpk" mode="IN" optional="false"/> <override name="comments" optional="true"/> </service> </services> |
现在再来看看$OFBIZ_HOME / hot-deploy / ofbizdemo / ofbiz-component.xml文件。您已经有资源条目,用于将此文件中定义的服务加载为:
<!-- service resources: model(s), eca(s) and group definitions --> <service-resource type="model" loader="main" location="servicedef/services.xml"/> |
对于要加载的服务定义,您将需要重新启动OFBiz。要测试此服务,您直接转到webtools - >运行服务选项: https:// localhost:8443 / webtools / control / runService
通过Web工具运行服务:这是由框架提供的智能实用程序来运行您的服务。
在提交上述表格后,您将提交一份表格,以输入服务的IN参数。
1.4.2 创建添加表单
让我们为这个服务创建我们的第一个表单,让我们编辑位于$ OFBIZ_HOME / hot-deploy / ofbizdemo / widget /OfbizDemoForms.xml的现有文件, 并为OfbizDemo添加Create Form,如下所示:
OfbizDemoForms.xml
<?xml version="1.0" encoding="UTF-8"?> <forms xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="<a href="http://ofbiz.apache.org/dtds/widget-form.xsd" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://ofbiz.apache.org/dtds/widget-form.xsd"> <form name="AddOfbizDemo" type="single" target="createOfbizDemo"> <!-- We have this utility in OFBiz to render form based on service definition. Service attributes will automatically lookedup and will be shown on form --> <auto-fields-service service-name="createOfbizDemo"/> <field name="ofbizDemoTypeId" title="${uiLabelMap.CommonType}"> <drop-down allow-empty="false" current-description=""> <!---We have made this drop down options dynamic(Values from db) using this --> <entity-options description="${description}" key-field-name="ofbizDemoTypeId" entity-name="OfbizDemoType"> <entity-order-by field-name="description"/> </entity-options> </drop-down> </field> <field name="submitButton" title="${uiLabelMap.CommonAdd}"><submit button-type="button"/></field> </form> </forms> |
在这里,您可以注意到我们已经使用auto-fields-service来根据服务定义IN / OUT属性自动生成表单。
转到屏幕xml文件(OfbizDemoScreens.xml)将此表单位置添加到您用于显示Hello World...文本的屏幕中。如下所示
将表单位置添加到主屏幕
<?xml version="1.0" encoding="UTF-8"?> <screens xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="<a href="http://ofbiz.apache.org/dtds/widget-screen.xsd" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://ofbiz.apache.org/dtds/widget-screen.xsd"> <screen name="main"> <section> <actions> <set field="headerItem" value="main"/> <!-- this highlights the selected menu-item with name "main" --> </actions> <widgets> <decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}"> <decorator-section name="body"> <screenlet title="Add Ofbiz Demo"> <include-form name="AddOfbizDemo" location="component://ofbizdemo/widget/OfbizDemoForms.xml"/> </screenlet> </decorator-section> </decorator-screen> </widgets> </section> </screen> </screens> |
1.4.3 控制器输入表单
在您进入表单并从添加表单开始创建OfbizDemo记录之前,您将需要在$ OFBIZ_HOME / hot-deploy / ofbizdemo / webapp / ofbizdemo/ WEB-INF / controller.xml 文件中为目标服务创建一个条目, 该目录服务将在提交表单时调用。您可以按照下面所示的请求映射在您的bizdemo应用程序控制器文件中进行操作:
<request-map uri="createOfbizDemo"> <security https="true" auth="true"/> <event type="service" invoke="createOfbizDemo"/> <response name="success" type="view" value="main"/> </request-map> |
一切设置,我们来看看最近创建的表单 http:// localhost:8080 / ofbizdemo
主键(ofbizDemoId)不需要使用表单发送,它将由OFBiz在db记录中自动排序。
1.4.4
创建查找表单
让我们为实体OfbizDemo创建一个查找表单,以便您搜索所创建的OfbizDemos。
1.在OfbizDemoForms.xml中添加表单(FindOfbizDemo和ListOfbizDemo)
OfbizDemoForms.xml
<form name="FindOfbizDemo" type="single" target="FindOfbizDemo" default-entity-name="OfbizDemo"> <field name="noConditionFind"><hidden value="Y"/> <!-- if this isn't there then with all fields empty no query will be done --></field> <field name="ofbizDemoId" title="${uiLabelMap.OfbizDemoId}"><text-find/></field> <field name="firstName" title="${uiLabelMap.OfbizDemoFirstName}"><text-find/></field> <field name="lastName" title="${uiLabelMap.OfbizDemoLastName}"><text-find/></field> <field name="ofbizDemoTypeId" title="${uiLabelMap.OfbizDemoType}"> <drop-down allow-empty="true" current-description=""> <entity-options description="${description}" key-field-name="ofbizDemoTypeId" entity-name="OfbizDemoType"> <entity-order-by field-name="description"/> </entity-options> </drop-down> </field> <field name="searchButton" title="${uiLabelMap.CommonFind}" widget-style="smallSubmit"><submit button-type="button" image-location="/images/icons/magnifier.png"/></field> </form> <form name="ListOfbizDemo" type="list" list-name="listIt" paginate-target="FindOfbizDemo" default-entity-name="OfbizDemo" separate-columns="true" odd-row-style="alternate-row" header-row-style="header-row-2" default-table-style="basic-table hover-bar"> <actions> <!-- Preparing search results for user query by using OFBiz stock service to perform find operations on a single entity or view entity --> <service service-name="performFind" result-map="result" result-map-list="listIt"> <field-map field-name="inputFields" from-field="ofbizDemoCtx"/> <field-map field-name="entityName" value="OfbizDemo"/> <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="ofbizDemoId" title="${uiLabelMap.OfbizDemoId}"><display/></field> <field name="ofbizDemoTypeId" title="${uiLabelMap.OfbizDemoType}"><display-entity entity-name="OfbizDemoType"/></field> <field name="firstName" title="${uiLabelMap.OfbizDemoFirstName}" sort-field="true"><display/></field> <field name="lastName" title="${uiLabelMap.OfbizDemoLastName}" sort-field="true"><display/></field> <field name="comments" title="${uiLabelMap.OfbizDemoComment}"><display/></field> </form> |
表单或屏幕的操作标签用于您的视图的数据准备逻辑。
我们已经使用OOTB OFBiz通用服务performFind进行搜索操作,当您必须在一个实体或一个视图实体上执行搜索时,该操作便于使用。
2.)在下一步中,我们将在屏幕中包含这些表单,我们将这些表单添加到 OfbizDemoScreens.xml 文件中。对于这种包括 FindOfbizDemo 在下面定义屏幕 OfbizDemoScreens.xml
<!-- Find and list all ofbizdemos in a tabular format --> <screen name="FindOfbizDemo"> <section> <actions> <set field="headerItem" value="findOfbizDemo"/> <set field="titleProperty" value="PageTitleFindOfbizDemo"/> <set field="ofbizDemoCtx" from-field="parameters"/> </actions> <widgets> <decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}"> <decorator-section name="body"> <section> <condition> <if-has-permission permission="OFBIZDEMO" action="_VIEW"/> </condition> <widgets> <decorator-screen name="FindScreenDecorator" location="component://common/widget/CommonScreens.xml"> <decorator-section name="search-options"> <include-form name="FindOfbizDemo" location="component://ofbizdemo/widget/OfbizDemoForms.xml"/> </decorator-section> <decorator-section name="search-results"> <include-form name="ListOfbizDemo" location="component://ofbizdemo/widget/OfbizDemoForms.xml"/> </decorator-section> </decorator-screen> </widgets> <fail-widgets> <label style="h3">${uiLabelMap.OfbizDemoViewPermissionError}</label> </fail-widgets> </section> </decorator-section> </decorator-screen> </widgets> </section> </screen> |
3.)在controller.xml中添加访问此新的Find Ofbiz演示页面的请求映射
<!-- Request Mapping --> <request-map uri="FindOfbizDemo"><security https="true" auth="true"/><response name="success" type="view" value="FindOfbizDemo"/></request-map> <!-- View Mapping --> <view-map name="FindOfbizDemo" type="screen" page="component://ofbizdemo/widget/OfbizDemoScreens.xml#FindOfbizDemo"/> |
现在,我们来添加一个显示查找选项的菜单。
在OFBiz中创建菜单非常简单,所有菜单都定义为* menus.xml。
当我们从ant目标创建一个组件时,我们得到一个名为OfbizDemoMenus.xml的文件
在OfbizDemoMenus.xml文件中创建以下条目。
OfbizDemoMenus.xml
<?xml version="1.0" encoding="UTF-8"?> <menus xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="<a href="http://ofbiz.apache.org/dtds/widget-menu.xsd" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://ofbiz.apache.org/dtds/widget-menu.xsd"> <menu name="MainAppBar" title="${uiLabelMap.OfbizDemoApplication}" extends="CommonAppBarMenu" extends-resource="component://common/widget/CommonMenus.xml"> <menu-item name="main" title="${uiLabelMap.CommonMain}"><link target="main"/></menu-item> <menu-item name="findOfbizDemo" title="${uiLabelMap.OfbizDemoFind}"><link target="FindOfbizDemo"/></menu-item> </menu> </menus> |
1.4.5 使用UI标签
Apache OFBiz的国际化真的很容易,我们定义了各种语言的UI标签,并且基于用户的区域设置,显示了相应的标签。
以下是UI标签的示例(在创建组件<component-name>时,默认情况下会创建UiLabels.xml,在本例中为 OfbizDemoUiLabels.xml)
OfbizDemoUiLabels.xml
<property key="OfbizDemoFind"> <value xml:lang="en">Find</value> </property> <property key="OfbizDemoFirstName"> <value xml:lang="en">First Name</value> </property> <property key="OfbizDemoId"> <value xml:lang="en">OFBiz Demo Id</value> </property> <property key="OfbizDemoLastName"> <value xml:lang="en">Last Name</value> </property> |
现在只需重新启动服务器,根据ofbizdemo应用程序(https:// localhost:8443 / ofbizdemo /control / main),您将看到“查找”菜单选项。
1.5 使用其他引擎的服务
只要您必须构建业务逻辑,您应该更愿意编写服务来利用内置的服务引擎中的功能。
您之前创建的服务“createOfbizDemo”正在使用engine =“entity-auto”,因此您不需要提供其实现,OFBiz负责创建操作。当您需要处理涉及从数据库和定制逻辑构建的多个实体的服务中的复杂操作时,您需要为您的服务提供自定义实现。在本节中,我们将重点介绍这一点。
1.5.1 Java中的服务
您可以在Java中实现一个服务,如下面给出的步骤所示:
1.)定义您的服务,这里再次,我们将在我们的定制Ofbiz演示应用程序的同一个实体(OfbizDemo)上运行。打开您的服务定义文件$OFBIZ_HOME / hot-deploy / ofbizdemo / servicedef / services.xml并添加新定义:
的services.xml
<service name="createOfbizDemoByJavaService" default-entity-name="OfbizDemo" engine="java" location="com.companyname.ofbizdemo.services.OfbizDemoServices" invoke="createOfbizDemo" auth="true"> <description>Create an Ofbiz Demo record using a service in Java</description> <auto-attributes include="pk" mode="OUT" optional="false"/> <auto-attributes include="nonpk" mode="IN" optional="false"/> <override name="comments" optional="true"/> </service> |
注意我们有这个时候使用engine =“java”。
2.)在您的ofbizdemo组件src目录中创建包,例如src - > com - > companyname - >ofbizdemo - > services。必须在Java中实现的服务可以放在您的应用程序的此目录中。
3.)在服务目录和实现方法中,在文件OfbizDemoServices.java中定义新的Java类,这将由您的服务定义调用,如下所示:
OfbizDemoServices.java
package com.companyname.ofbizdemo.services; import java.util.Map; import org.ofbiz.base.util.Debug; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.service.DispatchContext; import org.ofbiz.service.ServiceUtil; public class OfbizDemoServices { public static final String module = OfbizDemoServices.class.getName(); public static Map<String, Object> createOfbizDemo(DispatchContext dctx, Map<String, ? extends Object> context) { Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); try { GenericValue ofbizDemo = delegator.makeValue("OfbizDemo"); // Auto generating next sequence of ofbizDemoId primary key ofbizDemo.setNextSeqId(); // Setting up all non primary key field values from context map ofbizDemo.setNonPKFields(context); // Creating record in database for OfbizDemo entity for prepared value ofbizDemo = delegator.create(ofbizDemo); result.put("ofbizDemoId", ofbizDemo.getString("ofbizDemoId")); Debug.log("==========This is my first Java Service implementation in Apache OFBiz. OfbizDemo record created successfully with ofbizDemoId: "+ofbizDemo.getString("ofbizDemoId")); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError("Error in creating record in OfbizDemo entity ........" +module); } return result; } } |
4.)停止服务器并使用“./antclean build start”重新启动,它将编译您的类,并在whenbiz重新启动哪个更新的jar文件时使其可用。
5.)使用webtools ->运行服务选项(https:// localhost:8443 / webtools / control / runService)实现测试服务,或者简单地更新您的控制器请求调用的服务名称,以使用此服务,并使用添加表单在你以前准备的应用程序中 通过这样做,你的Add OfbizDemo表单将调用这个java服务。
<request-map uri="createOfbizDemo"> <security https="true" auth="true"/> <event type="service" invoke="createOfbizDemoByJavaService"/> <response name="success" type="view" value="main"/> </request-map> |
为了确保这个新的服务实现正在执行,您可以使用Debug.log(....)在控制台日志中查看已经放在代码中的这一行。对于登录OFBiz,您必须始终在Java类中使用Debug类方法。
控制台日志
[java] 2014-06-24 12:11:37,282 (http-bio-0.0.0.0-8443-exec-2) [ OfbizDemoServices.java:28 :INFO ] ==========This is my first Java Service implementation in Apache OFBiz. OfbizDemo record created successfully with ofbizDemoId: ...... |
1.5.2 服务在Groovy
为了利用即时编译的功能和更少的代码行,您可以使用Groovy DSL实现在OFBiz中构建业务逻辑的服务。
要使用Groovy实现服务,您可以按照以下步骤:
1.)将新的服务定义添加到services /services.xml文件中:
的services.xml
<service name="createOfbizDemoByGroovyService" default-entity-name="OfbizDemo" engine="groovy" location="component://ofbizdemo/script/com/companyname/ofbizdemo/OfbizDemoServices.groovy" invoke="createOfbizDemo" auth="true"> <description>Create an Ofbiz Demo record using a service in Java</description> <auto-attributes include="pk" mode="OUT" optional="false"/> <auto-attributes include="nonpk" mode="IN" optional="false"/> <override name="comments" optional="true"/> </service> |
2.)在这里添加新的groovy服务文件 组件://ofbizdemo/script/com/companyname/ofbizdemo/OfbizDemoServices.groovy
3.)将服务实现添加到文件OfbizDemoServices.groovy中
OfbizDemoServices.groovy
import org.ofbiz.entity.GenericEntityException; def createOfbizDemo() { result = [:]; try { ofbizDemo = delegator.makeValue("OfbizDemo"); // Auto generating next sequence of ofbizDemoId primary key ofbizDemo.setNextSeqId(); // Setting up all non primary key field values from context map ofbizDemo.setNonPKFields(context); // Creating record in database for OfbizDemo entity for prepared value ofbizDemo = delegator.create(ofbizDemo); result.ofbizDemoId = ofbizDemo.ofbizDemoId; logInfo("==========This is my first Groovy Service implementation in Apache OFBiz. OfbizDemo record " +"created successfully with ofbizDemoId: "+ofbizDemo.getString("ofbizDemoId")); } catch (GenericEntityException e) { logError(e.getMessage()); return error("Error in creating record in OfbizDemo entity ........"); } return result; } |
4.)停止服务器并重新开始使用./ant start,这次我们只需要加载新的服务定义,而不需要显式编译作为其在Groovy中的服务实现。
5.)使用webtools ->运行服务选项(https:// localhost:8443 / webtools / control / runService)实现测试服务,或者简单地更新您的控制器请求调用的服务名称,以使用此服务,并使用添加表单在您的应用程序,您之前准备测试。通过这样做,您的AddOfbizDemo表单将称之为groovy服务。
controller.xml
<request-map uri="createOfbizDemo"> <security https="true" auth="true"/> <event type="service" invoke="createOfbizDemoByGroovyService"/> <response name="success" type="view" value="main"/> </request-map> |
为了确保这个新的服务实现正在执行,您可以使用Debug.log(....)在控制台日志中查看已经放在代码中的这一行。对于登录OFBiz,您必须始终在Java类中使用Debug类方法。
[java] 2014-06-24 12:11:37,282 (http-bio-0.0.0.0-8443-exec-2) [ OfbizDemoServices.java:28 :INFO ] ==========This is my first Groovy Service implementation in Apache OFBiz. OfbizDemo record created successfully with ofbizDemoId: ..... |
要获得更多关于使用Groovy DSL在Apache OFBiz中进行服务和事件实现的细节,您可以在这里将 OFBiz Wiki中的Jacopo Cappellato创建的文档引用到 这里。
1.6 活动
1.6.1 活动示范
Apache OFBiz中的事件只是使用HttpServletRequest和HttpServletResponse对象使用的方法。您不需要像服务一样提供这些定义。这些从控制器直接调用。当您要将自定义服务器端验证添加到输入参数时,事件也很有用。对于执行数据库操作,您仍然可以从事件中调用预构建的服务。
要在OFBiz中写一个事件,请按照下列步骤操作:
1.)添加一个新的事件目录到包和一个新的Events类文件,如下所述:
OfbizDemoEvents.java
package com.companyname.ofbizdemo.events; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericValue; import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.LocalDispatcher; public class OfbizDemoEvents { public static final String module = OfbizDemoEvents.class.getName(); public static String createOfbizDemoEvent(HttpServletRequest request, HttpServletResponse response) { Delegator delegator = (Delegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); String ofbizDemoTypeId = request.getParameter("ofbizDemoTypeId"); String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); if (UtilValidate.isEmpty(firstName) || UtilValidate.isEmpty(lastName)) { String errMsg = "First Name and Last Name are required fields on the form and can't be empty."; request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } String comments = request.getParameter("comments"); try { Debug.logInfo("=======Creating OfbizDemo record in event using service createOfbizDemoByGroovyService=========", module); dispatcher.runSync("createOfbizDemoByGroovyService", UtilMisc.toMap("ofbizDemoTypeId", ofbizDemoTypeId, "firstName", firstName, "lastName", lastName, "comments", comments, "userLogin", userLogin)); } catch (GenericServiceException e) { String errMsg = "Unable to create new records in OfbizDemo entity: " + e.toString(); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } request.setAttribute("_EVENT_MESSAGE_", "OFBiz Demo created succesfully."); return "success"; } } |
2.)将调用此事件的控制器请求添加为:
controller.xml
<request-map uri="createOfbizDemoEvent"> <security https="true" auth="true"/> <event type="java" path="com.companyname.ofbizdemo.events.OfbizDemoEvents" invoke="createOfbizDemoEvent"/> <response name="success" type="view" value="main"/> <response name="error" type="view" value="main"/> </request-map> |
3.)通过重建它来停止并启动服务器,因为我们需要编译我们在#1中添加的Java事件类。
4.)现在要测试这个事件,你可以简单地把AddOfbizDemo表单的目标变成“createOfbizDemoEvent”,并且它现在就提交你的事件。
1.6.2 服务与事件之间的区别
以下是服务和事件之间的区别,
· 事件用于使用地图处理器的验证和转换,而服务则用于诸如CRUD操作之类的业务逻辑。
· 服务返回地图。
· 事件返回String。
· 服务加载服务器,任何定义更改(如果在MiniLang中,则不实现)需要重新加载。
· 我们可以在事件中呼叫服务。但是我们不能在服务中调用事件。
· 一个事件是通常在一个地方用于一个目的的特定的本地零件功能,并从其位置调用。
· 一个服务是一个可以在网络上任何位置的功能,大部分时间都在几个不同的地方使用,它被称为“名字”。
· 在事件的情况下,您可以访问HttpServletRequest和HttpServletResponse对象,您可以读/写任何您想要的。在服务的情况下,您只能访问服务参数。
参考: https ://cwiki.apache.org/confluence/display/OFBIZ/FAQ+-+Tips+-+Tricks+-+Cookbook+-+HowTo#FAQ-Tips-Tricks-Cookbook-HowTo-DifferenceBetweenEventAndService 和 http:// ofbiz。 135035.n4.nabble.com
标准 |
服务 |
活动 |
需要定义 |
是 |
没有 |
实施可能性 |
实体自动,Java,简单(XML)和Groovy |
简单(XML),Java&Groovy |
返回类型 |
地图 |
串 |
用于写业务逻辑 |
是 |
没有 |
工作计划可能 |
是 |
没有 |
1.7 自定义用户界面
1.7.1 使用FreeMarker模板和Groovy脚本
好吧,我们在OFBiz教程的最后一部分。在这部分中,我们将专注于为企业管理应用程序(即后端应用程序)定制ApacheOFBiz的UI层。这里重要的是根据用户想要开发应用程序。
因此,要首先自定义应用程序的UI部分,以便轻松使用Freemarker模板,而不是内置的窗体小部件。首先,我们将看到如何使用Freestyle和Groovy脚本与Apache OFBiz,然后我们将看到如何通过定义自己的装饰器来放置自定义样式。最初我们将使用OFBiz默认装饰器。
从这里开始遵循以下步骤:
1.)在位于$ OFBIZ_HOME / hot-deploy / ofbizdemo / webapp/ ofbizdemo / crud / AddOfbizDemo.ftl和ListOfbizDemo.ftl的位置添加两个Freemarker文件,如下所示:
AddOfbizDemo.ftl
<div class="screenlet-body"> <form id="createOfbizDemoEvent" method="post" action="<@ofbizUrl>createOfbizDemoEvent</@ofbizUrl>"> <input type="hidden" name="addOfbizDemoFromFtl" value="Y"/> <fieldset> <div> <span class="label">${uiLabelMap.OfbizDemoType}</span> <select name="ofbizDemoTypeId" class='required'> <#list ofbizDemoTypes as demoType> <option value='${demoType.ofbizDemoTypeId}'>${demoType.description}</option> </#list> </select>* </div> <div> <span class="label">${uiLabelMap.OfbizDemoFirstName}</span> <input type="text" name="firstName" id="firstName" class='required' maxlength="20" />* </div> <div> <span class="label">${uiLabelMap.OfbizDemoLastName}</span> <input type="text" name="lastName" id="lastName" class='required' maxlength="20" />* </div> <div> <span class="label">${uiLabelMap.OfbizDemoComment}</span> <input type="text" name="comments" id="comments" class='inputBox' size="60" maxlength="255" /> </div> </fieldset> <input type="submit" value="${uiLabelMap.CommonAdd}" /> </form> </div> |
ListOfbizDemo.ftl
<div class="screenlet-body"> <#if ofbizDemoList?has_content> <table cellspacing=0 cellpadding=2 border=0 class="basic-table"> <thead><tr> <th>${uiLabelMap.OfbizDemoId}</th> <th>${uiLabelMap.OfbizDemoType}</th> <th>${uiLabelMap.OfbizDemoFirstName}</th> <th>${uiLabelMap.OfbizDemoLastName}</th> <th>${uiLabelMap.OfbizDemoComment}</th> </tr></thead> <tbody> <#list ofbizDemoList as ofbizDemo> <tr> <td>${ofbizDemo.ofbizDemoId}</td> <td>${ofbizDemo.getRelatedOne("OfbizDemoType").get("description", locale)}</td> <td>${ofbizDemo.firstName?default("NA")}</td> <td>${ofbizDemo.lastName?default("NA")}</td> <td>${ofbizDemo.comments!}</td> </tr> </#list> </tbody> </table> </#if> </div> |
2.)在位置$ OFBIZ_HOME / hot-deploy / ofbizdemo / webapp/ ofbizdemo / WEB-INF / actions / crud / ListOfbizDemo.groovy添加新的Groovy文件,以获取数据获取逻辑,并添加代码以列出OfbizDemo记录:
ofbizDemoTypes = delegator.findList("OfbizDemoType", null, null, null, null, false); context.ofbizDemoTypes = ofbizDemoTypes; ofbizDemoList = delegator.findList("OfbizDemo", null, null, null, null, false); context.ofbizDemoList = ofbizDemoList; |
3.)将Ofbiz默认装饰器添加新的屏幕文件到OfbizDemoScreens.xml,新增的freemarker和groovy文件为:
OfbizDemoScreens.xml
4.)为OfbizDemo菜单添加新的控制器请求和新项目:
controller.xml
OfbizDemoMenus.xml
<menu-item name="addOfbizDemoFtl" title="${uiLabelMap.OfbizDemoAddFtl}"><link target="AddOfbizDemoFtl"/></menu-item> |
5.)添加应用程序使用的新UI标签。
6.)运行您的thebiz演示应用程序,并转到刚刚添加的新选项卡。你应该有:
1.7.2
1.7.3 创建自定义装饰器
将自己的用户界面放在Freemarker中可以让您自由地进行实验,进行CSS调整,并使用户想要的应用程序。在本节中,我们将看到我们如何做到这一点。
我们将通过为您的应用程序视图定义自定义装饰器来做到这一点。OFBiz中的装饰器只不过是一个屏幕,您可以通过将其包含在其他应用屏幕中来定义和重用它。您已经使用OFBiz附带的默认装饰器(main-decorator - >ApplicationDecorator)来执行此操作。到目前为止,只要观察你准备好的屏幕,你会发现,你正在使用这个主要装饰器,请参考OfbizDemoScreens.xml中的下面的一行。
OfbizDemoScreens.xml
<decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}"> |
mainDecoratorLocation在参数映射中可用,因为它在webapp的web.xml中定义
web.xml中
<context-param> <description>The location of the main-decorator screen to use for this webapp; referred to as a context variable in screen def XML files.</description> <param-name>mainDecoratorLocation</param-name> <param-value>component://ofbizdemo/widget/CommonScreens.xml</param-value> </context-param> |
现在是使用自定义样式定义自己的装饰器的时候了。
在下面的示例中,我们将使用Bootstrap来设计我们在本教程最后部分开发的示例Freemarker屏幕。按照下面的步骤建立自己的装饰器。
1.)下载Bootstrap v3.3.7目录,可以从这里下载 并解压缩。
2.)在$ OFBIZ_HOME / hot-deploy / ofbizdemo / webapp/ ofbizdemo /位置创建两个新目录,即“css”和“js”
3.)将bootstrap-3.3.7 / dist / css /bootstrap.min.css复制到$OFBIZ_HOME / hot-deploy / ofbizdemo / webapp / ofbizdemo / css
4.将bootstrap-3.3.7 / dist / js / bootstrap.min.js复制到$ OFBIZ_HOME / hot-deploy/ ofbizdemo / webapp / ofbizdemo / js。
5.)打开$ OFBIZ_HOME / hot-deploy / ofbizdemo / webapp/ ofbizdemo / WEB-INF / web.xml,并在最后的allowedPath中为css和js目录创建条目,如下所示:
web.xml中
<init-param> <param-name>allowedPaths</param-name> <param-value>/error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images:/includes/maincss.css:/css:/js</param-value> </init-param> |
6.)在位于$ OFBIZ_HOME / hot-deploy / ofbizdemo / webapp/ ofbizdemo /的位置添加名为“includes”的新目录,并在刚刚添加的名为PreBody.ftl和PostBody.ftl的新目录中创建两个新文件。我们将在装饰器屏幕中使用(包括)这两个文件来构建完整的HTML页面。
PreBody.ftl
<html> <head> <title>${layoutSettings.companyName}</title> <meta name="viewport" content="width=device-width, user-scalable=no"/> <#if webSiteFaviconContent?has_content> <link rel="shortcut icon" href=""> </#if> <#list styleSheets as styleSheet> <link rel="stylesheet" href="${StringUtil.wrapString(styleSheet)}" type="text/css"/> </#list> <#list javaScripts as javaScript> <script type="text/javascript" src="${StringUtil.wrapString(javaScript)}"/></script> </#list> </head> <body data-offset="125"> <h4 align="center"> ==================Page PreBody Starts From Decorator Screen========================= </h4> <div class="container menus" id="container"> <div class="row"> <div class="col-sm-12"> <ul id="page-title" class="breadcrumb"> <li> <a href="<@ofbizUrl>main</@ofbizUrl>">Main</a> </li> <li class="active"><span class="flipper-title">${StringUtil.wrapString(uiLabelMap[titleProperty])}</span></li> <li class="pull-right"> <a href="<@ofbizUrl>logout</@ofbizUrl>" title="${uiLabelMap.CommonLogout}">logout</i></a> </li> </ul> </div> </div> <div class="row"> <div class="col-lg-12 header-col"> <div id="main-content"> <h4 align="center"> ==================Page PreBody Ends From Decorator Screen=========================</h4> <h4 align="center"> ==================Page Body starts From Screen=========================</h4> |
PostBody.ftl
<#-- Close the tags opened in the PreBody section --> </div> </div> </div> </div> <h4 align="center"> ==================Page PostBody and Page body in general ends here from Decorator Screen=========================</h4> </body> </html> |
7.)打开组件的共享屏幕文件$ OFBIZ_HOME / hot-deploy / ofbizdemo / widget/ CommonScreens.xml,这是我们将定义我们的自定义装饰器的文件。
8.)更新屏幕名为“OfbizDemoCommonDecorator”(将作为您的应用程序的自定义装饰器),如下所示:
CommonScreens.xml
<screen name="OfbizDemoCommonDecorator"> <section> <actions> <property-map resource="OfbizDemoUiLabels" map-name="uiLabelMap" global="true"/> <property-map resource="CommonUiLabels" map-name="uiLabelMap" global="true"/> <!-- Including custom CSS Styles that you want to use in your application view. [] in field can be used to set the order of loading CSS files to load if there are multiple --> <set field="styleSheets[]" value="/ofbizdemo/css/bootstrap.min.css"/> <!-- Including custom JS that you want to use in your application view. [] in field can be used to set the order of loading of JS files to load if there are multiple --> <set field="javaScripts[+0]" value="/ofbizdemo/js/bootstrap.min.js" global="true"/> <set field="layoutSettings.companyName" from-field="uiLabelMap.OfbizDemoCompanyName" global="true"/> </actions> <widgets> <section> <condition> <if-has-permission permission="OFBIZDEMO" action="_VIEW"/> </condition> <widgets> <platform-specific><html><html-template location="component://ofbizdemo/webapp/ofbizdemo/includes/PreBody.ftl"/></html></platform-specific> <decorator-section-include name="pre-body"/> <decorator-section-include name="body"/> <platform-specific><html><html-template location="component://ofbizdemo/webapp/ofbizdemo/includes/PostBody.ftl"/></html></platform-specific> </widgets> <fail-widgets> <label style="h3">${uiLabelMap.OfbizDemoViewPermissionError}</label> </fail-widgets> </section> </widgets> </section> </screen> |
9.)在上一部分创建的Freemarker屏幕中使用此装饰器:
OfbizDemoScreens.xml
10.)更新您的FTL文件以遵循HTML网络标准,并将CSS应用于:
AddOfbizDemo.ftl
<form method="post" action="<@ofbizUrl>createOfbizDemoEventFtl</@ofbizUrl>" name="createOfbizDemoEvent" class="form-horizontal"> <div class="control-group"> <label class="control-label" for="ofbizDemoTypeId">${uiLabelMap.OfbizDemoType}</label> <div class="controls"> <select id="ofbizDemoTypeId" name="ofbizDemoTypeId"> <#list ofbizDemoTypes as demoType> <option value='${demoType.ofbizDemoTypeId}'>${demoType.description}</option> </#list> </select> </div> </div> <div class="control-group"> <label class="control-label" for="firstName">${uiLabelMap.OfbizDemoFirstName}</label> <div class="controls"> <input type="text" id="firstName" name="firstName" required> </div> </div> <div class="control-group"> <label class="control-label" for="lastName">${uiLabelMap.OfbizDemoLastName}</label> <div class="controls"> <input type="text" id="lastName" name="lastName" required> </div> </div> <div class="control-group"> <label class="control-label" for="comments">${uiLabelMap.OfbizDemoComment}</label> <div class="controls"> <input type="text" id="comments" name="comments"> </div> </div> <div class="control-group"> <div class="controls"> <button type="submit" class="btn">${uiLabelMap.CommonAdd}</button> </div> </div> </form> |
ListOfbizDemo.ftl
<table class="table table-bordered table-striped table-hover"> <thead> <tr> <th>${uiLabelMap.OfbizDemoId}</th> <th>${uiLabelMap.OfbizDemoType}</th> <th>${uiLabelMap.OfbizDemoFirstName}</th> <th>${uiLabelMap.OfbizDemoLastName}</th> <th>${uiLabelMap.OfbizDemoComment}</th> </tr> </thead> <tbody> <#list ofbizDemoList as ofbizDemo> <tr> <td>${ofbizDemo.ofbizDemoId}</td> <td>${ofbizDemo.getRelatedOne("OfbizDemoType").get("description", locale)}</td> <td>${ofbizDemo.firstName?default("NA")}</td> <td>${ofbizDemo.lastName?default("NA")}</td> <td>${ofbizDemo.comments!}</td> </tr> </#list> </tbody> </table> |
10.现在重新启动OFBiz,因为您已经在web.xml中对allowedPath进行了条目。当它重新加载命令 https:// localhost:8443 / ofbizdemo / control / AddOfbizDemoFtl时, 您应该看到使用自定义样式的页面,而不是使用默认的OFBiz主题。它应该是:
在这里,您可以按需要玩它。尝试更改标题或新增标题,添加页脚,进行验证等。因此,您可以使用Freemarker模板,CSS和JS自定义OFBiz的UI层。
您可能需要添加自己的CSS或JS文件,您可以按照与Bootstrap文件相同的方式来包含它们。