对于OFBiz来说我们所有的定制化开发都要在
hot-deploy中进行。也就是说我们自己开发的组件都要放在hot-deploy文件夹中。
现在我们试着来定制化开发自己的一个组件。
首先我们在hot-deploy文件夹下新建一个名字叫practice的文件夹(hot-deploy\practice),注意这个文件夹名字一定要和我们要开发的组件名一样。
然后hot-deploy\practice路径下创建ofbiz-component.xml文件,文件内容如下
<?xml version="1.0"encoding="UTF-8"?>
<ofbiz-component name="practice"
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="practice"
title="Practice"
server="default-server"
location="webapp/practice"
base-permission="OFBTOOLS,ACCOUNTING"
mount-point="/practice"/>
</ofbiz-component>
这个文件在其他的任何已经存在的组件中都有,你可以从其他组件中拷贝过来然后进行修改。
ofbiz-component.xml这个文件时负责让OFBiz知道你的资源位置,并同时构筑ClassPath。
resource-loader name节点的“name”可以自己起一个任意的名称,但是“type”一定要写成“component”,因为只有写成“component”,OFBiz才能知道要加载这个组件。
a) name :- web应用的名字。
b) title :- 应用的标识,会显示在顶端导航栏上。
c) server :- 这个让OFBiz知道使用哪个server
d) base-permission :- 这行要求用户要有OFBTOOLS权限才能使用这个应用。因为'admin'用户有这个权限,所以我们不必创建其它新用户。
e) location :- 在这个服务器上缺省基准路径的位置。
f) mount-point :- 这是访问资源的URL,应该是localhost:8080/practice
g) app-bar-display :- 这个是让OFBiz知道是否显示在主应用导航条上,这个是公用OFBiz修饰的一部分。
创建web应用
1:在practice组件中创建一个"webapp"目录(hot-deploy/practice/webapp).,这个目录包含所有这组件相关的webapp目录。
2:在webapp目录下创建一个子目录命名为"practice",这个就是我们要开发的webapp名称
(hot-deploy/practice/webapp/practice). 一个组件可以附加多个web应用。比如在"marketing"组件中有两个webapps "marketing" and "sfa"。我们创建的webapp将按照J2EE的Web应用标准。
3:在你webapp下创建WEB-INF目录(hot-deploy/practice/webapp/practice/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。.
4: 创建一个命名为"web.xml"(web.xml遵守 j2ee
web应用规范). 这个文件的内容可以从存在的任意组件中拷贝,比如 /framework/example组件. 需要改变的重要值是<display-name>、localDispatcherName和 mainDecoratorLocation.
web.xml的内容如下:
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Open For Business - PracticeManager</display-name>
<description>Practice Manager Module of the Open For BusinessProject</description>
<context-param>
<param-name>entityDelegatorName</param-name>
<param-value>default</param-value>
<description>The Name of theEntity Delegator to use, defined in entityengine.xml</description>
</context-param>
<context-param>
<param-name>localDispatcherName</param-name>
<param-value>practice</param-value>
<description>A unique nameused to identify/recognize the local dispatcher for the ServiceEngine</description>
</context-param>
<context-param>
<param-name>mainDecoratorLocation</param-name>
<param-value>component://practice/widget/CommonScreens.xml</param-value>
<description>The location ofthe main-decorator screen to use for this webapp; referred to as a contextvariable in screen def XML files.</description>
</context-param>
<context-param>
<param-name>partyDecoratorLocation</param-name>
<param-value>component://practice/widget/CommonScreens.xml</param-value>
<description>The location ofthe CommonPartyDecorator screen to use for this webapp; referred to as acontext variable in screen def XML files.</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 mountingimplementations 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 ControlServlet</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>
5:创建一个命名"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"/>
<includelocation="component://commonext/webapp/WEB-INF/controller.xml"/>
<description>Practice Manager Module SiteConfiguration File</description>
<owner>Copyright 2001-2009 The Apache SoftwareFoundation</owner>
<!-- <handler name="screen"type="view"class="org.ofbiz.widget.screen.ScreenWidgetViewHandler"/> -->
<!-- Events to run on every request before security(chains exempt) -->
<!-- Request Mappings -->
<errorpage>/error/error.jsp</errorpage>
<request-map uri="main">
<security https="true"auth="false"/>
<responsename="success" type="view" value="main"/>
</request-map>
<!-- end of request mappings -->
<!-- View Mappings-->
<view-map name="main" type="screen"page="component://practice/widget/PracticeScreens.xml#main"/>
<!-- end of view mappings -->
</site-conf>
6:移到上一级,创建一个新的目录,命名为'error'(hot-deploy/practice/webapp/practice/error).
6.a : 在"error"目录中创建一个文件. 这个文件的内容可以取自任意存在的组件中,比如example组件。 你显示错误信息页面的位置将被指定在controller.xml文件的开始位置,比如
<errorpage>/error/error.jsp</errorpage> .你需要创建或拷贝一个/webapp/practice/error/error.jsp向用户显示错误信息。
7:在你的组件目录practice中创建一个"widget"(hot-deploy/practice/widget). 这个目录就包含forms, menus, and screens,用来处理用户界面的。
8:在"widget"中创建文件"PracticeScreens.xml".这个文件的内容可以取自任意组件中,
例如example组件。
PracticeScreens.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 practice"/>
</widgets>
</section>
</screen>
<screen name="person">
<section>
<actions>
<scriptlocation="component://practice/webapp/practice/WEB-INF/actions/Person.groovy"/>
</actions>
<widgets>
<decorator-screenname="CommonPracticeDecorator"location="${parameters.mainDecoratorLocation}">
<decorator-section name="body">
<platform-specific>
<html>
<html-templatelocation="component://practice/webapp/practice/Person.ftl">
</html>
</platform-specific>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
</screens>
9:现在我们这里有基本的元素,让我们再检查基本流程,不管你的组件变得如何庞大和复杂。开始,通过浏览器来发动一个请求来看指定的资源。举例说明这个请求:"localhost:8080/practice/control/main"
1. 当OFBiz看到这个请求,首先检查/practice部分。这是因为在我们的ofbiz-component.xml文件,我们指
出我们的web应用的装载点是/practice。现在知道我们的practice组件将处理余下的请求。
2. OFBiz将然后看我们的controller.xml文件。
在我们的controller.xml文件有request-maps和view-maps。如果它发现一个名字为'main'的request-map,它将使用相关联的view-map,如下所述。这个request-map 也能指定一个view或我们将会看到的event或service. 如果指定一个view将在the controller.xml 文件中进一步查找,看是否有在request-map指定值对应的view-map。
3. 现在我们继续保持简单,假定所有views到一个type=screen. 在这个情况下,页面标签指定一个路径到
screen定义文件而且一个screen名称显示在"#"符号后
10:现在该运行你的第一个练习应用。
通过命令行输入下面: java -Xmx256M -jar ofbiz.jar (the -Xmx256M命令仅保证程序有足够的内存)。然后在浏览器上点击这个地址http://localhost:8080/practice/control/main。浏览器应该看到输出屏幕显示"This is first practice"