建立一個組件
第一步:在hot-deploy 下建立子目錄命名為“computer“。這個目錄名要匹配我們要建立的組件名。
第二步:在hot-deploy/computer 路徑下建立ofbiz-component.xml 檔案,填入下面的內容。
<?xml version="1.0" encoding="UTF-8"?><ofbiz-component name="computer"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd"><resource-loader name="main" type="component" /><webapp name="computer" title="Computer" server="default-server"base-permission="OFBTOOLS" location="webapp/computer" mount-point="/computer"app-bar-display="false" /></ofbiz-component>
1. 這個ofbiz-component.xml 檔案是負責讓OFBiz 知道資源的位置,同時讓你加入到classpath 中.
2. 這個'resource-loader name' 可以是任一字元串。這兒我們設為"main". 這個 'type' 告訴OFBiz 我們將開始裝載一個組件。
3. 在標籤中,有不同的屬性和它們的目的如下:
a) name :- 定義我們web 應用的名字。
b) title :- 這個將是應用的標識,會顯示在頂端導覽列上。
c) server :- 這個讓OFBiz 知道使用哪個server
d) base-permission :- 這行要求使用者要有OFBTOOLS 許可權才能使用這個應用。因為'admin'使用者有這個許可權,所以我們不必建立其它新使用者。
e) location :- 在這個伺服器上預設基準路徑的位置。
f) mount-point :- 這是訪問資源的URL,應該是localhost:8080/computer
g) app-bar-display :- 這個是讓OFBiz 知道是否顯示在主應用導航條上,這個是公用OFBiz 修飾的一部分。
建立 web 應用
第一步:在 computer 組件中建立一個"webapp"目錄(hot-deploy/computer/webapp).,這個目錄包含所有這組件相關的webapp 目錄。
第二步:在 webapp 目錄下建立一個子目錄命名為"computer",這個就是我們要開發的webapp 名稱(hot-deploy/computer/webapp/computer).
第三步:在你 webapp 下建立WEB-INF 目錄(hot-deploy/computer/webapp/computer/WEB-INF)。一個OFBiz 的web 應用要有兩個設定檔:controller.xml 和web.xml。這controller.xml 告訴OFBiz 從訪問者來的不同請求做不同的事:做什麼動作和渲染什麼頁面。web.xml 告訴OFBiz 什麼資源 (database and business logic access) 對這個web 應用是有效和如何處理web 相關的事,比如welcome pages, redirects, and error pages。.
第三步:建立一個命名為"web.xml"(web.xml 遵守 j2ee web 應用規範). 這個檔案的內容如下
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><!--Licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreements. See the NOTICE filedistributed with this work for additional informationregarding copyright ownership. The ASF licenses this fileto you under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliancewith the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing,software distributed under the License is distributed on an"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied. See the License for thespecific language governing permissions and limitationsunder the License.--><web-app> <display-name>Open For Business - Computer Application</display-name> <description>Computer Application of the Open For Business Project</description> <context-param> <param-name>webSiteId</param-name> <param-value>COMPUTER</param-value> <description>A unique ID used to look up the WebSite entity</description> </context-param> <context-param> <param-name>localDispatcherName</param-name><param-value>computer</param-value> <description>A unique name used to identify/recognize the local dispatcher for the Service Engine</description> </context-param> <context-param> <param-name>entityDelegatorName</param-name><param-value>default</param-value> <description>The Name of the Entity Delegator to use, defined in entityengine.xml</description> </context-param> <context-param> <param-name>mainDecoratorLocation</param-name> <param-value>component://computer/widget/CommonScreens.xml</param-value> <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> </context-param> <!-- context-param> <param-name>widgetVerbose</param-name> <param-value>false</param-value> <description>Enable/disable widget boundary comments. will override widget.properties See org.ofbiz.widget.ModelWidget.widgetBoundaryCommentsEnabled().</description> </context-param--> <context-param> <param-name>compressHTML</param-name> <param-value>false</param-value> <description>Remove unnecessary whitespace from HTML output.</description> </context-param> <filter> <filter-name>ContextFilter</filter-name> <display-name>ContextFilter</display-name> <filter-class>org.ofbiz.webapp.control.ContextFilter</filter-class> <init-param><param-name>disableContextSecurity</param-name><param-value>N</param-value></init-param> <init-param> <param-name>allowedPaths</param-name> <param-value>/error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images:/includes/maincss.css</param-value> </init-param> <init-param><param-name>errorCode</param-name><param-value>403</param-value></init-param> <init-param><param-name>redirectPath</param-name><param-value>/control/main</param-value></init-param> </filter> <filter-mapping><filter-name>ContextFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping> <listener><listener-class>org.ofbiz.webapp.control.ControlEventListener</listener-class></listener> <listener><listener-class>org.ofbiz.webapp.control.LoginEventListener</listener-class></listener> <!-- NOTE: not all app servers support mounting implementations of the HttpSessionActivationListener interface --> <!-- <listener><listener-class>org.ofbiz.webapp.control.ControlActivationEventListener</listener-class></listener> --> <servlet> <servlet-name>ControlServlet</servlet-name> <display-name>ControlServlet</display-name> <description>Main Control Servlet</description> <servlet-class>org.ofbiz.webapp.control.ControlServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping><servlet-name>ControlServlet</servlet-name><url-pattern>/control/*</url-pattern></servlet-mapping> <session-config><session-timeout>60</session-timeout><!-- in minutes --></session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list></web-app>
第五步:建立一個命名"controller.xml" (被ofbiz webapp 控制器使用)的檔案。這個檔案開始是小而簡單,但隨後我們增加功能而快速增長。代碼如下
<?xml version="1.0" encoding="UTF-8"?><site-conf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/site-conf.xsd"><include location="component://common/webcommon/WEB-INF/common-controller.xml" /><description>Computer Component Site Configuration File</description><owner>Copyright 2001-2009 The Apache Software Foundation</owner><handler name="screen" type="view"class="org.ofbiz.widget.screen.ScreenWidgetViewHandler" /> <!-- Request Mappings --><request-map uri="main"><security https="false" auth="false" /><response name="success" type="view" value="main" /></request-map><!-- end of request mappings --><!-- View Mappings --><view-map name="main" type="screen"page="component://computer/widget/ComputerScreens.xml#main" /><!-- end of view mappings --></site-conf>
第六步:在目錄hot-deploy/computer/webapp/computer下建一個index.jsp.代碼如下
<%response.sendRedirect("control/main");%>
第七步:在你的組件目錄practice 中建立一個"widget"(hot-deploy/practice/widget). 這個目錄就包含forms,menus, and screens,用來處理使用者介面的。
第八步:在"widget"中建立檔案"ComputerScreens.xml".代碼如下
<?xml version="1.0" encoding="UTF-8"?><screens xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/widget-screen.xsd"><screen name="main"><section><widgets><label text="This is first computer" /></widgets></section></screen></screens>
第九步:重新啟動ofbiz。啟動完成後輸入http://localhost:8080/computer/control/main既可以看到如下介面