Ofbiz(http://www.ofbiz.org) ?Open Source 的商务Y件系l,充分利用了各优秀的的Open Source 目Q?br />?Tomcat, Ant, BeanShell, Jboss {,构徏了一个强大的pȝq_QOfbiz 已经完成了大部分商务cY件系l?br />都需要的部gQ像用户认证、工作流、商务规则处理等QOfbiz 的核心技术在?Entity EngineQ其他的lg基本?br />是基于它的。简单来?Entity Engine 的主要功能是数据库表创建、对象与数据表的映射、对象的查询{做了强?br />装Q你可以在一个简单的 XML 文g中定义数据库表结构,Ofbiz 会自动帮你在数据库徏表,q动态生成映对象,
你在E序中可以只考虑?Object 的处理,Ofbiz 会自动通过事务逻辑更新到数据库中。Ofbiz 宣称的优点之一是用
很少?Code 完成复杂的处理?
2.Ofbiz 下蝲与安?/p>
首先要安?J2SDK1.4Q到 http://java.sun.com 上下载,安装后设?JAVA_HOME 环境变量?J2SDK 的安装目录?/p>
讉K|站 http://www.ofbiz.orgQ上面有下蝲的连接,请选择 Complete 包,因ؓq个包中已经包含了运?Ofbiz
的所有东西,下蝲下来解开后就可以q行了?/p>
解开 Ofbiz 包到一个目录下Q假设是 “C:\ofbiz”,该目录下会?catalina ?ofbiz 两个目录Q?catalina
目录?Tomcat 的目录,Ofbiz 对其配置做了修改Qofbiz 目录?Ofbiz 的程序代码目录。在命o行状态下q入
“c:\ofbiz\catalina\bin?目录Q运行?ofbiz run”命令,可以启?OfbizQ启动后你可以用览器访?
“http://localhost:8080/ecommerce”,q可以访?Ofbiz 的电子商务模块,通过面上面的连接你可以讉K?br />其他模块?/p>
3.Ofbiz Schema 的创?
Ofbiz 应用入门Q?/p>
以一个实例说明,假设我们需要徏一个客戯料表Qv名ؓ StudyCustomerQ各个段分别如下Q?br /> StudyCustomer {
customerId Integer,
customerName String,
customerNote String,
}
我们来实现基本的数据操作---???查询Q具体步骤如下:
1.?XML 文g中定义数?SchemaQ?br /> 需要用C个文Ӟ一个是我们要徏的项目的 entitymodel_xxx.xml ?entityengine.xmlQ还?br /> entitygroup.xml,
entitymodel_xxx.xml 是需要我们自己创建的Q假设我们v名ؓ entitymodel_study.xmlQ放?
“c:\ofbiz\ofbiz\commonapp\entitydef”目录下Q?br /> entityengine.xml ?Ofbiz 已经有的Q放?“c:\ofbiz\commonapp\etc”目录下Q用来包含我?br /> 定义?entitymodel 文g?br /> entitygroup.xml 也是 Ofbiz 已经有的Q跟 engityengine.xml 在同一目录下,我们需要把我们?br /> Schema 定义加入到该文g?/p>
entitymodel_study.xml 文g的定义格式如下:
<!--================================================================================-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entitymodel PUBLIC "-//OFBiz//DTD Entity Model//EN"
"http://www.ofbiz.org/dtds/entitymodel.dtd">
<entitymodel>
<title>Entity of an Open For Business Project Component</title>
<description>None</description>
<copyright>Copyright (c) 2002 The Open For Business Project - www.ofbiz.org</copyright>
<author>None</author>
<version>1.0</version>
<!-- ========================================================= -->
<!-- ======================== Data Model ===================== -->
<!-- The modules in this file are as follows: -->
<!-- - org.ofbiz.commonapp.study -->
<!-- ========================================================= -->
<!-- ========================================================= -->
<!-- org.ofbiz.commonapp.study -->
<!-- ========================================================= -->
<entity entity-name="StudyCustomer"
package-name="org.ofbiz.commonapp.study"
title="Study Customer Entity">
<field name="customerId" type="id-ne"></field>
<field name="customerName" type="long-varchar"></field>
<field name="customerNote" type="long-varchar"></field>
<prim-key field="customerId"/>
</entity>
</entitymodel>
<!--================================================================================-->
q个 XML 文g中的 Tag 基本是看得明白的Q只?field ?type ?Ofbiz 已经预定义好的,q?br />是ؓ了保证数据库间的q移?/p>
?entityengine.xml 加入我们刚才定义的文Ӟ加入一行在合适的位置Q?br /> <resource loader="mainfile" location="entitymodel_study.xml"/>
具体攄位置我们可以通过查看 entityengine.xml 扑ֈQ里面已l有加好的其他文件?/p>
?entitygroup.xml 加入我们?Schema 定义Q在后面加入一?br /> <entity-group group="org.ofbiz.commonapp" entity="StudyCustomer" />
q样我们定义好?SchemaQ现在把 c:\ofbiz\commonapp\etc\entityengine.xml 拯?br /> c:\ofbiz\catalina\shared\classes 目录下,q点要切讎ͼ我以前就因ؓ没有拯Q最?Schema
怎么也创Z了?/p>
重新启动 OfbizQ访?URL: http://localhost:8080/webtoolsQ点d上方?"Login" 链接Q?br /> ?admin/ofbiz dQ登录进入后选择链接“Check/Update Database”,q时会出?Check ?FormQ?br /> 该表单可以只?Schema 是否改变Q默认的 GroupName 是“org.ofbiz.commonapp”,q个不需要变Q?br /> 点击“Check Only”按钮,Ofbiz 会检验变动情况,昄Z个完整的列表Q你可以查一下是否有我们刚徏?br /> "StudyCustomer"Q如果没有,可能是我们前面定义的有些问题Q检查一下再重新做?/p>
在检查到以后Q可以再选择“Check and Add Missing”,q是 Ofbiz 很强大的一个功能,你在 XML 中新
增了表,或在某个表中新增了段Q它会自动映到数据库中Q避免我们去直接操作数据库?/p>
现在已经完成?StudyCustomer Schema 的创?如果x验一下是否有表创建,我们可以用编辑器打开
c:\ofbiz\data\ofbiz.script Q在里面查询 CREATE TABLE StudyCustomer 的字P如果前面没有
问题Q我们可以找到的?/p>
4.如何使用已经定义?Schema
如何使用已经定义?Schema
Ofbiz 遵@ MVC 的设计模式,?View 端,?JSP 端主要?Ofbiz 定义?Tag 来显C或
提取数据QControl 是一?Controller ServletQ我们在 Controller Servlet ?URI mapping
配置文g中定义各 URL 应该指向什么程序,q样Q通过q个 mapping 配置文gQ可以保证我们各个页?br /> 及具体处理程序之间的独立性,例我们可以通过修改q个配置文g可以改变某?Form ?Post Action
?URLQ而不需要修改实际的 HTML ?JSP 代码?/p>
Ofbiz 中定义了 Regions 的概念,卛_一?HTML 面分成几个区域Q像 Top, Left, Right, Main
{,通过q些 Regions 我们可以方便的组?UI 界面Qƈ且可以方便改变各部分所处的位置Q如我们可以
把菜单很Ҏ的从上方Ud下方Q只需要改变一个配|文件。Regions cM?HTML 中的 FrameQ但它是
通过一个页面来l合界面QFrame 是通过几个面昄在不同的帧中QFrame 的控制比较复杂,而且需?br /> 改变相关的程序?/p>
?Ofbiz 中,我们可以直接?JSP 中操?Schema 定义?ObjectQ即我们刚定义的 StudyCustomerQ?br /> CZ如下Q?/p>
<%@ taglib uri="ofbizTags" prefix="ofbiz" %>
<%@ page import="java.util.*" %>
<%@ page import="org.ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>
<%@ page import="org.ofbiz.core.entity.*" %>
<jsp:useBean id="delegator" type="org.ofbiz.core.entity.GenericDelegator" scope="request" />
<jsp:useBean id="security" type="org.ofbiz.core.security.Security" scope="request" />
<%if(security.hasEntityPermission("PARTYMGR", "_VIEW", session)) {%>
<%
try {
delegator.create("StudyCustomer",
UtilMisc.toMap("customerId","1","customerName","Cust1","customerNote","Customer Note 1"));
Iterator custs =
UtilMisc.toIterator(delegator.findAll("StudyCustomer",UtilMisc.toList("customerId","customerName","customerNote")));
while(custs.hasNext())
{
GenericValue cust = (GenericValue)custs.next();
out.println(cust.getString("customerId"));
out.println(cust.getString("customerName"));
out.println(cust.getString("customerNote"));
}
} catch(Exception e)
{
out.println(e.getMessage());
}
%>
<%}else{%>
<h3>You do not have permission to view this page. ("PARTYMGR_VIEW" or "PARTYMGR_ADMIN" needed)</h3>
<%}%>
q段E序挺容易理解,先是通过 delegator 创徏一?ObjectQ该 Object 会?Ofbiz 自动同步?br />数据库中。然后通过 delegator ?findAll 取到所有已保存?ObjectQ最后通过一?Iterator 对象
昄出来?/p>
q个E序起名?testofbiz.jspQؓ单v见,我们攑ֈ Ofbiz 已有的一?Webapp 的目录下Q放?br />c:\ofbiz\ofbiz\partymgr\webapp\party 目录下。然后我们需要修改两个配|文Ӟcontroller.xml
?regions.xmlQ这两个文g是我们上面提到?mapping ?regions 配置文g?/p>
q两个文仉在:c:\ofbiz\ofbiz\partymgr\webapp\WEB-INF 下,?controller.xml 中加入下?/p>
<request-map uri="testofbiz">
<description>Test Ofbiz</description>
<security https="false" auth="false"/>
<response name="success" type="view" value="testofbiz"/>
</request-map>
?br /> <view-map name="testofbiz" type="region"/>
加入位置请参?controller.xml 中已l有的配|。在 regions.xml 中加入:
<define id='testofbiz' region='MAIN_REGION'>
<put section='title'>Test Ofbiz</put>
<put section='content' content='/party/testofbiz.jsp'/>
</define>
具体加入位置请参考已有的配置?/p>
配置完后Q重新启?ofbizQ然后访?URL:
http://localhost:8080/partymgr/control/testofbiz
׃我们?testofbiz.jsp E序中用了 Ofbiz 的安全控制机Ӟpȝ会提C现在没有访?br /> 权限Q需要登录,点击双的“Login??admin/ofbiz d后会看到我们E序 testofbiz.jsp
的运行结果。如果需要增加新记录Q请修改
UtilMisc.toMap("customerId","1","customerName","Cust1","customerNote","Customer Note 1"));
中的各个D늚|然后再访?http://localhost:8080/partymgr/control/testofbizQ如果不修改
而直接访问那?URL Ӟpȝ会提C?Primary key 冲突?/p>
5.按照昄与逻辑分离的原则?Schema:
上篇讲了如何?JSP 中用创建的 Schema 对象Q这ơ我们来讲述一下如何把E序
逻辑攑ֈ JavaBeans 中,把显C处理放?JSP 中,q?controller.xml 两
部分整合h?/p>
首先我们来创Z?JavaBeansQ来完成Add/Get/Delete/Update Schema 对象
的操作,E序文g名ؓ TestOfbiz.javaQ放|在
c:\ofbiz\ofbiz\testOfbiz\com\geeyo\ofbiz 目录? 具体E序如下Q?/p>
>=================================================================
package com.geeyo.ofbiz;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
import org.ofbiz.core.util.*;
import org.ofbiz.core.entity.*;
import org.ofbiz.core.service.*;
import org.ofbiz.core.security.*;
import org.ofbiz.core.stats.*;
public class TestOfbiz
{
public static void main(String[] args)
throws Exception
{
GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
delegator.create("StudyCustomer",UtilMisc.toMap("customerId","3","customerName","Kane3","customerNote","This is test customer.3"));
Iterator custs = UtilMisc.toIterator(delegator.findAll("StudyCustomer",UtilMisc.toList("customerId","customerName","customerNote")));
while(custs.hasNext())
{
GenericValue cust = (GenericValue)custs.next();
System.out.println(cust.getString("customerId"));
System.out.println(cust.getString("customerName"));
System.out.println(cust.getString("customerNote"));
}
}
public static String createNewRecord(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
Map paras = UtilMisc.getParameterMap(request);
GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
delegator.create("StudyCustomer",paras);
return "success";
}
public static String lookAllRecords(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
Iterator custs = UtilMisc.toIterator(delegator.findAll("StudyCustomer",UtilMisc.toList("customerId","customerName","customerNote")));
Collection col = new ArrayList();
while(custs.hasNext())
{
GenericValue cust = (GenericValue)custs.next();
col.add(cust);
}
request.getSession().setAttribute("search_results",col);
return "success";
}
public static String findRecord(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
String id = (String)request.getParameter("customerId");
GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
try {
GenericValue cust = delegator.findByPrimaryKey("StudyCustomer",UtilMisc.toMap("customerId",id));
request.getSession().setAttribute("edit_cust",cust);
} catch (GenericEntityException gee) {
Debug.logWarning(gee);
}
return "success";
}
public static String updateRecord(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
Map paras = UtilMisc.getParameterMap(request);
GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
GenericValue cust = delegator.findByPrimaryKey("StudyCustomer",UtilMisc.toMap("customerId",paras.get("customerId")));
cust.setNonPKFields(paras);
cust.store();
request.getSession().setAttribute("edit_cust",cust);
return "success";
}
public static String removeRecord(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
String strId = request.getParameter("id");
GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
GenericValue cust = delegator.findByPrimaryKey("StudyCustomer",UtilMisc.toMap("customerId",strId));
cust.remove();
return "success";
}
}
>=================================================================
E序中的处理大部分可以看懂的Q其中有个功能,?br /> Map paras = UtilMisc.getParameterMap(request);
q是 Ofbiz 的一个有但非常有用的功能,它是?request 中各D늚名字和值映到一?Map
对象中,然后使用
cust.setNonPKFields(paras);
可以赋l?Object cust 的各个段Q免了我们?request.getParameter("name")来取各个
|在值很多的时候这个功能可以大大减冗余代码量?/p>
基本E序的逻辑是这LQ?br /> 1.?request d传来的?br />?2.使用 delegator 来处理,Add/Update/Delete/Query
3.返回结果放?Session 中传l?JSP
我做了个 Ant build.xml 文g可以帮助~译Q把q个文g攑֜:
c:\ofbiz\ofbiz\testOfbiz\ 目录下,然后在命令行H口下进入该目录Q敲?ant
来编译(需要保证已l安?AntQ,~译后的 .class 会放?br /> c:\ofbiz\ofbiz\testOfbiz\com\geeyo\ofbiz 下,
拯 c:\ofbiz\ofbiz\testofbiz\com 目录?c:\ofbiz\ofbiz\partymgr\webapp\WEB-INF\classes
目录下?/p>
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 E序QJSP E序全部攑֜
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>
<%}%>
>=============================================================================
上面E序中需要说明的?br /> <ofbiz:if name="search_results">
?br /> <ofbiz:iterator name="cust" property="search_results">Q?br />
<ofbiz:if name="search_results"> 是用来检验在 session ?pageContext 对象
中是否包?search_results 对象Q该对象是由我们的程序放?session 中的?br /> <ofbiz:iterator name="cust" property="search_results"> 是用来@环读取对?br />search_resultsQ是?Collection 对象Q中存储的各对象Qƈ赋给custQ然后在循环?br />中,我们可以用 cust 对象来读取各个段的g?/p>
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>
<%}%>
>=============================================================================
q个E序很容易理解,需要注意的是每个文本框的名字,要跟 Schema StudyCustomer 的各
个段一_以ɽE序中跟Ҏ处理?/p>
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>
<%}%>
>=============================================================================
q个E序中,主要是通过
<ofbiz:entityfield attribute="edit_cust" field="customerId"/>
把取到的对象的段昄出来, 对象 edit_cust 是我们在E序中取到ƈ攑ֈ session 中的?/p>
下面我们来配|?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>
>=============================================================================
现在完成了Q我们重新启?OfbizQ然后用 IE 讉KQ?br /> http://localhost:8080/partymgr/control/listtestQ用admin/ofbiz d后就可以
看到我们刚才的工作成果了Q你现在可以增加/删除/修改记录?/p>
6.Ofbiz 通过 XML 来完成数据库操作Q非常强大的功能Q?/p>
q是 Ofbiz 的一个非常强大的功能Q可能通过单的 XML 文g来完成数据增/?改的处理Q?br />q些处理在数据库应用中是非常多的Q因为很多需要维护的数据Q所以写E序也是最花时间的Q?br />Ofbiz 把这些操作通过 XML 来完成,不能不说是一大革?--使我们不用写E序可以完成大
部分处理Q这是每个程序员都向往的终极目标?/p>
我们下面举例来讲qC下,处理的数据还是利用我们前面创建的 StudyCustomerQ?XML
配置文g来完成前面程?TestOfbiz.java 的大部分操作?/p>
?c:\ofbiz\ofbiz\testOfbiz\com\geeyo\ofbiz 目录下创建文?TestOfbizServices.xmlQ?br />该文件的内容如下Q?/p>
>=================================================================
<?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 基本是不用解释的Q定义了
createNewRecord
updateRecord
lookAllRecords
removeRecord
findRecord
q几个方法,而且都有对用h限的查,q几个方法对应于前面 TestOfbiz.java 中的几个ҎQ?br />q样来做数据库操作显然比?Java E序写要单得多,
下面q需要在 controller.xmlQ具体文件得位置请参照前面的教程Q更改一?mapping 的设|,
更改如下Q以前?TestOfbiz.java 时的配置我以注释的方式保留着以做参照Q?/p>
>=================================================================
<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"/>
>=================================================================
配置该文件的Ҏ请参照前面的教程Qregions.xml 不需改动?/p>
配置完后L前面讲过的方法访?URL: http://localhost:8080/partymgr/control/listtest
现在我们可以看到QOfbiz ?MVC 斚w做得非常好,我们可以把后端的处理E序?java ?br />成用 XMl 控制Q而其他部分(?JSPQ不需M改动Q这可以保证我们pȝ各部分的独立性?/p>