ofbiz helloworld

   网上的例子都太老了,基本上对于ofbiz12.04都跑不起来,所以今天自己做了一个,下面来分享一下
参考:http://www.opensourcestrategies.com/ofbiz/hello_world1.php

1. 创建一个应用叫hello1,在ofbiz里,一个应用可以创建在 framework/, application/, specialized/, 或者 hot-deploy/ 目录下,为了方便起见我们建立在hot-deploy/ 目录下,因为这个目录下的应用时热部署,直接改动不需要重新启动服务器,省时,方便调试。
在hot-deploy/目录下创建一个文件夹名为hello1

2.在ofbiz里边,每个应用组件里必须要包含一个 ofbiz-component.xml 文件,这个文件告诉ofbiz在你的应用里所包含的的table,service,业务逻辑,表现层。。。
在hello1目录下创建一个文件名为ofbiz-component.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements.  See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership.  The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License.  You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied.  See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. -->
  18. <ofbiz-component name="hello1"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd">
  21. <resource-loader name="main" type="component"/>
  22. <webapp name="hello1"
  23.    title="My First OFBiz Application"
  24.    server="default-server"
  25.    location="webapp/hello1"
  26.    mount-point="/hello1"
  27.    app-bar-display="false"/>   
  28. </ofbiz-component>
复制代码

-ofbiz-component name="hello1"----这个组件名为hello1
-<resource-loader name="main" type="component"/>---用 “main” 这个resource-loader,这个基本上是默认的
- webapp name="hello1"  ----- 这个组件有一个应用叫hello1
- server="default-server" -----这个组件所用的servcer是default-server
-location="webapp/hello1" ---- 这个应用的所有资源都在webapp/hello1/这个目录下
- mount-point="/hello1"----- 这个应用的URI为hello1
因为我们不想他出现在app-bar里,所以这里设置为false,反则设置为true

3. 下面我们开始为hello1这个组件建立一个名为hello1的应用。
在hello1这个目录下建立一个文件夹名为webapp,之后再webapp文件夹里边再建立一个文件夹名为hello1。


