由于本平台过于庞大,决定从一个小示例开始,动手前搜了一下,网上有不少例子,尝试过后发现都不能运行成功。参考《OFBiz Tutorial - A Beginners Development Guide》及官方的Practice例子后,终于成功的编了个世界经典--Hello World,写此文以记录。
环境:
jdk1.8.0_65 X64
apache-ofbiz-13.07.02 zip形式的发布包
eclipse-mars.1
步骤:
第 1 步:在 hot-deploy 下创建子目录命名为“helloworld“,这是我们的组件名
第 2 步:在 hot-deploy/helloworld路径下创建 ofbiz-component.xml 文件,格式可以从其它同名文件中拷贝,内容如下
<?xml version="1.0" encoding="UTF-8"?> <ofbiz-component name="helloworld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd"> <!-- The name attribute in the <resource-loader> tag can be any string. Here we are setting it as "main". The type attribute tells OFBiz that we will be loading a component. --> <resource-loader name="main" type="component" /> <!-- webapp name title: app title 将显示在导航栏上 base-permission:需要哪个权限才可以使用这个应用 location:基准路径 mount-point:挂载点,如http://localhost:8080/helloworld app-bar-display:是否显示在主应用导航条上,这个是公用ofbiz修饰的一部分 --> <webapp name="helloworld" server="default-server" title="Helloworld" location="webapp/helloworld" mount-point="/helloworld" app-bar-display="true" base-permission="OFBTOOLS" /> </ofbiz-component>
第 3 步:创建web应用
3.1 在 hot-deploy/helloworld下创建目录webapp(hot-deploy/helloworld/webapp)
3.2 在 webapp 目录下创建一个子目录命名为"helloworld",这个就是我们要开发的 webapp 名称(hot-deploy/helloworld/webapp/helloworld)
注:一个组件可以附加多个web应用
3.3 在你 webapp 下创建 WEB-INF 目录(hot-deploy/helloworld/webapp/helloworld/WEB-INF)
注:OFBiz 的web 应用要有两个配置文件: controller.xml 和 web.xml。这 controller.xml 告诉 OFBiz 从访问者来的不同请求做
不同的事:做什么动作和渲染什么页面。 web.xml 告诉 OFBiz 什么资源对这个 web 应用是有效的和如何处理 web 相关的事情
3.4 创建一个命名为"web.xml"(web.xml 遵守 j2ee web 应用规范),此处的全路径为:hot-deploy/helloworld/webapp/helloworld/WEB-INF/web.xml,格式可以从OFBiz的其它应用中拷贝,内容如下
<?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"> <web-app> <display-name>HelloWorld Application</display-name> <description>HelloWorld Application of the Open For Business Project</description> <context-param> <param-name>webSiteId</param-name> <param-value>HELLOWORLD</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>helloworld</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://helloworld/widget/HelloWorldScreens.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>entityDelegatorName</param-name><param-value>default</param-value> <description>The Name of the Entity Delegator to use, defined in entityengine.xml</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:/js</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> <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> </web-app>
3.5 创建controller.xml文件,文件的路径为:hot-deploy/helloworld/webapp/helloworld/WEB-INF/controller.xml,对做过web开发人员而言,这个文件的内容很容易理解,文件的文件内容如下:
<?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>HelloWorld Component Site Configuration File</description> <owner>Open For business Project (c)2005 </owner> <request-map uri="main"> <security auth="false" https="false"/> <response name="success" type="view" value="main"/> </request-map> <view-map name="main" type="screen" page="component://helloworld/widget/HelloworldScreens.xml#main"/> </site-conf>
3.6 在组件目录 helloworld中创建一个"widget"(hot-deploy/helloworld/widget)目录,这个目录是用于处理用户界面的,如:forms, menus,和 screens
3.7 在"widget"中创建文件"HelloworldScreens.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 the first practice."></label> </widgets> </section> </screen> </screens>
4. 启动OFBiz,可以在OFBiz的目录下执行 java -Xmx256M -jar ofbiz.jar
5. 打开浏览器,输入http://localhost:8080/helloworld/control/main
如果出现“This is the first practice.”,恭喜你,成功了!
好了,到这里我们已经构建出了一个最简化的基于OFBiz的小应用了,相关配置项的含义待有时间再慢慢写下来。