Ofbiz12.04 进阶之三 第一个Helloword的施用

Ofbiz12.04 进阶之三 第一个Helloword的应用
网上的例子都太老了,基本上对于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

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<ofbiz-component name="hello1"
       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="hello1"
   title="My First OFBiz Application"
   server="default-server"
   location="webapp/hello1"
   mount-point="/hello1"
   app-bar-display="false"/>   
</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文件或者从别的应用里边拷贝一个过来修改。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEweb-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<!--
Licensedto the Apache Software Foundation (ASF) under one
ormore contributor license agreements.  Seethe NOTICE file
distributedwith this work for additional information
regardingcopyright ownership.  The ASF licensesthis file
to youunder the Apache License, Version 2.0 (the
"License");you may not use this file except in compliance
withthe License.  You may obtain a copy ofthe License at

http://www.apache.org/licenses/LICENSE-2.0

Unlessrequired by applicable law or agreed to in writing,
softwaredistributed under the License is distributed on an
"ASIS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND,either express or implied.  See theLicense for the
specificlanguage governing permissions and limitations
underthe License.
-->

<web-app>
   <display-name>Hello1</display-name>
   <description>The First Hello WorldApplication</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>hello1</param-value>
       <description>A unique name usedto identify/recognize the local dispatcher for the ServiceEngine</description>
   </context-param>
<context-param>
      <param-name>serviceReaderUrls</param-name>
      <param-value>/WEB-INF/services.xml</param-value>
       <description>Configuration File(s)For The Service Dispatcher</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>

   <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.ofbiz是基于MVC的框架,那么对于它的每一个应用来说我们一、都要配置一个controller.xml文件
在WEB-INF目录下边新建一个controller.xml文件或者从别的应用里边拷贝一个过来修改
<?xmlversion="1.0" encoding="UTF-8" ?>

<site-confxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/site-conf.xsd">
   <description>First Hello World SiteConfiguration File</description>
   <owner>Open For Business Project (c)2005 </owner>
  <errorpage>/error/error.jsp</errorpage>

   <handler name="java"type="request" class="org.ofbiz.webapp.event.JavaEventHandler"/>
   <handler name="soap"type="request"class="org.ofbiz.webapp.event.SOAPEventHandler"/>
   <handler name="service"type="request"class="org.ofbiz.webapp.event.ServiceEventHandler"/>
   <handler name="service-multi"type="request"class="org.ofbiz.webapp.event.ServiceMultiEventHandler"/>
   <handler name="simple"type="request"class="org.ofbiz.webapp.event.SimpleEventHandler"/>

   <handler name="ftl"type="view"class="org.ofbiz.webapp.ftl.FreeMarkerViewHandler"/>
   <handler name="jsp"type="view"class="org.ofbiz.webapp.view.JspViewHandler"/>
   <handler name="screen"type="view"class="org.ofbiz.widget.screen.ScreenWidgetViewHandler"/>

   <handler name="http"type="view" class="org.ofbiz.webapp.view.HttpViewHandler"/>



   <!-- Request Mappings -->
   <request-map uri="main">
       <response name="success"type="view" value="main"/>
   </request-map>

   <!-- end of request mappings -->

   <!-- View Mappings -->
   <view-map name="error"page="/error/error.jsp"/>
   <view-map name="main"type="ftl"page="component://hello1/webapp/hello1/main.ftl"/>
   <!-- end of view mappings -->
</site-conf>

6. 最后按照controller的配置,我们新建两个页面(error.jsp和 main.ftl)
在hello1/webapp/hello1目录下,新建一个error目录,并且在error目录下新建一个error.jsp文件
<%@ pageimport="org.ofbiz.base.util.*" %>
<html>
<head>
<title>Open For BusinessMessage</title>
<metahttp-equiv="Content-Type" content="text/html;charset=iso-8859-1">
</head>

<% String errorMsg = (String)request.getAttribute("_ERROR_MESSAGE_"); %>

<body bgcolor="#FFFFFF">
<div>
<br/>
<table width="100%" border="1"height="200">
  <tr>
    <td>
      <table width="100%" border="0"height="200">
        <tr bgcolor="#CC6666">
          <td height="45">
             <divalign="center"><font face="Verdana, Arial, Helvetica,sans-serif" size="4"color="#FFFFFF"><b>:ERRORMESSAGE:</b></font></div>
          </td>
        </tr>
        <tr>
          <td>
             <divalign="left"><font face="Verdana, Arial, Helvetica,sans-serif"size="2"><%=UtilFormatOut.replaceString(errorMsg,"\n", "<br/>")%></font></div>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</div>
<divalign="center"></div>
</body>
</html>

在hello1/webapp/hello1目录下新建一个main.ftl文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.0 Transitional//EN">
<HTML>
<title>Hello World</title>
<HEAD>
<META content="text/html;charset=utf-8" http-equiv=Content-Type></HEAD>
<BODY>
<h1>HELLO</h1>
Hello world!

</BODY></HTML>


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