- 浏览: 72445 次
- 性别:
- 来自: 杭州
-
最新评论
-
Arron.li:
我现在正在研究使用jboss cache,结合spring和h ...
pojocache在tomcat下的配置 -
heidan:
还算提出了点自己的思想,借鉴一下
EXT提交服务器的三种方式(转) -
lkjust08:
你的action中是怎么做处理的?
EXT提交服务器的三种方式(转) -
seeksdream:
...
EXT提交服务器的三种方式(转) -
浴盆儿:
你这是哪个版本的啊,我怎么没找到COMPLETE包呢,
原创 Ofbiz 入门教程(转)
(刚接触,省的google,所以转来)
1.Ofbiz 介绍:
Ofbiz(http://www.ofbiz.org) 是 Open Source 的商务软件系统,充分利用了各优秀的的Open Source 项目,
像 Tomcat, Ant, BeanShell, Jboss 等,构建了一个强大的系统平台,Ofbiz 已经完成了大部分商务类软件系统
都需要的部件,像用户认证、工作流、商务规则处理等,Ofbiz 的核心技术在于 Entity Engine,其他的组件基本都
是基于它的。简单来说 Entity Engine 的主要功能是将数据库表创建、对象与数据表的映射、对象的查询等做了强大
封装,你可以在一个简单的 XML 文件中定义数据库表结构,Ofbiz 会自动帮你在数据库建表,并动态生成映射对象,
你在程序中可以只考虑对 Object 的处理,Ofbiz 会自动通过事务逻辑更新到数据库中。Ofbiz 宣称的优点之一是用
很少的 Code 完成复杂的处理。
2.Ofbiz 下载与安装
首先要安装 J2SDK1.4,到 http://java.sun.com 上下载,安装后设定 JAVA_HOME 环境变量为 J2SDK 的安装目录。
访问网站 http://www.ofbiz.org,上面有下载的连接,请选择 Complete 包,因为这个包中已经包含了运行 Ofbiz
的所有东西,下载下来解开后就可以运行了。
解开 Ofbiz 包到一个目录下,假设是 “C:\ofbiz”,该目录下将会有 catalina 和 ofbiz 两个目录, catalina
目录是 Tomcat 的目录,Ofbiz 对其配置做了修改,ofbiz 目录是 Ofbiz 的程序代码目录。在命令行状态下进入
“c:\ofbiz\catalina\bin” 目录,运行“ ofbiz run”命令,就可以启动 Ofbiz,启动后你可以用浏览器访问
“http://localhost:8080/ecommerce”,这可以访问 Ofbiz 的电子商务模块,通过页面上面的连接你可以访问到
其他模块。
3.Ofbiz Schema 的创建
Ofbiz 应用入门:
以一个实例说明,假设我们需要建一个客户资料表,起名为 StudyCustomer,各个段分别如下:
StudyCustomer {
customerId Integer,
customerName String,
customerNote String,
}
我们来实现基本的数据操作---增/删/改/查询,具体步骤如下:
1.在 XML 文件中定义数据 Schema:
需要用到三个文件,一个是我们要建的项目的 entitymodel_xxx.xml 和 entityengine.xml,还有
entitygroup.xml,
entitymodel_xxx.xml 是需要我们自己创建的,假设我们起名为 entitymodel_study.xml,放在
“c:\ofbiz\ofbiz\commonapp\entitydef”目录下,
entityengine.xml 是 Ofbiz 已经有的,放在 “c:\ofbiz\commonapp\etc”目录下,用来包含我们
定义的 entitymodel 文件。
entitygroup.xml 也是 Ofbiz 已经有的,跟 engityengine.xml 在同一目录下,我们需要把我们的
Schema 定义加入到该文件中
entitymodel_study.xml 文件的定义格式如下:
<!--================================================================================-->
<?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>
<!--================================================================================-->
这个 XML 文件中的 Tag 基本是看得明白的,只是 field 的 type 是 Ofbiz 已经预定义好的,这
是为了保证数据库间的迁移。
在 entityengine.xml 加入我们刚才定义的文件,加入一行在合适的位置:
<resource loader="mainfile" location="entitymodel_study.xml"/>
具体放的位置我们可以通过查看 entityengine.xml 找到,里面已经有加好的其他文件。
在 entitygroup.xml 加入我们的 Schema 定义,在后面加入一行
<entity-group group="org.ofbiz.commonapp" entity="StudyCustomer" />
这样我们就定义好了 Schema,现在把 c:\ofbiz\commonapp\etc\entityengine.xml 拷贝到
c:\ofbiz\catalina\shared\classes 目录下,这点要切记,我以前就因为没有拷贝,最后 Schema
怎么也创建不了。
重新启动 Ofbiz,访问 URL: http://localhost:8080/webtools,点击右上方的 "Login" 链接,
用 admin/ofbiz 登录,登录进入后选择链接“Check/Update Database”,这时会出现 Check 的 Form,
该表单可以只检验 Schema 是否改变,默认的 GroupName 是“org.ofbiz.commonapp”,这个不需要变,
点击“Check Only”按钮,Ofbiz 会检验变动情况,显示出一个完整的列表,你可以查一下是否有我们刚建的
"StudyCustomer",如果没有,可能是我们前面定义的有些问题,检查一下再重新做。
在检查到以后,可以再选择“Check and Add Missing”,这是 Ofbiz 很强大的一个功能,你在 XML 中新
增了表,或在某个表中新增了段,它会自动映射到数据库中,避免我们去直接操作数据库。
现在已经完成了 StudyCustomer Schema 的创建,如果想检验一下是否有表创建,我们可以用编辑器打开
c:\ofbiz\data\ofbiz.script ,在里面查询 CREATE TABLE StudyCustomer 的字样,如果前面没有
问题,我们可以找到的。
4.如何使用已经定义的 Schema
如何使用已经定义的 Schema
Ofbiz 遵循 MVC 的设计模式,在 View 端,即 JSP 端主要使用 Ofbiz 定义的 Tag 来显示或
提取数据,Control 是一个 Controller Servlet,我们在 Controller Servlet 的 URI mapping
配置文件中定义各 URL 应该指向什么程序,这样,通过这个 mapping 配置文件,可以保证我们各个页面
及具体处理程序之间的独立性,例我们可以通过修改这个配置文件就可以改变某个 Form 的 Post Action
的 URL,而不需要修改实际的 HTML 或 JSP 代码。
Ofbiz 中定义了 Regions 的概念,即将一个 HTML 页面分成几个区域,像 Top, Left, Right, Main
等,通过这些 Regions 我们可以方便的组合 UI 界面,并且可以方便改变各部分所处的位置,如我们可以
把菜单很容易的从上方移到下方,只需要改变一个配置文件。Regions 类似于 HTML 中的 Frame,但它是
通过一个页面来组合界面,Frame 是通过几个页面显示在不同的帧中,Frame 的控制比较复杂,而且需要
改变相关的程序。
在 Ofbiz 中,我们可以直接在 JSP 中操作 Schema 定义的 Object,即我们刚定义的 StudyCustomer,
示例如下:
<%@ 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>
<%}%>
这段程序挺容易理解,先是通过 delegator 创建一个 Object,该 Object 将会由 Ofbiz 自动同步到
数据库中。然后通过 delegator 的 findAll 取到所有已保存的 Object,最后通过一个 Iterator 对象
显示出来。
这个程序起名为 testofbiz.jsp,为简单起见,我们放到 Ofbiz 已有的一个 Webapp 的目录下,放到
c:\ofbiz\ofbiz\partymgr\webapp\party 目录下。然后我们需要修改两个配置文件:controller.xml
和 regions.xml,这两个文件就是我们上面提到的 mapping 和 regions 配置文件。
这两个文件都在:c:\ofbiz\ofbiz\partymgr\webapp\WEB-INF 下,在 controller.xml 中加入下面
<request-map uri="testofbiz">
<description>Test Ofbiz</description>
<security https="false" auth="false"/>
<response name="success" type="view" value="testofbiz"/>
</request-map>
和
<view-map name="testofbiz" type="region"/>
加入位置请参照 controller.xml 中已经有的配置。在 regions.xml 中加入:
<define id='testofbiz' region='MAIN_REGION'>
<put section='title'>Test Ofbiz</put>
<put section='content' content='/party/testofbiz.jsp'/>
</define>
具体加入位置请参考已有的配置。
配置完后,重新启动 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:\ofbiz\ofbiz\testOfbiz\com\geeyo\ofbiz 目录下, 具体程序如下:
>=================================================================
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:\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)不需任何改动,这可以保证我们系统各部分的独立性。
1.Ofbiz 介绍:
Ofbiz(http://www.ofbiz.org) 是 Open Source 的商务软件系统,充分利用了各优秀的的Open Source 项目,
像 Tomcat, Ant, BeanShell, Jboss 等,构建了一个强大的系统平台,Ofbiz 已经完成了大部分商务类软件系统
都需要的部件,像用户认证、工作流、商务规则处理等,Ofbiz 的核心技术在于 Entity Engine,其他的组件基本都
是基于它的。简单来说 Entity Engine 的主要功能是将数据库表创建、对象与数据表的映射、对象的查询等做了强大
封装,你可以在一个简单的 XML 文件中定义数据库表结构,Ofbiz 会自动帮你在数据库建表,并动态生成映射对象,
你在程序中可以只考虑对 Object 的处理,Ofbiz 会自动通过事务逻辑更新到数据库中。Ofbiz 宣称的优点之一是用
很少的 Code 完成复杂的处理。
2.Ofbiz 下载与安装
首先要安装 J2SDK1.4,到 http://java.sun.com 上下载,安装后设定 JAVA_HOME 环境变量为 J2SDK 的安装目录。
访问网站 http://www.ofbiz.org,上面有下载的连接,请选择 Complete 包,因为这个包中已经包含了运行 Ofbiz
的所有东西,下载下来解开后就可以运行了。
解开 Ofbiz 包到一个目录下,假设是 “C:\ofbiz”,该目录下将会有 catalina 和 ofbiz 两个目录, catalina
目录是 Tomcat 的目录,Ofbiz 对其配置做了修改,ofbiz 目录是 Ofbiz 的程序代码目录。在命令行状态下进入
“c:\ofbiz\catalina\bin” 目录,运行“ ofbiz run”命令,就可以启动 Ofbiz,启动后你可以用浏览器访问
“http://localhost:8080/ecommerce”,这可以访问 Ofbiz 的电子商务模块,通过页面上面的连接你可以访问到
其他模块。
3.Ofbiz Schema 的创建
Ofbiz 应用入门:
以一个实例说明,假设我们需要建一个客户资料表,起名为 StudyCustomer,各个段分别如下:
StudyCustomer {
customerId Integer,
customerName String,
customerNote String,
}
我们来实现基本的数据操作---增/删/改/查询,具体步骤如下:
1.在 XML 文件中定义数据 Schema:
需要用到三个文件,一个是我们要建的项目的 entitymodel_xxx.xml 和 entityengine.xml,还有
entitygroup.xml,
entitymodel_xxx.xml 是需要我们自己创建的,假设我们起名为 entitymodel_study.xml,放在
“c:\ofbiz\ofbiz\commonapp\entitydef”目录下,
entityengine.xml 是 Ofbiz 已经有的,放在 “c:\ofbiz\commonapp\etc”目录下,用来包含我们
定义的 entitymodel 文件。
entitygroup.xml 也是 Ofbiz 已经有的,跟 engityengine.xml 在同一目录下,我们需要把我们的
Schema 定义加入到该文件中
entitymodel_study.xml 文件的定义格式如下:
<!--================================================================================-->
<?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>
<!--================================================================================-->
这个 XML 文件中的 Tag 基本是看得明白的,只是 field 的 type 是 Ofbiz 已经预定义好的,这
是为了保证数据库间的迁移。
在 entityengine.xml 加入我们刚才定义的文件,加入一行在合适的位置:
<resource loader="mainfile" location="entitymodel_study.xml"/>
具体放的位置我们可以通过查看 entityengine.xml 找到,里面已经有加好的其他文件。
在 entitygroup.xml 加入我们的 Schema 定义,在后面加入一行
<entity-group group="org.ofbiz.commonapp" entity="StudyCustomer" />
这样我们就定义好了 Schema,现在把 c:\ofbiz\commonapp\etc\entityengine.xml 拷贝到
c:\ofbiz\catalina\shared\classes 目录下,这点要切记,我以前就因为没有拷贝,最后 Schema
怎么也创建不了。
重新启动 Ofbiz,访问 URL: http://localhost:8080/webtools,点击右上方的 "Login" 链接,
用 admin/ofbiz 登录,登录进入后选择链接“Check/Update Database”,这时会出现 Check 的 Form,
该表单可以只检验 Schema 是否改变,默认的 GroupName 是“org.ofbiz.commonapp”,这个不需要变,
点击“Check Only”按钮,Ofbiz 会检验变动情况,显示出一个完整的列表,你可以查一下是否有我们刚建的
"StudyCustomer",如果没有,可能是我们前面定义的有些问题,检查一下再重新做。
在检查到以后,可以再选择“Check and Add Missing”,这是 Ofbiz 很强大的一个功能,你在 XML 中新
增了表,或在某个表中新增了段,它会自动映射到数据库中,避免我们去直接操作数据库。
现在已经完成了 StudyCustomer Schema 的创建,如果想检验一下是否有表创建,我们可以用编辑器打开
c:\ofbiz\data\ofbiz.script ,在里面查询 CREATE TABLE StudyCustomer 的字样,如果前面没有
问题,我们可以找到的。
4.如何使用已经定义的 Schema
如何使用已经定义的 Schema
Ofbiz 遵循 MVC 的设计模式,在 View 端,即 JSP 端主要使用 Ofbiz 定义的 Tag 来显示或
提取数据,Control 是一个 Controller Servlet,我们在 Controller Servlet 的 URI mapping
配置文件中定义各 URL 应该指向什么程序,这样,通过这个 mapping 配置文件,可以保证我们各个页面
及具体处理程序之间的独立性,例我们可以通过修改这个配置文件就可以改变某个 Form 的 Post Action
的 URL,而不需要修改实际的 HTML 或 JSP 代码。
Ofbiz 中定义了 Regions 的概念,即将一个 HTML 页面分成几个区域,像 Top, Left, Right, Main
等,通过这些 Regions 我们可以方便的组合 UI 界面,并且可以方便改变各部分所处的位置,如我们可以
把菜单很容易的从上方移到下方,只需要改变一个配置文件。Regions 类似于 HTML 中的 Frame,但它是
通过一个页面来组合界面,Frame 是通过几个页面显示在不同的帧中,Frame 的控制比较复杂,而且需要
改变相关的程序。
在 Ofbiz 中,我们可以直接在 JSP 中操作 Schema 定义的 Object,即我们刚定义的 StudyCustomer,
示例如下:
<%@ 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>
<%}%>
这段程序挺容易理解,先是通过 delegator 创建一个 Object,该 Object 将会由 Ofbiz 自动同步到
数据库中。然后通过 delegator 的 findAll 取到所有已保存的 Object,最后通过一个 Iterator 对象
显示出来。
这个程序起名为 testofbiz.jsp,为简单起见,我们放到 Ofbiz 已有的一个 Webapp 的目录下,放到
c:\ofbiz\ofbiz\partymgr\webapp\party 目录下。然后我们需要修改两个配置文件:controller.xml
和 regions.xml,这两个文件就是我们上面提到的 mapping 和 regions 配置文件。
这两个文件都在:c:\ofbiz\ofbiz\partymgr\webapp\WEB-INF 下,在 controller.xml 中加入下面
<request-map uri="testofbiz">
<description>Test Ofbiz</description>
<security https="false" auth="false"/>
<response name="success" type="view" value="testofbiz"/>
</request-map>
和
<view-map name="testofbiz" type="region"/>
加入位置请参照 controller.xml 中已经有的配置。在 regions.xml 中加入:
<define id='testofbiz' region='MAIN_REGION'>
<put section='title'>Test Ofbiz</put>
<put section='content' content='/party/testofbiz.jsp'/>
</define>
具体加入位置请参考已有的配置。
配置完后,重新启动 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:\ofbiz\ofbiz\testOfbiz\com\geeyo\ofbiz 目录下, 具体程序如下:
>=================================================================
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:\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的安装配置步骤、基础环境设置、开发环境搭建、第一个应用程序的创建等初级主题。原创性表明这是作者个人或团队基于实践经验编写的,因此可能包含了更多实战...
内容概要:本文详细介绍了如何利用威纶通触摸屏及其配套软件EasyBuilder Pro构建一个水箱液位控制的PID仿真程序。主要内容涵盖触摸屏界面设计、PID算法实现、通信配置以及仿真模型搭建等方面。文中不仅提供了具体的代码示例,还分享了许多调试经验和优化技巧,如抗积分饱和处理、通信同步设置等。此外,作者还强调了实际应用中的注意事项,例如参数范围限制、突发情况模拟等。 适合人群:从事工业自动化领域的工程师和技术人员,尤其是对PID控制器有一定了解并希望深入掌握其实际应用的人群。 使用场景及目标:适用于需要进行水箱液位控制系统设计、调试和优化的工作环境。主要目标是帮助读者理解和掌握PID控制的基本原理及其在实际工程项目中的具体实现方法。 其他说明:附带完整的工程文件可供下载,便于读者快速上手实践。文中提到的所有代码片段均经过实际验证,确保可靠性和实用性。
内容概要:《2024年中国城市低空经济发展指数报告》由36氪研究院发布,指出低空经济作为新质生产力的代表,已成为中国经济新的增长点。报告从发展环境、资金投入、创新能力、基础支撑和发展成效五个维度构建了综合指数评价体系,评估了全国重点城市的低空经济发展状况。北京和深圳在总指数中名列前茅,分别以91.26和84.53的得分领先,展现出强大的资金投入、创新能力和基础支撑。低空经济主要涉及无人机、eVTOL(电动垂直起降飞行器)和直升机等产品,广泛应用于农业、物流、交通、应急救援等领域。政策支持、市场需求和技术进步共同推动了低空经济的快速发展,预计到2026年市场规模将突破万亿元。 适用人群:对低空经济发展感兴趣的政策制定者、投资者、企业和研究人员。 使用场景及目标:①了解低空经济的定义、分类和发展驱动力;②掌握低空经济的主要应用场景和市场规模预测;③评估各城市在低空经济发展中的表现和潜力;④为政策制定、投资决策和企业发展提供参考依据。 其他说明:报告强调了政策监管、产业生态建设和区域融合错位的重要性,提出了加强法律法规建设、人才储备和基础设施建设等建议。低空经济正加速向网络化、智能化、规模化和集聚化方向发展,各地应找准自身比较优势,实现差异化发展。
内容概要:本文详细介绍了多智能体协同编队控制的技术原理及其Python实现。首先通过生动形象的例子解释了编队控制的核心概念,如一致性算法、虚拟结构法、预测补偿等。接着深入探讨了编队形状的设计方法,包括如何利用虚拟结构法生成特定编队形状,并讨论了通信质量和参数调试的重要性。此外,还涉及了避障策略、动态权重分配以及故障检测等实际应用中的挑战和解决方案。最后,通过具体实例展示了如何将理论应用于实际项目中,如无人机编队表演、自动驾驶车队等。 适用人群:对多智能体系统、编队控制感兴趣的科研人员、工程师及高校师生。 使用场景及目标:适用于研究和开发多智能体协同编队控制系统的场景,旨在帮助读者理解并掌握相关技术和实现方法,提高系统的稳定性和可靠性。 其他说明:文中不仅提供了详细的代码示例,还分享了许多实践经验和技术细节,有助于读者更好地理解和应用这些技术。同时强调了参数调试、通信质量、预测补偿等方面的关键因素对于系统性能的影响。
内容概要:本文详细介绍了名为'MPC_ACC_2020-master'的四旋翼飞行器模型预测跟踪控制器(Matlab实现)。四旋翼飞行器由于其高度非线性和强耦合特性,在复杂环境中难以实现精准控制。模型预测控制(MPC)通过预测未来状态并在每一步进行在线优化,解决了这一难题。文中展示了关键代码片段,解释了系统参数定义、初始化、预测模型构建、成本函数构建、优化求解及控制输入的应用。此外,还探讨了MPC_ACC_2020-master如何通过精心设计的成本函数和优化算法确保四旋翼飞行器状态收敛到设定点。 适合人群:从事飞行器控制领域的研究人员和技术爱好者,尤其是对模型预测控制感兴趣的开发者。 使用场景及目标:适用于四旋翼飞行器的轨迹跟踪任务,旨在提高飞行器在复杂环境下的稳定性与准确性。具体应用场景包括但不限于无人机竞速、自动巡航、物流配送等。 其他说明:尽管该项目主要用于科研目的,但其简洁高效的代码结构也为实际工程应用提供了良好借鉴。同时,项目中存在一些待改进之处,如状态估计部分未考虑真实情况下的噪声干扰,后续版本计划移植到C++并集成进ROS系统。
内容概要:本文探讨了基于MATLAB2020b平台,采用CNN-LSTM模型结合人工大猩猩部队(GTO)算法进行电力负荷预测的方法。首先介绍了CNN-LSTM模型的基本结构及其在处理多变量输入(如历史负荷和气象数据)方面的优势。随后详细解释了如何通过GTO算法优化超参数选择,提高模型预测精度。文中展示了具体的MATLAB代码示例,包括数据预处理、网络层搭建、训练选项设定等方面的内容,并分享了一些实践经验和技术细节。此外,还讨论了模型的实际应用效果,特别是在某省级电网数据上的测试结果。 适合人群:从事电力系统数据分析的研究人员、工程师,以及对深度学习应用于时间序列预测感兴趣的开发者。 使用场景及目标:适用于需要精确预测未来电力负荷的情况,旨在帮助电力公司更好地规划发电计划,优化资源配置,保障电网安全稳定运行。通过本研究可以学习到如何构建高效的CNN-LSTM模型,并掌握利用GTO算法进行超参数优化的具体步骤。 其他说明:文中提到的一些技巧和注意事项有助于避免常见错误,提高模型性能。例如,合理的数据预处理方式、适当的超参数范围设定等都能显著改善最终的预测效果。
数据集一个高质量的医学图像数据集,专门用于脑肿瘤的检测和分类研究以下是关于这个数据集的详细介绍:该数据集包含5249张脑部MRI图像,分为训练集和验证集。每张图像都标注了边界框(Bounding Boxes),并按照脑肿瘤的类型分为四个类别:胶质瘤(Glioma)、脑膜瘤(Meningioma)、无肿瘤(No Tumor)和垂体瘤(Pituitary)。这些图像涵盖了不同的MRI扫描角度,包括矢状面、轴面和冠状面,能够全面覆盖脑部解剖结构,为模型训练提供了丰富多样的数据基础。高质量标注:边界框是通过LabelImg工具手动标注的,标注过程严谨,确保了标注的准确性和可靠性。多角度覆盖:图像从不同的MRI扫描角度拍摄,包括矢状面、轴面和冠状面,能够全面覆盖脑部解剖结构。数据清洗与筛选:数据集在创建过程中经过了彻底的清洗,去除了噪声、错误标注和质量不佳的图像,保证了数据的高质量。该数据集非常适合用于训练和验证深度学习模型,以实现脑肿瘤的检测和分类。它为开发医学图像处理中的计算机视觉应用提供了坚实的基础,能够帮助研究人员和开发人员构建更准确、更可靠的脑肿瘤诊断系统。这个数据集为脑肿瘤检测和分类的研究提供了宝贵的资源,能够帮助研究人员开发出更准确、更高效的诊断工具,从而为脑肿瘤患者的早期诊断和治疗规划提供支持。
内容概要:本文详细介绍了STM32F103的CAN通讯和IAP升级Bootloader的源码实现及其硬件设计。首先,针对CAN通讯部分,文章深入探讨了CAN外设的初始化配置,包括波特率、位时间、过滤器等重要参数的设置方法,并提供了一段完整的初始化代码示例。接着,对于IAP升级Bootloader,文中讲解了通过CAN总线接收HEX文件并写入Flash的具体实现步骤,以及如何安全地从Bootloader跳转到应用程序。此外,文章还附上了原理图和PCB文件,有助于理解和优化硬件设计。最后,作者分享了一些实用的调试技巧和注意事项,如终端电阻的正确使用、CRC校验的应用等。 适合人群:嵌入式系统开发者、硬件工程师、从事STM32开发的技术人员。 使用场景及目标:适用于正在开发STM32相关项目的工程师,尤其是那些需要实现CAN通讯和固件在线升级功能的人群。通过学习本文提供的源码和技术要点,可以帮助他们快速掌握相关技能,提高开发效率。 其他说明:本文不仅提供了详细的代码示例,还包含了丰富的实践经验分享,能够帮助读者更好地理解和解决实际开发中遇到的问题。
工具集语音、监控、摄像头、画笔等功能于一体!清晰语音录入,确保声画同步;监控级画面录制,操作细节无遗漏;摄像头多视角呈现,让内容更生动。录制时,画笔可标注重点,快速传递关键信息。自带视频播放,无需第三方;快捷键操作便捷,录制高效。强大解码器兼容多格式,不同设备随心播放。无论是教学、办公还是创作
内容概要:本文详细介绍了西门子S7-1500 PLC在制药厂洁净空调建筑管理系统(BMS)中的应用案例。重点讨论了硬件配置(1500 CPU + ET200SP分布式IO)、温湿度控制策略(串级PID、分程调节)、以及具体的编程实现(SCL语言)。文中分享了多个技术细节,如PT100温度采集、PID控制算法优化、报警管理和HMI界面设计等。此外,作者还提到了一些调试过程中遇到的问题及其解决方案,如PID_Compact块的手动模式设定值跳变问题、博图V15.1的兼容性问题等。 适合人群:从事工业自动化领域的工程师和技术人员,特别是那些对PLC编程、温湿度控制和洁净空调系统感兴趣的读者。 使用场景及目标:适用于制药厂或其他对温湿度控制要求严格的行业。主要目标是确保洁净空调系统的高效运行,将温湿度波动控制在极小范围内,保障生产环境的安全性和稳定性。 其他说明:本文不仅提供了详细的编程代码和硬件配置指南,还分享了许多实践经验,帮助读者更好地理解和应用相关技术。同时,强调了在实际项目中需要注意的关键点和潜在问题。
2025年6G近场技术白皮书2.0.pdf
少儿编程scratch项目源代码文件案例素材-Frogeon.zip
2025年感知技术十大趋势深度分析报告.pdf
内容概要:本文详细介绍了一种用于解决车间调度问题的遗传算法(Matlab实现),即JSPGA。文章首先介绍了遗传算法的基本概念及其在车间调度问题中的应用场景。接着,作者展示了完整的Matlab源码,包括参数设置、种群初始化、选择、交叉、变异、适应度计算以及结果输出等模块。文中还特别强调了适应度计算方法的选择,采用了最大完工时间的倒数作为适应度值,并通过三维甘特图和迭代曲线直观展示算法性能。此外,文章提供了多个调参技巧和改进方向,帮助读者更好地理解和应用该算法。 适合人群:对遗传算法感兴趣的研究人员、工程师以及希望深入理解车间调度问题求解方法的技术爱好者。 使用场景及目标:适用于需要优化多台机器、多个工件加工顺序与分配的实际工业生产环境。主要目标是通过遗传算法找到最优或近似最优的调度方案,从而减少最大完工时间,提高生产效率。 其他说明:文章不仅提供了详细的理论解释和技术细节,还包括了大量实用的代码片段和图表,使读者能够轻松复现实验结果。同时,作者还分享了一些个人经验和建议,为后续研究提供了有价值的参考。
内容概要:本文深入探讨了永磁同步电机(PMSM)的最大转矩电流比(MTPA)控制算法,并详细介绍了基于Simulink的仿真模型设计。首先,文章阐述了PMSM的数学模型,包括电压方程和磁链方程,这是理解控制算法的基础。接着,解释了矢量控制原理,通过将定子电流分解为励磁电流和转矩电流分量,实现对电机的有效控制。随后,重点讨论了MTPA控制的目标和方法,即在限定电流条件下最大化转矩输出。此外,文章还涉及了前馈补偿、弱磁控制和SVPWM调制等关键技术,提供了具体的实现代码和仿真思路。最后,通过一系列实验验证了各控制策略的效果。 适合人群:从事电机控制系统设计的研究人员和技术人员,尤其是对永磁同步电机和Simulink仿真感兴趣的工程师。 使用场景及目标:适用于希望深入了解PMSM控制算法并在Simulink环境中进行仿真的技术人员。主要目标是掌握MTPA控制的核心原理,学会构建高效的仿真模型,优化电机性能。 其他说明:文中不仅提供了详细的理论推导,还有丰富的代码示例和实践经验,有助于读者快速理解和应用相关技术。同时,强调了实际工程中常见的问题及解决方案,如负载扰动、弱磁控制和SVPWM调制等。
内容概要:本文详细介绍了三机并联的风光储混合系统在Matlab中的仿真方法及其关键技术。首先,针对光伏阵列模型,讨论了其核心二极管方程以及MPPT(最大功率点跟踪)算法的应用,强调了环境参数对输出特性的影响。接着,探讨了永磁同步风机的矢量控制,尤其是转速追踪和MPPT控制策略。对于混合储能系统,则深入讲解了超级电容和蓄电池的充放电策略,以及它们之间的协调机制。此外,还涉及了PQ控制的具体实现,包括双闭环结构的设计和锁相环的优化。最后,提供了仿真过程中常见的问题及解决方案,如求解器选择、参数敏感性和系统稳定性等。 适合人群:从事电力电子、新能源系统设计与仿真的工程师和技术人员,以及相关专业的研究生。 使用场景及目标:适用于希望深入了解风光储混合系统工作原理的研究人员,旨在帮助他们掌握Matlab仿真技巧,提高系统设计和优化的能力。 其他说明:文中不仅提供了详细的理论推导和代码示例,还分享了许多实践经验,有助于读者更好地理解和应用所学知识。
本书由国际发展研究中心(IDRC)和东南亚研究院(ISEAS)联合出版,旨在探讨亚洲背景下电子商务的发展与实践。IDRC自1970年起,致力于通过科学技术解决发展中国家的社会、经济和环境问题。书中详细介绍了IDRC的ICT4D项目,以及如何通过项目如Acacia、泛亚网络和泛美项目,在非洲、亚洲和拉丁美洲推动信息通信技术(ICTs)的影响力。特别强调了IDRC在弥合数字鸿沟方面所作出的贡献,如美洲连通性研究所和非洲连通性项目。ISEAS作为东南亚区域研究中心,专注于研究该地区的发展趋势,其出版物广泛传播东南亚的研究成果。本书还收录了电子商务在亚洲不同国家的具体案例研究,包括小型工匠和开发组织的电子商务行动研究、通过互联网直接营销手工艺品、电子营销人员的创新方法以及越南电子商务发展的政策影响。
2025工业5G终端设备发展报告.pdf
内容概要:本文档《Java经典面试笔试题及答案.docx》涵盖了广泛的Java基础知识和技术要点,通过一系列面试题的形式,深入浅出地讲解了Java的核心概念。文档内容包括但不限于:变量的声明与定义、对象序列化、值传递与引用传递、接口与抽象类的区别、继承的意义、方法重载的优势、集合框架的结构、异常处理机制、线程同步、泛型的应用、多态的概念、输入输出流的使用、JVM的工作原理等。此外,还涉及了诸如线程、GUI事件处理、类与接口的设计原则等高级主题。文档不仅解释了各个知识点的基本概念,还提供了实际应用场景中的注意事项和最佳实践。 适合人群:具备一定Java编程基础的学习者或开发者,特别是准备参加Java相关岗位面试的求职者。 使用场景及目标:①帮助读者巩固Java基础知识,提升对Java核心技术的理解;②为面试做准备,提供常见面试题及其详细解答;③指导开发者在实际项目中应用Java的最佳实践,优化代码质量和性能。 其他说明:文档内容详实,涵盖了Java开发中的多个方面,从基础语法到高级特性均有涉及。建议读者在学习过程中结合实际编程练习,加深对各个知识点的理解和掌握。同时,对于复杂的概念和技术,可以通过查阅官方文档或参考书籍进一步学习。