4. 对于每一个web application,都应该有一个web描述文件,即web.xml
那么我们在webapp目录下建一个WEB-INF目录,在WEB-INF目录下边新建一个web.xml文件或者从别的应用里边拷贝一个过来修改。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPEweb-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
  3. <!--
  4. Licensedto the Apache Software Foundation (ASF) under one
  5. ormore contributor license agreements.  Seethe NOTICE file
  6. distributedwith this work for additional information
  7. regardingcopyright ownership.  The ASF licensesthis file
  8. to youunder the Apache License, Version 2.0 (the
  9. "License");you may not use this file except in compliance
  10. withthe License.  You may obtain a copy ofthe License at
  11. http://www.apache.org/licenses/LICENSE-2.0
  12. Unlessrequired by applicable law or agreed to in writing,
  13. softwaredistributed under the License is distributed on an
  14. "ASIS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. KIND,either express or implied.  See theLicense for the
  16. specificlanguage governing permissions and limitations
  17. underthe License.
  18. -->
  19. <web-app>
  20.    <display-name>Hello1</display-name>
  21.    <description>The First Hello WorldApplication</description>
  22.   <context-param>
  23.       <param-name>entityDelegatorName</param-name>
  24.       <param-value>default</param-value>
  25.        <description>The Name of theEntity Delegator to use, defined in entityengine.xml</description>
  26.    </context-param>
  27.    <context-param>
  28.       <param-name>localDispatcherName</param-name>
  29.       <param-value>hello1</param-value>
  30.        <description>A unique name usedto identify/recognize the local dispatcher for the ServiceEngine</description>
  31.    </context-param>
  32. <context-param>
  33.       <param-name>serviceReaderUrls</param-name>
  34.       <param-value>/WEB-INF/services.xml</param-value>
  35.        <description>Configuration File(s)For The Service Dispatcher</description>
  36.    </context-param>
  37.    <filter>
  38.       <filter-name>ContextFilter</filter-name>
  39.       <display-name>ContextFilter</display-name>
  40.       <filter-class>org.ofbiz.webapp.control.ContextFilter</filter-class>
  41.        <init-param>
  42.           <param-name>disableContextSecurity</param-name>
  43.           <param-value>N</param-value>
  44.        </init-param>
  45.        <init-param>
  46.           <param-name>allowedPaths</param-name>
  47.           <param-value>/error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images:/includes/maincss.css</param-value>
  48.        </init-param>
  49.        <init-param>
  50.           <param-name>errorCode</param-name>
  51.            <param-value>403</param-value>
  52.        </init-param>
  53.        <init-param>
  54.           <param-name>redirectPath</param-name>
  55.           <param-value>/control/main</param-value>
  56.        </init-param>
  57.    </filter>
  58.    <filter-mapping>
  59.        <filter-name>ContextFilter</filter-name>
  60.           <url-pattern>/*</url-pattern>
  61.    </filter-mapping>
  62.   <listener><listener-class>org.ofbiz.webapp.control.ControlEventListener</listener-class></listener>
  63.   <listener><listener-class>org.ofbiz.webapp.control.LoginEventListener</listener-class></listener>
  64.    <servlet>
  65.       <servlet-name>ControlServlet</servlet-name>
  66.       <display-name>ControlServlet</display-name>
  67.        <description>Main ControlServlet</description>
  68.       <servlet-class>org.ofbiz.webapp.control.ControlServlet</servlet-class>
  69.       <load-on-startup>1</load-on-startup>
  70.    </servlet>
  71.    <servlet-mapping>
  72.       <servlet-name>ControlServlet</servlet-name>
  73.       <url-pattern>/control/*</url-pattern>
  74.    </servlet-mapping>
  75.    <session-config>
  76.       <session-timeout>60</session-timeout> <!-- in minutes-->
  77.    </session-config>
  78.    <welcome-file-list>
  79.       <welcome-file>index.jsp</welcome-file>
  80.       <welcome-file>index.html</welcome-file>
  81.       <welcome-file>index.htm</welcome-file>
  82.   </welcome-file-list>
  83. </web-app>
复制代码

5.ofbiz是基于MVC的框架,那么对于它的每一个应用来说我们一、都要配置一个controller.xml文件
在WEB-INF目录下边新建一个controller.xml文件或者从别的应用里边拷贝一个过来修改

  1. <?xmlversion="1.0" encoding="UTF-8" ?>
  2. <site-confxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.       xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/site-conf.xsd">
  4.    <description>First Hello World SiteConfiguration File</description>
  5.    <owner>Open For Business Project (c)2005 </owner>
  6.   <errorpage>/error/error.jsp</errorpage>
  7.    <handler name="java"type="request" class="org.ofbiz.webapp.event.JavaEventHandler"/>
  8.    <handler name="soap"type="request"class="org.ofbiz.webapp.event.SOAPEventHandler"/>
  9.    <handler name="service"type="request"class="org.ofbiz.webapp.event.ServiceEventHandler"/>
  10.    <handler name="service-multi"type="request"class="org.ofbiz.webapp.event.ServiceMultiEventHandler"/>
  11.    <handler name="simple"type="request"class="org.ofbiz.webapp.event.SimpleEventHandler"/>
  12.    <handler name="ftl"type="view"class="org.ofbiz.webapp.ftl.FreeMarkerViewHandler"/>
  13.    <handler name="jsp"type="view"class="org.ofbiz.webapp.view.JspViewHandler"/>
  14.    <handler name="screen"type="view"class="org.ofbiz.widget.screen.ScreenWidgetViewHandler"/>
  15.    <handler name="http"type="view" class="org.ofbiz.webapp.view.HttpViewHandler"/>
  16.    <!-- Request Mappings -->
  17.    <request-map uri="main">
  18.        <response name="success"type="view" value="main"/>
  19.    </request-map>
  20.    <!-- end of request mappings -->
  21.    <!-- View Mappings -->
  22.    <view-map name="error"page="/error/error.jsp"/>
  23.    <view-map name="main"type="ftl"page="component://hello1/webapp/hello1/main.ftl"/>
  24.    <!-- end of view mappings -->
  25. </site-conf>
复制代码

6. 最后按照controller的配置,我们新建两个页面(error.jsp和 main.ftl)
在hello1/webapp/hello1目录下,新建一个error目录,并且在error目录下新建一个error.jsp文件

  1. <%@ pageimport="org.ofbiz.base.util.*" %>
  2. <html>
  3. <head>
  4. <title>Open For BusinessMessage</title>
  5. <metahttp-equiv="Content-Type" content="text/html;charset=iso-8859-1">
  6. </head>
  7. <% String errorMsg = (String)request.getAttribute("_ERROR_MESSAGE_"); %>
  8. <body bgcolor="#FFFFFF">
  9. <div>
  10. <table width="100%" border="1"height="200">
  11.   <tr>
  12.     <td>
  13.       <table width="100%" border="0"height="200">
  14.         <tr bgcolor="#CC6666">
  15.           <td height="45">
  16.              <divalign="center"><font face="Verdana, Arial, Helvetica,sans-serif" size="4"color="#FFFFFF"><b>:ERRORMESSAGE:</b></font></div>
  17.           </td>
  18.         </tr>
  19.         <tr>
  20.           <td>
  21.              <divalign="left"><font face="Verdana, Arial, Helvetica,sans-serif"size="2"><%=UtilFormatOut.replaceString(errorMsg,"\n", "
  22. ")%></font></div>
  23.           </td>
  24.         </tr>
  25.       </table>
  26.     </td>
  27.   </tr>
  28. </table>
  29. </div>
  30. <divalign="center"></div>
  31. </body>
  32. </html>
复制代码

在hello1/webapp/hello1目录下新建一个main.ftl文件

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.0 Transitional//EN">
  2. <HTML>
  3. <title>Hello World</title>
  4. <HEAD>
  5. <META content="text/html;charset=utf-8" http-equiv=Content-Type></HEAD>
  6. <BODY>
  7. <h1>HELLO</h1>
  8. Hello world!
  9. </BODY></HTML>
复制代码

基本上一个新的组件就已经完成了,因为我们的组件是建立在hot-deploy目录下,所以运行ofbiz,这个组件直接就被加载了。如果想要把这个组件放到applications目录下,需要修改applications目录下的component-load.xml文件
在<component-loader> </component-loader>里边添加一行<load-componentcomponent-location="hello1"/>,这样当我们从新启动ofbiz的时候这个组件就会被ofbiz所加载。

posted @ 2015-03-09 17:35  shenming  阅读(214)  评论(0编辑  收藏  举报