ofbiz 官网例子 地址:https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Development+Guide
步骤 一:
创建组件定义的文件:
1、在 hot-deploy
/文件下建一个子目录,目录名为“practice” (如:hot-deploy/practice
),这个文件名称应该与组件名称对应。
2、在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"
base-permission="OFBTOOLS"
location="webapp/practice"
mount-point="/practice"
app-bar-display="false"/>
</ofbiz-component>
解释ofbiz-component.xml:
ofbiz-component.xml文件是负责告诉ofbiz组件资源的位置。他还将这个位置添加到类路径中。
在<resource-loader>标签里的名字可以是任何字符串,在这里我们定义为mian函数,type 属性告诉ofbiz 将要加载一个组件。
<resource-loader name="main" type="component"/>
<webapp>标签有7个属性:
<webapp name="practice"
title="Practice"
server="default-server"
base-permission="OFBTOOLS"
location="webapp/practice"
mount-point="/practice"
app-bar-display="false"/>
7个属性解释如下:
属性 | 解释 | 必须 |
---|---|---|
name | 我们的web应用程序的名字 | true |
title | 这个标题将显示在应用程序的顶部导航 | true |
server | 服务器使用 | true |
base-permission | 这个组件所需要的权限 | true |
location | 基本路径的默认服务 | true |
mount-point | 用于访问这个资源的地址,这种情况下他可以访问localhost:8080/practice | true |
app-bar-display | 为true 时该组件会出现在主导航中,如果为false 则不会 | true |
创建web应用程序的配置:
1、在practice组件中创建一个"webapp"文件(如hot-deploy/practice/webapp
),这个目录将包含所有这个组件相关的webapp文件。
2、因为"practice"是你要开发应用的名字,创建一个hot-deploy/practice/webapp/practice
文件。一个组件可以包含多个webapp。
例如"marketing"下有两个webapps组件"marketing" and "sfa"。
3、创建hot-deploy/practice/webapp/practice/WEB-INF
文件。
ofbiz应用程序需要两个配置文件,a controller.xml 和 a web.xml 。controller.xml告诉访问者如何请求,采取什么行动和页面如何呈现。
web.xml告诉OFBiz可用什么资源(数据库和业务逻辑访问)于此web应用程序以及如何处理与web相关的问题。如欢迎页面、跳转、或错误页面等。
4、创建一个web.xml,文件内容可以从已有组件中的web.xml复制。如e.g. /framework/example组件,修改 <display-name>, localDispatcherName, mainDecoratorLocation 和 webSiteId.
<context-param>
<param-name>webSiteId</param-name>
<param-value>PRACTICE</param-value>
<description>A unique ID used to look up the WebSite entity to get information about catalogs, etc.</description>
</context-param>
<context-param>
<param-name>localDispatcherName</param-name>
<param-value>practice</param-value>
<description>A unique name used to identify/recognize the local dispatcher for the Service Engine</description>
</context-param>
<context-param>
<param-name>mainDecoratorLocation</param-name>
<param-value>component://practice/widget/PracticeScreens.xml</param-value>
<!-- change the path to the following if the above doesn't work for you -->
<!-- <param-value>component://practice/webapp/practice/widget/PracticeScreens.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>
a 现在把websiteId的值改为"PRACTICE",这将在后面教程中讲解。
b 现在把mainDecoratorLocation的值暂时设置为component://practice/widget/CommonScreens.xml。这个位置是用于指向屏幕的主要装饰位置。
${parameters.mainDecoratorLocation}
这就增加了独立于变化在很多地方我们需要改变的主要装饰的位置,我们将只需要改变一个地方的位置,它将适用于所有的屏幕使用。在屏幕的另一个优点是resuability现有的屏幕我们可以用于多个组件的模式是用来显示mainDecoratorLocation相同。你在屏幕上记得添加修饰符。
c 创建 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>Practice Component Site Configuration File</description>
<owner>Copyright 2001-2009 The Apache Software Foundation</owner>
<!-- 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://practice/widget/PracticeScreens.xml#main"/>
<!-- change the path to the following if the above doesn't work for you -->
<!-- <view-map name="main" type="screen" page="component://practice/webapp/practice/widget/PracticeScreens.xml#main"/> -->
<!-- end of view mappings -->
</site-conf>
创建用户界面:
1、创建一个文件夹‘error’(hot-deploy/practice/webapp/practice/error),
a 在"error"目录里创建一个error.jsp页面。error.jsp内容可以在已经有的组件中复制(如example 组件),错误页面的地址将被指定在controller.xml 文件中,就像<errorpage>/error/error.jsp</errorpage> 这样。你必须要去创建一个或者复制一个/webapp/practice/error/error.jsp 用来展示错误信息。
2 在组件目录"practice"中,创建一个名为"widget"的文件(如 hot-deploy/practice/widget),这个目录将为UI创建将包括表单、菜单、屏幕。
3 在widget目录下创建PracticeScreens.xml 内容可以去已有的组件中复制如example 组件中。此后你将能够创建屏幕视图。
<?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>
</screens>
检查浏览应用程序:
现在我们有了基本元素,让我们回顾一下基本流程,不论多大的和多复杂的组件,首先一个请求,请求浏览器特定的资源。比如请求:"localhost:8080/practice/control/main"。
a 当ofbiz 看到这个请求首先会看practice部分,这是因为在我们的ofbiz-component.xml里配置了。我们曾经说过 webapp的挂载点是应该是/practice,因此OFBiz就知道我们practice 组件并将处理其余的请求。
b ofbiz 会看看我们的controller.xml文件,controller.xml里面会指明请求映射(request-maps)和视图(view-maps),如同他能发现一个名字为main的 请求映射(request-maps)他将用这个映射联系视图(view-maps),例如,request-map可以指定一个视图,等候我们会看到一个事件或一个服务,如果指定了一个视图,它将会进一步到controller.xml文件,看看是否有视图名称所指定的值标签request-map。
c 现在我们将保持简单呈现,所有屏幕视图的 type =screen,如果是这样的话,page标签将指定一个 * +Screens类似的地址然后用 # 号标记拼接request-map uri 值(如下代码)。
<view-map name="main" type="screen" page="component://practice/widget/PracticeScreens.xml#main"/>
运行这个practice应用程序:
1 现在是你第一次运行practice应用程序的时候了。
启动服务器通过输入下面的命令行:java -Xmx256M -jar ofbiz.jar(the -Xmx256M 这个命令确保可以有足够的内存)在你浏览器打开urlhttp://localhost:8080/practice/control/main 。你在浏览器打开应该是下面的效果
2 在webapp 下的practice 文件中创建一个index.jsp 文件,文件内容可以到example组件中复制,这个文件主要负责转发和响应control/main,如果给你个地址http://localhost:8080/practice/ 或者给你个地址 http://localhost:8080/practice/unknown/request/ 他将在web.xml 中重新指向重定向地址,ContextFilter将过滤请求和使用重定向路径重定向请求
步骤 一:
用户界面做一些改进:
1、现在是时候在这个应该程序中为这个屏幕(screens)做个装饰(decorator)了。
在"widget"目录中创建一个CommonScreens.xml文件,在整个应用程序,这个文件将包括常用的屏幕,常见的屏幕可能会有页眉和页脚引入到其他屏幕里。你可以参考 example 组件中的CommonScreens.xml文件 (代码如下)
<screen name="CommonPracticeDecorator">
<section>
<widgets>
<decorator-section-include name="body"/>
</widgets>
</section>
</screen>
参照 Example 组件中的CommonScreens.xml文件的main-decorator的使用,在web.xml 里添加一个引用CommonScreens.xml
<context-param>
<param-name>commonDecoratorLocation</param-name>
<param-value>component://practice/webapp/practice/widget/CommonScreens.xml</param-value>
<description>The location of the common-decorator screen to use for this webapp; referred to as a context variable in screen def XML files.</description>
</context-param>
为组件创建一个菜单文件,"widget"目录下创建一个 PracticeMenus.xml文件,可以参考"example"组件中的ExampleMenus.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<menus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/widget-menu.xsd">
<menu name="PracticeAppBar" title="PracticeApplication" extends="CommonAppBarMenu" extends-resource="component://common/widget/CommonMenus.xml">
<menu-item name="main" title="Main"><link target="main"/></menu-item>
</menu>
</menus>
在你的CommonPracticeDecorator中引入菜单 如下代码。
<screen name="CommonPracticeDecorator">
<section>
<widgets>
<include-menu location="component://practice/webapp/practice/widget/PracticeMenus.xml" name="PracticeAppBar"/>
<decorator-section-include name="body"/>
</widgets>
</section>
</screen>
2 在WEB-INF目录下创建一个actions子目录,在这个目录中,我们将创建脚本文件,脚本文件用于准备动态数据,这写文件将一groovy位后缀的文件,早些时候我们使用bsh(beanshell)文件,这些脚本将被用来从数据库获取数据的动态UI。工作时在groovy中总是有意识的对导入的类和包,文件中引入你一直用的文件,将日志消息从“调试”类的使用方法,在"actions"目录下创建一个名字为Person.groovy的文件,获取"Person"实例的所有信息,此刻这真的是非常少的代码(一行)。
context.persons = delegator.findList("Person", null, null, null, null, false);
上面的语句将获取所有人实体的记录,并将人放在上下文的名称。现在这个列表将迭代ftl文件中显示的记录
在webapp/practice/目录下创建一个Person.ftl文件,用来显示数据获取通过groovy文件吗。此刻你唯一的代码只需要做迭代列表。
<#if persons?has_content>
<h2>Some of the people who visited our site are:</h2>
<br>
<ul>
<#list persons as person>
<li>${person.firstName?if_exists} ${person.lastName?if_exists}</li>
</#list>
</ul>
</#if>
5 现在要在PracticeScreens.xml文件中创建一个新的名字为"person"屏幕条目,并且创建一个PracticeMenus.xml文件,
<screen name="person">
<section>
<actions>
<script location="component://practice/webapp/practice/WEB-INF/actions/Person.groovy"/>
</actions>
<widgets>
<decorator-screen name="CommonPracticeDecorator" location="${parameters.commonDecoratorLocation}">
<decorator-section name="body">
<platform-specific>
<html>
<html-template location="component://practice/webapp/practice/Person.ftl"/>
</html>
</platform-specific>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
6 现在改变controller.xml文件指向新的屏幕,正如我们前面所做的,现在再次运行应用程序并观察结果http://localhost:8080/practice/control/person,
使用表单控件创建一个实体形式展示person的内容
1 在PracticeMenus.xml文件中创建一个PersonForm的菜单组。
2 在widget 目录里创建一个名为PracticeForms.xml的文件并且一list 表单形式展示动态person数据。
可以参考 ExampleScreens.xml 和 ExampleForms.xml 文件。
<form name="ListPersons" type="list" list-name="persons" list-entry-name="person" default-map-name="person" paginate-target="personForm">
<!-- Important: Here service definition for updatePracticePerson has been used for automatically rendering the form fields, which you can use after completing CRUD operations from Part-3 -->
<!-- auto-fields-service service-name="updatePracticePerson" default-field-type="display" map-name="person"/-->
<!-- The above method can be used in case a service specific form is being rendered, otherwise form-fields can be explicitly mentioned as given below:-->
<field name="firstName"><display/></field>
<field name="middleName" ><display/> </field>
<field name="lastName" ><display/> </field>
</form>
创建一个名为personForm 的屏幕(screen) ,并引入list form 。
<screen name="PersonForm">
<section>
<actions>
<set field="headerItem" value="personForm"/>
<set field="titleProperty" value="PageTitlePracticePersonForm"/>
<entity-condition entity-name="Person" list="persons"/>
</actions>
<widgets>
<decorator-screen name="CommonPracticeDecorator" location="${parameters.mainDecoratorLocation}">
<decorator-section name="body">
<label text="Person List" style="h2"/>
<include-form name="ListPersons" location="component://practice/widget/PracticeForms.xml"></include-form>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
4 在 controller.xml 中添加此 屏幕(screen)
现在再次运行应用程序,观察不同,到目前为止你有从事控制器请求映射,屏幕小部件,部件形式,装饰,菜单,groovy和ftl。
如果您喜欢我写的博文,读后觉得收获很大,不妨小额赞助我一下,让我有动力继续写出高质量的博文,感谢您的赞赏!!!