ofbiz 开发

ofbiz 开发1 Ofbiz 介绍 Ofbiz http www ofbiz org 是 OpenSource 的商务软件系统 充分利用了各优秀的的 OpenSource 项目 像 Tomcat Ant BeanShell Jboss 等 构建了一个强大的系统平台 Ofbiz 已经完成了大部分商务类软件系统都需要的部件 像用户认证 工作流 商务规则处理等 Ofbiz 的核心技术在

You do not have permission to view this page. (“PARTYMGR_VIEW” or “PARTYMGR_ADMIN” needed)

<% }%>

这段程序挺容易理解,先是通过 delegator 创建一个 Object,该 Object 将会由 Ofbiz 自动同步到
数据库中。然后通过 delegator 的 findAll 取到所有已保存的 Object,最后通过一个 Iterator 对象
显示出来。

这个程序起名为 testofbiz.jsp,为简单起见,我们放到 Ofbiz 已有的一个 Webapp 的目录下,放到
c:ofbizofbizpartymgrwebappparty 目录下。然后我们需要修改两个配置文件:controller.xml
和 regions.xml,这两个文件就是我们上面提到的 mapping 和 regions 配置文件。

这两个文件都在:c:ofbizofbizpartymgrwebappWEB-INF 下,在 controller.xml 中加入下面





Test Ofbiz













加入位置请参照 controller.xml 中已经有的配置。在 regions.xml 中加入:



Test Ofbiz




具体加入位置请参考已有的配置。

配置完后,重新启动 ofbiz,然后访问 URL:
http://localhost:8080/partymgr/control/testofbiz

由于我们在 testofbiz.jsp 程序中使用了 Ofbiz 的安全控制机制,系统会提示现在没有访问
权限,需要登录,点击右边的“Login” 用 admin/ofbiz 登录后会看到我们程序 testofbiz.jsp
的运行结果。如果需要增加新记录,请修改

UtilMisc.toMap(“customerId”,”1″,”customerName”,”Cust1″,”customerNote”,”Customer Note 1″));

中的各个段的值,然后再访问 http://localhost:8080/partymgr/control/testofbiz,如果不修改
而直接访问那个 URL 时,系统会提示 Primary key 冲突。

5.按照显示与逻辑分离的原则使用 Schema:

上篇讲了如何在 JSP 中使用创建的 Schema 对象,这次我们来讲述一下如何把程序
逻辑放到 JavaBeans 中,把显示处理放到 JSP 中,并使用 controller.xml 将两
部分整合起来。

首先我们来创建一个 JavaBeans,来完成Add/Get/Delete/Update Schema 对象
的操作,程序文件名为 TestOfbiz.java,放置在
c:ofbizofbiz estOfbizcomgeeyoofbiz 目录下, 具体程序如下:

>=================================================================
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”;
}

}

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

程序中的处理大部分可以看懂的,其中有个功能,是
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:ofbizofbiz estOfbiz 目录下,然后在命令行窗口下进入该目录,敲入 ant
来编译(需要保证已经安装 Ant),编译后的 .class 会放在
c:ofbizofbiz estOfbizcomgeeyoofbiz 下,
拷贝 c:ofbizofbiz estofbizcom 目录到 c:ofbizofbizpartymgrwebappWEB-INFclasses
目录下。

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





Test ofbiz












































description=”compile the source ” >

















description=”generate the distribution” >







description=”clean up” >




























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

然后我们来创建 JSP 程序,JSP 程序全部放在
c:ofbizofbizpartymgrwebappparty 下面

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

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

<%@ page import="java dot util.*, org.ofbiz.core.service.ModelService" %>
<% at page import="org dot ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>
<% at page import="org dot ofbiz.core.entity.*" %>



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












Id Name Note
” class=”buttontext”>[Edit]
” class=”buttontext” οnclick=”return confirmDelete()”>[Remove]









是用来检验在 session 或 pageContext 对象

中是否包含 search_results 对象,该对象是由我们的程序放到 session 中的。


是用来循环读取对象

search_results(是个 Collection 对象)中存储的各对象,并赋给cust,然后在循环体
中,我们就可以用 cust 对象来读取各个段的值了。

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

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

<%@ page import="java dot util.*, org.ofbiz.core.service.ModelService" %>
<% at page import="org dot ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>
<% at page import="org dot ofbiz.core.entity.*" %>




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

Id
Name
Note


















<% }else{ %>

You do not have permission to view this page. (“PARTYMGR_VIEW” or “PARTYMGR_ADMIN” needed)

<% }%>
>=============================================================================

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

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

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

<%@ page import="java dot util.*, org.ofbiz.core.service.ModelService" %>
<% at page import="org dot ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>
<% at page import="org dot ofbiz.core.entity.*" %>




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

Id “> Name “> Note “>


















<% }else{ %>

You do not have permission to view this page. (“PARTYMGR_VIEW” or “PARTYMGR_ADMIN” needed)

<% }%>

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

这个程序中,主要是通过


把取到的对象的段显示出来, 对象 edit_cust 是我们在程序中取到并放到 session 中的。

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





Show the create form














Test Ofbiz














List all records

















Show records

































update a record

















remove a record


















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

在 regions.xml 中加入:
>=============================================================================



Create Ofbiz





List Ofbiz





Show Ofbiz



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

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

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

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

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

在 c:ofbizofbiz estOfbizcomgeeyoofbiz 目录下创建文件 TestOfbizServices.xml,
该文件的内容如下:

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


” target=”_blank”> http://www.ofbiz.org/dtds/simple-methods.dtd”>




















































































































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

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

createNewRecord
updateRecord
lookAllRecords
removeRecord
findRecord

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

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

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





Show the create form














List all records

















Show records

































update a record

















remove a record



















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

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

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

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

http://www.cnpoint.com/mvnforum/mvnforum/viewthread?thread=65









































































































































































































































































































































































































版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/203153.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午10:27
下一篇 2026年3月19日 下午10:28


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号