本教程是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
| packagecom.companyname.ofbizdemo.services; importjava.util.Map; importorg.ofbiz.base.util.Debug; importorg.ofbiz.entity.Delegator; importorg.ofbiz.entity.GenericEntityException; importorg.ofbiz.entity.GenericValue; importorg.ofbiz.service.DispatchContext; importorg.ofbiz.service.ServiceUtil; publicclassOfbizDemoServices { publicstaticfinalString module = OfbizDemoServices.class.getName(); publicstaticMap<String, Object> createOfbizDemo(DispatchContext dctx, Map<String, ? extendsObject> 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); returnServiceUtil.returnError("Error in creating record in OfbizDemo entity ........"+module); } returnresult; } } |
4.)停止服務器並使用“./antclean build start”重新啟動,它將編譯您的類,並在whenbiz重新啟動哪個更新的jar文件時使其可用。
5.)使用webtools ->運行服務選項(https:// localhost:8443 / webtools / control / runService)實現測試服務,或者簡單地更新您的控制器請求調用的服務名稱,以使用此服務,並使用添加表單在你以前准備的應用程序中 通過這樣做,你的Add OfbizDemo表單將調用這個java服務。
| <request-mapuri="createOfbizDemo"> <securityhttps="true"auth="true"/> <eventtype="service"invoke="createOfbizDemoByJavaService"/> <responsename="success"type="view"value="main"/> </request-map> |
為了確保這個新的服務實現正在執行,您可以使用Debug.log(....)在控制台日志中查看已經放在代碼中的這一行。對於登錄OFBiz,您必須始終在Java類中使用Debug類方法。
控制台日志
| [java] 2014-06-2412: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
| <servicename="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-attributesinclude="pk"mode="OUT"optional="false"/> <auto-attributesinclude="nonpk"mode="IN"optional="false"/> <overridename="comments"optional="true"/> </service> |
2.)在這里添加新的groovy服務文件 組件://ofbizdemo/script/com/companyname/ofbizdemo/OfbizDemoServices.groovy
3.)將服務實現添加到文件OfbizDemoServices.groovy中
OfbizDemoServices.groovy
import org.ofbiz.entity.GenericEntityException; defcreateOfbizDemo() { 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()); returnerror("Error in creating record in OfbizDemo entity ........"); } returnresult; } |
4.)停止服務器並重新開始使用./ant start,這次我們只需要加載新的服務定義,而不需要顯式編譯作為其在Groovy中的服務實現。
5.)使用webtools ->運行服務選項(https:// localhost:8443 / webtools / control / runService)實現測試服務,或者簡單地更新您的控制器請求調用的服務名稱,以使用此服務,並使用添加表單在您的應用程序,您之前准備測試。通過這樣做,您的AddOfbizDemo表單將稱之為groovy服務。
controller.xml
| <request-mapuri="createOfbizDemo"> <securityhttps="true"auth="true"/> <eventtype="service"invoke="createOfbizDemoByGroovyService"/> <responsename="success"type="view"value="main"/> </request-map> |
為了確保這個新的服務實現正在執行,您可以使用Debug.log(....)在控制台日志中查看已經放在代碼中的這一行。對於登錄OFBiz,您必須始終在Java類中使用Debug類方法。
| [java] 2014-06-2412: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
| packagecom.companyname.ofbizdemo.events; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.ofbiz.base.util.Debug; importorg.ofbiz.base.util.UtilMisc; importorg.ofbiz.base.util.UtilValidate; importorg.ofbiz.entity.Delegator; importorg.ofbiz.entity.GenericValue; importorg.ofbiz.service.GenericServiceException; importorg.ofbiz.service.LocalDispatcher; publicclassOfbizDemoEvents { publicstaticfinalString module = OfbizDemoEvents.class.getName(); publicstaticString 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-mapuri="createOfbizDemoEvent"> <securityhttps="true"auth="true"/> <eventtype="java"path="com.companyname.ofbizdemo.events.OfbizDemoEvents"invoke="createOfbizDemoEvent"/> <responsename="success"type="view"value="main"/> <responsename="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
| <divclass="screenlet-body"> <formid="createOfbizDemoEvent"method="post"action="<@ofbizUrl>createOfbizDemoEvent</@ofbizUrl>"> <inputtype="hidden"name="addOfbizDemoFromFtl"value="Y"/> <fieldset> <div> <spanclass="label">${uiLabelMap.OfbizDemoType}</span> <selectname="ofbizDemoTypeId"class='required'> <#list ofbizDemoTypes as demoType> <optionvalue='${demoType.ofbizDemoTypeId}'>${demoType.description}</option> </#list> </select>* </div> <div> <spanclass="label">${uiLabelMap.OfbizDemoFirstName}</span> <inputtype="text"name="firstName"id="firstName"class='required'maxlength="20"/>* </div> <div> <spanclass="label">${uiLabelMap.OfbizDemoLastName}</span> <inputtype="text"name="lastName"id="lastName"class='required'maxlength="20"/>* </div> <div> <spanclass="label">${uiLabelMap.OfbizDemoComment}</span> <inputtype="text"name="comments"id="comments"class='inputBox'size="60"maxlength="255"/> </div> </fieldset> <inputtype="submit"value="${uiLabelMap.CommonAdd}"/> </form> </div> |
ListOfbizDemo.ftl
| <div class="screenlet-body"> <#ifofbizDemoList?has_content> <table cellspacing=0cellpadding=2border=0class="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-itemname="addOfbizDemoFtl"title="${uiLabelMap.OfbizDemoAddFtl}"><linktarget="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-screenname="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> <metaname="viewport"content="width=device-width, user-scalable=no"/> <#if webSiteFaviconContent?has_content> <linkrel="shortcut icon"href=""> </#if> <#list styleSheets as styleSheet> <linkrel="stylesheet"href="${StringUtil.wrapString(styleSheet)}"type="text/css"/> </#list> <#list javaScripts as javaScript> <scripttype="text/javascript"src="${StringUtil.wrapString(javaScript)}"/></script> </#list> </head> <bodydata-offset="125"> <h4align="center"> ==================Page PreBody Starts From Decorator Screen========================= </h4> <divclass="container menus"id="container"> <divclass="row"> <divclass="col-sm-12"> <ulid="page-title"class="breadcrumb"> <li> <ahref="<@ofbizUrl>main</@ofbizUrl>">Main</a> </li> <liclass="active"><spanclass="flipper-title">${StringUtil.wrapString(uiLabelMap[titleProperty])}</span></li> <liclass="pull-right"> <ahref="<@ofbizUrl>logout</@ofbizUrl>" title="${uiLabelMap.CommonLogout}">logout</i></a> </li> </ul> </div> </div> <divclass="row"> <divclass="col-lg-12 header-col"> <divid="main-content"> <h4align="center"> ==================Page PreBody Ends From Decorator Screen=========================</h4> <h4align="center"> ==================Page Body starts From Screen=========================</h4> |
PostBody.ftl
| <#-- Close the tags opened in the PreBody section --> </div> </div> </div> </div> <h4align="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
| <screenname="OfbizDemoCommonDecorator"> <section> <actions> <property-mapresource="OfbizDemoUiLabels"map-name="uiLabelMap"global="true"/> <property-mapresource="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 --> <setfield="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 --> <setfield="javaScripts[+0]"value="/ofbizdemo/js/bootstrap.min.js"global="true"/> <setfield="layoutSettings.companyName"from-field="uiLabelMap.OfbizDemoCompanyName"global="true"/> </actions> <widgets> <section> <condition> <if-has-permissionpermission="OFBIZDEMO"action="_VIEW"/> </condition> <widgets> <platform-specific><html><html-templatelocation="component://ofbizdemo/webapp/ofbizdemo/includes/PreBody.ftl"/></html></platform-specific> <decorator-section-includename="pre-body"/> <decorator-section-includename="body"/> <platform-specific><html><html-templatelocation="component://ofbizdemo/webapp/ofbizdemo/includes/PostBody.ftl"/></html></platform-specific> </widgets> <fail-widgets> <labelstyle="h3">${uiLabelMap.OfbizDemoViewPermissionError}</label> </fail-widgets> </section> </widgets> </section> </screen> |
9.)在上一部分創建的Freemarker屏幕中使用此裝飾器:
OfbizDemoScreens.xml
10.)更新您的FTL文件以遵循HTML網絡標准,並將CSS應用於:
AddOfbizDemo.ftl
| <formmethod="post"action="<@ofbizUrl>createOfbizDemoEventFtl</@ofbizUrl>" name="createOfbizDemoEvent" class="form-horizontal"> <divclass="control-group"> <labelclass="control-label"for="ofbizDemoTypeId">${uiLabelMap.OfbizDemoType}</label> <divclass="controls"> <selectid="ofbizDemoTypeId"name="ofbizDemoTypeId"> <#list ofbizDemoTypes as demoType> <optionvalue='${demoType.ofbizDemoTypeId}'>${demoType.description}</option> </#list> </select> </div> </div> <divclass="control-group"> <labelclass="control-label"for="firstName">${uiLabelMap.OfbizDemoFirstName}</label> <divclass="controls"> <inputtype="text"id="firstName"name="firstName"required> </div> </div> <divclass="control-group"> <labelclass="control-label"for="lastName">${uiLabelMap.OfbizDemoLastName}</label> <divclass="controls"> <inputtype="text"id="lastName"name="lastName"required> </div> </div> <divclass="control-group"> <labelclass="control-label"for="comments">${uiLabelMap.OfbizDemoComment}</label> <divclass="controls"> <inputtype="text"id="comments"name="comments"> </div> </div> <divclass="control-group"> <divclass="controls"> <buttontype="submit"class="btn">${uiLabelMap.CommonAdd}</button> </div> </div> </form> |
ListOfbizDemo.ftl
| <tableclass="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文件相同的方式來包含它們。