`
liuruirui1986
  • 浏览: 11960 次
社区版块
存档分类
最新评论

Ofbiz 入门教程 2

阅读更多
程序中的处理大部分可以看懂的,其中有个功能,是
  Map paras = UtilMisc.getParameterMap(request);
这是 Ofbiz 的一个有趣但非常有用的功能,它是把 request 中各段的名字和值映射到一个 Map
对象中,然后使用
  cust.setNonPKFields(paras);
就可以赋给 Object cust 的各个段,免了我们使用 request.getParameter("name")来取各个
值,在值很多的时候这个功能可以大大减少冗余代码量。

  基本程序的逻辑是这样的,
  1.从 request 读取传来的值
 2.使用 delegator 来处理,Add/Update/Delete/Query
  3.将返回结果放到 Session 中传给 JSP

  我做了个 Ant build.xml 文件可以帮助编译,把这个文件放在:
   c:\ofbiz\ofbiz\testOfbiz\ 目录下,然后在命令行窗口下进入该目录,敲入 ant
  来编译(需要保证已经安装 Ant),编译后的 .class 会放在
   c:\ofbiz\ofbiz\testOfbiz\com\geeyo\ofbiz 下,
  拷贝 c:\ofbiz\ofbiz\testofbiz\com 目录到 c:\ofbiz\ofbiz\partymgr\webapp\WEB-INF\classes
  目录下。

  build.xml
>=============================================================================

<project name="TestOfbiz" default="dist" basedir=".">
    <description>
          Test ofbiz
    </description>

  <!--test cvs-->
  <!-- set global properties for this build -->

  <property name="src" location="."/>
  <property name="build" location="."/>

  <property name="lib_dir"  location="c:/ofbiz/catalina/shared/lib"/>
  <property name="lib1_dir"  location="c:/ofbiz/catalina/common/lib"/>

  <path id="project.class.path">
    <fileset dir="${lib_dir}">
       <include name="*.jar"/>
    </fileset>
    <fileset dir="${lib1_dir}">
       <include name="*.jar"/>
    </fileset>
  </path>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}">
      <classpath refid="project.class.path"/>
    </javac>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
  </target>
</project>


>=============================================================================

然后我们来创建 JSP 程序,JSP 程序全部放在
  c:\ofbiz\ofbiz\partymgr\webapp\party 下面

  1.listofbiz.jsp
>=============================================================================

<%@ taglib uri="ofbizTags" prefix="ofbiz" %>

<%@ page import="java.util.*, org.ofbiz.core.service.ModelService" %>
<%@ page import="org.ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>
<%@ page import="org.ofbiz.core.entity.*" %>
<jsp:useBean id="security" type="org.ofbiz.core.security.Security" scope="request" />
<jsp:useBean id="delegator" type="org.ofbiz.core.entity.GenericDelegator" scope="request" />

<script language="JavaScript">
  function confirmDelete()
  {
     return confirm("Are your sure to delete?");
  }
</script>


<%if(security.hasEntityPermission("PARTYMGR", "_VIEW", session)) {%>

<table width="600" align="center">
  <ofbiz:if name="search_results">
   <tr><th>Id</th><th>Name</th><th>Note</th><th></th></tr>
    <ofbiz:iterator name="cust" property="search_results">
      <tr>
        <td><ofbiz:entityfield attribute="cust" field="customerId"/></td>
        <td><ofbiz:entityfield attribute="cust" field="customerName"/></td>
        <td><ofbiz:entityfield attribute="cust" field="customerNote"/></td>
        <td>
          <a href='<ofbiz:url>/showtest?customerId=<ofbiz:entityfield attribute="cust" field="customerId"/></ofbiz:url>' class="buttontext">[Edit]</a>
          <a href='<ofbiz:url>/removetest?customerId=<ofbiz:entityfield attribute="cust" field="customerId"/></ofbiz:url>' class="buttontext" onclick="return confirmDelete()">[Remove]</a>
        </td>
      </tr>
     </ofbiz:iterator>
    </ofbiz:if>
</table>
<table width="200" align="center">
  <tr>
  <td><a href='<ofbiz:url>/createTestForm</ofbiz:url>'>Create customer</a></td>
  </tr>
</table>

<%}else{%>
  <h3>You do not have permission to view this page. ("PARTYMGR_VIEW" or "PARTYMGR_ADMIN" needed)</h3>
<%}%>

>=============================================================================

上面程序中需要说明的是
  <ofbiz:if name="search_results">

  <ofbiz:iterator name="cust" property="search_results">,

<ofbiz:if name="search_results"> 是用来检验在 session 或 pageContext 对象
中是否包含 search_results 对象,该对象是由我们的程序放到 session 中的。
<ofbiz:iterator name="cust" property="search_results"> 是用来循环读取对象
search_results(是个 Collection 对象)中存储的各对象,并赋给cust,然后在循环体
中,我们就可以用 cust 对象来读取各个段的值了。


2.createofbiz.jsp
>=============================================================================

<%@ taglib uri="ofbizTags" prefix="ofbiz" %>

<%@ page import="java.util.*, org.ofbiz.core.service.ModelService" %>
<%@ page import="org.ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>
<%@ page import="org.ofbiz.core.entity.*" %>
<jsp:useBean id="security" type="org.ofbiz.core.security.Security" scope="request" />
<jsp:useBean id="delegator" type="org.ofbiz.core.entity.GenericDelegator" scope="request" />
<%if(security.hasEntityPermission("PARTYMGR", "_VIEW", session)) {%>

<form method="post" action="<ofbiz:url>/createTest</ofbiz:url>" name="createofbiz">
<table width="300" align="center">
  <tr>
   <td>Id</td><td><input type="text" name="customerId" size="20"></td>
  </tr>
  <tr>
   <td>Name</td><td><input type="text" name="customerName" size="20"></td>
  </tr>
  <tr>
   <td>Note</td><td><input type="text" name="customerNote" size="30"></td>
  </tr>
  <tr>
   <td></td>
  <td><input type="submit"></td>
  </tr>
</table>
</form>

<%}else{%>
  <h3>You do not have permission to view this page. ("PARTYMGR_VIEW" or "PARTYMGR_ADMIN" needed)</h3>
<%}%>
>=============================================================================

  这个程序很容易理解,需要注意的是每个文本框的名字,要跟 Schema StudyCustomer 的各
个段一致,以使程序中跟容易处理。

3.showofbiz.jsp
>=============================================================================

<%@ taglib uri="ofbizTags" prefix="ofbiz" %>

<%@ page import="java.util.*, org.ofbiz.core.service.ModelService" %>
<%@ page import="org.ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>
<%@ page import="org.ofbiz.core.entity.*" %>
<jsp:useBean id="security" type="org.ofbiz.core.security.Security" scope="request" />
<jsp:useBean id="delegator" type="org.ofbiz.core.entity.GenericDelegator" scope="request" />
<%if(security.hasEntityPermission("PARTYMGR", "_VIEW", session)) {%>

<form method="post" action="<ofbiz:url>/updateTest</ofbiz:url>" name="updateofbiz">
<table width="300" align="center">
  <tr>
   <td>Id</td><td><input type="text" name="customerId" size="20" value="<ofbiz:entityfield attribute="edit_cust" field="customerId"/>"></td>
  </tr>
  <tr>
   <td>Name</td><td><input type="text" name="customerName" size="20" value="<ofbiz:entityfield attribute="edit_cust" field="customerName"/>"></td>
  </tr>
  <tr>
   <td>Note</td><td><input type="text" name="customerNote" size="30" value="<ofbiz:entityfield attribute="edit_cust" field="customerNote"/>"></td>
  </tr>
  <tr>
   <td></td>
   <td><input type="submit"></td>
  </tr>
</table>
</form>

<%}else{%>
  <h3>You do not have permission to view this page. ("PARTYMGR_VIEW" or "PARTYMGR_ADMIN" needed)</h3>
<%}%>

>=============================================================================

  这个程序中,主要是通过
  <ofbiz:entityfield attribute="edit_cust" field="customerId"/>
  把取到的对象的段显示出来, 对象 edit_cust 是我们在程序中取到并放到 session 中的。

下面我们来配置 controller.xml 和 regions.xml, 在 controller.xml 中加入:
>=============================================================================

   <request-map uri="createTestForm">
      <description>Show the create form</description>
      <security https="false" auth="false"/>
      <response name="success" type="view" value="createTestForm"/>
   </request-map>

   <request-map uri="testofbiz">
      <description>Test Ofbiz</description>
      <security https="false" auth="false"/>
      <response name="success" type="view" value="testofbiz"/>
   </request-map>
 
   <request-map uri="listtest">
      <description>List all records</description>
      <security https="false" auth="false"/>
      <event type="java" path="com.geeyo.ofbiz.TestOfbiz" invoke="lookAllRecords" />
      <response name="success" type="view" value="listAllTest"/>
   </request-map>

   <request-map uri="showtest">
      <description>Show records</description>
      <security https="false" auth="false"/>
      <event type="java" path="com.geeyo.ofbiz.TestOfbiz" invoke="findRecord" />
      <response name="success" type="view" value="showTest"/>
   </request-map>

   <request-map uri="createTest">
       <security https="true" auth="true"/>
       <event type="java" path="com.geeyo.ofbiz.TestOfbiz" invoke="createNewRecord"/>
       <response name="success" type="request" value="listtest"/>
       <response name="error" type="view" value="createTestForm"/>
   </request-map>

   <request-map uri="updateTest">
      <description>update a record</description>
      <security https="false" auth="false"/>
      <event type="java" path="com.geeyo.ofbiz.TestOfbiz" invoke="updateRecord" />
      <response name="success" type="request" value="listtest"/>
   </request-map>

   <request-map uri="removetest">
      <description>remove a record</description>
      <security https="false" auth="false"/>
      <event type="java" path="com.geeyo.ofbiz.TestOfbiz" invoke="removeRecord" />
      <response name="success" type="request" value="listtest"/>
   </request-map>

   <view-map name="listAllTest" type="region"/>
   <view-map name="createTestForm" type="region"/>
   <view-map name="showTest" type="region"/>
>=============================================================================

  在 regions.xml 中加入:
>=============================================================================
   <define id='createTestForm' region='MAIN_REGION'>
       <put section='title'>Create Ofbiz</put>
       <put section='content' content='/party/createofbiz.jsp'/>
   </define>

   <define id='listAllTest' region='MAIN_REGION'>
       <put section='title'>List Ofbiz</put>
       <put section='content' content='/party/listofbiz.jsp'/>
   </define>

   <define id='showTest' region='MAIN_REGION'>
       <put section='title'>Show Ofbiz</put>
       <put section='content' content='/party/showofbiz.jsp'/>
   </define>

>=============================================================================

  现在就完成了,我们重新启动 Ofbiz,然后用 IE 访问:
  http://localhost:8080/partymgr/control/listtest,用admin/ofbiz 登录后就可以
看到我们刚才的工作成果了,你现在可以增加/删除/修改记录。


6.Ofbiz 通过 XML 来完成数据库操作(非常强大的功能)

  这是 Ofbiz 的一个非常强大的功能,可能通过简单的 XML 文件来完成数据增/删/改的处理,
这些处理在数据库应用中是非常多的,因为很多需要维护的数据,所以写程序也是最花时间的,
Ofbiz 把这些操作通过 XML 来完成,不能不说是一大革命---使我们不用写程序就可以完成大
部分处理,这是每个程序员都向往的终极目标。

  我们下面举例来讲述一下,处理的数据还是利用我们前面创建的 StudyCustomer,使用 XML
配置文件来完成前面程序 TestOfbiz.java 的大部分操作。

  在 c:\ofbiz\ofbiz\testOfbiz\com\geeyo\ofbiz 目录下创建文件 TestOfbizServices.xml,
该文件的内容如下:

>=================================================================

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE simple-methods PUBLIC "-//OFBiz//DTD Simple Methods//EN" "http://www.ofbiz.org/dtds/simple-methods.dtd">

<simple-methods>

   <!-- TestOfbiz methods -->
   <simple-method method-name="createNewRecord" short-description="Create a new record">
       <check-permission permission="STUDYCUSTOMER" action="_CREATE"><fail-message message="Security Error: to run createRecord you must have the STUDYCUSTOMER_CREATE permission"/></check-permission>        <check-errors/>

       <make-value entity-name="StudyCustomer" value-name="newEntity"/>
       <set-pk-fields map-name="parameters" value-name="newEntity"/>
       <set-nonpk-fields map-name="parameters" value-name="newEntity"/>

       <create-value value-name="newEntity"/>
   </simple-method>
   <simple-method method-name="updateRecord" short-description="Update a record">
       <check-permission permission="STUDYCUSTOMER" action="_UPDATE"><fail-message message="Security Error: to run updateRecord you must have the STUDYCUSTOMER_UPDATE permission"/></check-permission>

       <check-errors/>

       <make-value entity-name="StudyCustomer" value-name="lookupPKMap"/>
       <set-pk-fields map-name="parameters" value-name="lookupPKMap"/>
       <find-by-primary-key entity-name="StudyCustomer" map-name="lookupPKMap" value-name="lookedUpValue"/>
       <set-nonpk-fields map-name="parameters" value-name="lookedUpValue"/>
      
       <store-value value-name="lookedUpValue"/>
   </simple-method>

   <simple-method method-name="findRecord" short-description="lookup a record">
       <check-errors/>

       <make-value entity-name="StudyCustomer" value-name="lookupPKMap"/>
       <set-pk-fields map-name="parameters" value-name="lookupPKMap"/>
       <find-by-primary-key entity-name="StudyCustomer" map-name="lookupPKMap" value-name="edit_cust"/>
              <field-to-session field-name="edit_cust"/>
   </simple-method>

   <simple-method method-name="removeRecord" short-description="Delete a record">
       <check-permission permission="STUDYCUSTOMER" action="_DELETE"><fail-message message="Security Error: to run deleteRecord you must have the STUDYCUSTOMER_DELETE permission"/></check-permission>
       <check-errors/>

       <make-value entity-name="StudyCustomer" value-name="lookupPKMap"/>
       <set-pk-fields map-name="parameters" value-name="lookupPKMap"/>
       <find-by-primary-key entity-name="StudyCustomer" map-name="lookupPKMap" value-name="lookedUpValue"/>
       <remove-value value-name="lookedUpValue"/>
   </simple-method>

   <simple-method method-name="lookAllRecords" short-description="lookup suitable records">
       <check-errors/>
       <find-by-and entity-name="StudyCustomer" list-name="search_results"/>
              <field-to-session field-name="search_results"/>
   </simple-method>
  
</simple-methods>


>=================================================================

上面的 XML 基本是不用解释的,定义了

  createNewRecord
  updateRecord
  lookAllRecords
  removeRecord
  findRecord

  这几个方法,而且都有对用户权限的检查,这几个方法对应于前面 TestOfbiz.java 中的几个方法,
这样来做数据库操作显然比用 Java 程序写要简单得多,

  下面还需要在 controller.xml(具体文件得位置请参照前面的教程)更改一下 mapping 的设置,
更改如下,以前使用 TestOfbiz.java 时的配置我以注释的方式保留着以做参照:

>=================================================================

   <request-map uri="createTestForm">
      <description>Show the create form</description>
      <security https="false" auth="false"/>
      <response name="success" type="view" value="createTestForm"/>
   </request-map>

   <request-map uri="listtest">
      <description>List all records</description>
      <security https="false" auth="false"/>
         <event type="simple" path="com/geeyo/ofbiz/TestOfbizServices.xml" invoke="lookAllRecords" />
      <response name="success" type="view" value="listAllTest"/>
   </request-map>

   <request-map uri="showtest">
      <description>Show records</description>
      <security https="false" auth="false"/>
         <event type="simple" path="com/geeyo/ofbiz/TestOfbizServices.xml" invoke="findRecord" />
      <response name="success" type="view" value="showTest"/>
   </request-map>

   <request-map uri="createTest">
       <security https="true" auth="true"/>
       <event type="simple" path="com/geeyo/ofbiz/TestOfbizServices.xml" invoke="createNewRecord"/>
       <response name="success" type="request" value="listtest"/>
       <response name="error" type="view" value="createTestForm"/>
   </request-map>

   <request-map uri="updateTest">
      <description>update a record</description>
      <security https="false" auth="false"/>
      <event type="simple" path="com/geeyo/ofbiz/TestOfbizServices.xml" invoke="updateRecord" />
      <response name="success" type="request" value="listtest"/>
   </request-map>

   <request-map uri="removetest">
      <description>remove a record</description>
      <security https="false" auth="false"/>
      <event type="simple" path="com/geeyo/ofbiz/TestOfbizServices.xml" invoke="removeRecord" />
      <response name="success" type="request" value="listtest"/>
   </request-map>

   <view-map name="listAllTest" type="region"/>
   <view-map name="createTestForm" type="region"/>
   <view-map name="testofbiz" type="region"/>
   <view-map name="showTest" type="region"/>

>=================================================================

  配置该文件的方法请参照前面的教程,regions.xml 不需改动。

  配置完后请用前面讲过的方法访问 URL: http://localhost:8080/partymgr/control/listtest

  现在我们可以看到,Ofbiz 在 MVC 方面做得非常好,我们可以把后端的处理程序从 java 改
成用 XMl 控制,而其他部分(像 JSP)不需任何改动,这可以保证我们系统各部分的独立性。
分享到:
评论

相关推荐

    Ofbiz 入门教程

    ### Ofbiz 入门教程详解 #### 一、Ofbiz 概述与环境搭建 **1. Ofbiz 简介** Ofbiz 是一个开源的企业级应用框架,它提供了全面的功能来支持电子商务业务流程,包括销售、库存管理、订单处理等。作为 Apache 软件...

    ofbiz入门教程-初学者开发指南

    要开始使用 Ofbiz,首先需要安装 J2SDK 1.4。从 Java 官方网站下载并设置好JAVA_HOME环境变量。然后,从 Ofbiz 官方网站下载 Complete 包,解压后置于任意目录,例如 "C:\ofbiz"。在这个目录下,你会发现 catalina ...

    原创 Ofbiz 入门教程

    ### Ofbiz 入门教程详解 #### 一、Ofbiz 概述 Ofbiz(Open for Business)是一款开源的企业级商务应用系统,它利用了一系列优秀的开源项目如Tomcat、Ant、BeanShell、Jboss等,构建出了一个强大的系统平台。Ofbiz...

    OFBiz经典入门教程加速度编写

    《OFBiz经典入门教程加速度编写》是一篇针对开源企业应用框架OFBiz的入门教程,旨在帮助初学者快速掌握OFBiz的基本使用和开发技巧。OFBiz(Open For Business Project)是一个全面的企业级业务应用程序框架,它由...

    ofbiz开发者入门教程

    在本教程中,我们将深入探讨如何入门Apache Ofbiz的开发,这是一个开源的企业级应用框架,专为电子商务、供应链管理和企业资源规划等业务流程设计。Ofbiz提供了强大的组件化架构,使得开发者可以方便地构建和扩展...

    OFBiz教程_-_初学者的开发指南

    本教程旨在为初次接触OFBiz框架的开发者提供一个全面且易于理解的入门指南。OFBiz是一个开源的企业级电子商务框架,提供了完整的业务流程管理解决方案,包括订单处理、库存管理、客户服务支持等功能。通过本教程的...

    ofbiz入门实例(jiasudu制作)

    【标题】"Ofbiz入门实例(jiasudu制作)"是一个针对开源企业应用系统Ofbiz的实践教程,由博主jiasudu精心制作。这个实例教程旨在帮助初学者快速理解并上手Ofbiz,从而能够构建和管理自己的企业级应用程序。 【描述】...

    ofbiz开发教程

    **OFBiz开发教程** OFBiz,全称Open For Business Project,是一个开源的企业级应用套件,主要用于构建电子商务、供应链管理、客户关系管理等业务系统。本教程专为初学者设计,旨在帮助您快速掌握OFBiz的基本操作和...

    ofbiz开发入门总结

    Ofbiz社区提供了大量的文档和教程,包括官方的用户指南、开发者手册以及在线论坛,这些都是学习Ofbiz的宝贵资源。同时,阅读和理解Ofbiz的源码,能够帮助你更深入地理解其工作原理。 总结来说,Ofbiz是一个功能强大...

    ofbiz 入门+增删改查+实体

    Apache OFBiz是一个开源...总之,OFBiz入门涉及环境配置、组件和应用的创建、实体模型定义以及CRUD操作的实现。通过这些步骤,你可以开始开发基于OFBiz的业务应用程序,利用其强大的功能和灵活性来满足企业的各种需求。

    Ofbiz快速开发入门详解

    总的来说,《Ofbiz快速开发入门详解》是一本实用的教程,适合对Java Web开发感兴趣,希望掌握企业级应用框架的开发者。通过这本书,你不仅能理解Ofbiz的基本架构,还能学会如何利用Ofbiz快速开发出满足业务需求的...

    OFBIZ开发指南 英文2

    《OFBIZ开发指南》是一本专为开发者设计的详细教程,主要针对OFBIZ(Open For Business Project)这一开源企业应用平台。OFBIZ是一个基于Java的企业级应用框架,旨在提供一系列完整的业务功能,包括供应链管理、电子...

    OFBiz教程-初学者开发指南

    本教程专为初学者设计,旨在解决OFBiz入门过程中的常见问题,帮助开发者快速理解并掌握OFBiz的基本操作和开发流程。 **1. 创建组件定义文件** 在OFBiz中,每个组件都是一个独立的功能模块。首先,你需要在`hot-...

    OFBIZ10.04组件开发入门.doc

    **OFBIZ 10.04 组件开发入门** OFBIZ,全称为Open For Business Project,是一个基于Java的企业级应用框架,主要用于构建电子商务、供应链管理、CRM等复杂业务系统。本文档主要针对OFBIZ 10.04版本,通过讲解Region...

    ofbiz学习笔记(自学整理)

    文档中提供的"新建 Microsoft Word 文档 (2).docx"和"新建 Microsoft Word 文档.docx"可能是详细的学习笔记或者教程,涵盖了Ofbiz的基础概念、配置步骤、实例分析等内容。建议首先阅读这些文档,以便对Ofbiz有一个...

    ofbiz资料大全

    OFBiz开发快速入门.rar OFBiz-技术文档.rar OFBiz API中文版.rar Apache OFBiz Cookbook Sep 2010.rar Opentaps widget使用说明.rar OFBiz.Development.2008.rar Groovy中文教程.rar freemarker中文手册.rar ...

    OFBiz教程_初学者的开发指南

    OFBiz框架的强大之处在于其灵活性和扩展性,而本教程正是帮助开发者入门的关键资源之一。未来的学习过程中,可以进一步探索OFBiz提供的更多高级特性,如安全性管理、国际化支持等,以满足更复杂的应用需求。

    ofbiz10.04表结构

    描述中的"OFBiz开发者装备系列:0006"可能是指一系列针对OFBiz开发者的教程或者参考资料,编号0006可能表示这是第六个主题或章节。这一系列可能涵盖了OFBiz的各个方面,从基础概念到高级开发技巧,旨在帮助开发者更...

Global site tag (gtag.js) - Google Analytics