OFBIZ的第一次体验

刚学ofbiz开源框架,当下载与安装后,首先想做的事就是用它来写一个helloWorld。于是就按照官网开始写吧。

第一步:

Create the sub-directory (folder) in hot-deploy/ and name it "practice"(hot-deploy/practice). The directory name should match the new components name that we are creating. Note : Remember that all customized development is done at this place only. 啥意思呢?俺的英语也烂,但应该是说在ofbiz(也就是我们下载的ofbiz解压后的目录)下的hot-deploy文件夹下建一个practice的文件夹,而且这个名字要和我们即将新建的组件的名字要一致。注意事项应该是说以后咱们自己写的应用只能放到hot-deploy这个文件夹下。

第二步:

Create the ofbiz-component.xml file on path hot-deploy/practice and place the following content in it (for reference you can check this file in any other component of OFBiz): 新建一个ofbiz-component.xml文件 在practice下面,这个文件其实在示例里面可以找到,在spcialpurpose这个目录下有个example文件夹,(示例存放的位置与ofbiz的版本可能有关,所以路径可能不一样,下面会提到,如果在spcialpurpose下不能找到示例,请参照第六步提到的路径查找)可以去搜来用。现在在文件里写内容:

<?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>


官网上说这个文件应该是起一个资源定位作用,并把资源放到classpath下。

第三步:

Create a "webapp" directory in the practice component (hot-deploy/practice/webapp).
This directory contains all the webapp related files for the component we are creating.

啥意思?在practice下建一个webapp文件夹,行动。这个目录时包含的就是整个web应用时与我们建的组件相关的文件,组件在哪儿?我也不知道,咱接着往下走。

第四步:

Create a sub-directory inside the webapp directory by the name of  "practice" which is the name of the webapp which you are going to develop (hot-deploy/practice/webapp/practice).
The webapp we are creating will follow the J2EE webapp standards.

在webapp下建个子目录,取个名字叫practice,为啥这样,人家 说了,这个名字要和你正开发的项目名字一样,那就这样吧。而且这个应用就是按照j2ee的标准来的。看了一位大牛的文章后,果断跑到tomcat里的webapp里一看,确实是那么回事,和开发j2ee的webapp像。

第五步:

Create WEB-INF directory in your webapp (hot-deploy/practice/webapp/practice/WEB-INF).
An OFBiz web application requires two configuration files, a controller.xml and a web.xml. The controller.xml tells OFBiz what to do with various requests from visitors: what actions to take and what  pages to render. web.xml tells OFBiz what resources (database and business logic access) are available for this web application and how to handle web-related issues, such as welcome pages, redirects, and error pages.

这个看着就有点感觉了,在practice下建WEB-INF (注意这里的practice是webapp下的),然后说在WEB-INF下有两个文件,a controller.xml and a web.xml. 至少有一个咱认识 ,controller.xml 这个告诉ofbiz 用户的请求,该采取什么动作,显示啥样的页面。 原来这个和struts2的配置文件差不多嘛。 再来看web.xml。它告知ofbiz能从应用里定位哪些资源(数据和逻辑业务)以及如何处理这些关系分配。原来他就是一个我们认识的web.xml

第六步:

Create a file named "web.xml"(web.xml follows j2ee webapp specs). Contents of this file can be copied from any of the existing component e.g. /framework/example component. The Important values to change are the <display-name>, the localDispatcherName, the mainDecoratorLocation and the webSiteId.

新建一个web.xml ,告诉我们可以去示例里面去找一个。这里他的示例路径不知道是哪个版本的,我的示例反正不在这里,第二步里有提到。接着提到需要改几个重要的值。看下面的修改代码:

<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/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>

在示例里拷贝过来的文件就按上面的代码修改。其他的内容就别管也别删。

第七步:

Create a file named "controller.xml" (used by ofbiz webapp controller) 

不用说,新建一个controller.xml文件。这个文件是被ofbiz的应用窗口来使用

<?xml version= "1.0" encoding= "UTF-8" ?>
        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>
        <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://practice/widget/PracticeScreens.xml#main" />
        <!-- end of view mappings -->
</site-conf>

第八步:

Create a sub-directory inside your component directory "practice" named "widget"(hot-deploy/practice/widget). This directory will contain your forms, menus, and screens which will be created for UI.

这里是让咱返回到最上级的practice目录,在里面新建一个widget文件夹,也就是和webapp在同一个目录下。再看它的作用,原来这货就是界面。里面包含的都是创建界面的表单、菜单等内容。可是该咋做呢?接着走。

第九步:

Create a file inside the directory "widget" named "PracticeScreens.xml". Contents of this file can be copied from any existing component  e.g. example component.
As now onwards you will be able to create screens views so an important reading at this place will be Best Practices Guide.

这里终于告诉咱,先建个 PracticeScreens.xml 在里面(这个在示例里搜Screens.xml ,能出来好多,选 个Common的,这首字母还得大写?先照着写吧)。作用?原来这个建好后就可以创建屏幕视图了,还告诉咱,这个地方就要看仔细了,很关键,那咱就看它咋弄吧。

<?xml version= "1.0" encoding= "UTF-8" ?>
      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>

咋感觉比controller.xml里的内容要简单点呢?不管怎么着,好像我们把一个应用就写完了。下面应该是去验证咱们的努力是笑容还是泪水了。

第十步:

启动ofbiz,如果还没ant 过的就要先把ofbiz先在本地配置好,然后在tools文件夹下面启动startofbiz.bat

下面就在浏览器里输入  http://localhost:8080/practice/control/main

猜你喜欢

转载自wlgqo.iteye.com/blog/1926243