Getting started with OFBiz Development

Source: Internet
Author: User
Tags form post

1. Ofbiz introduction:

Ofbiz (http://www.ofbiz.org) is Open Source business software system, make full use of all the excellent Open Source projects, such as Tomcat, Ant, BeanShell, Jboss, etc., to build a powerful system platform, ofbiz has completed the components required by most business software systems, such as user authentication, workflow, and business rule processing. The core technology of Ofbiz lies in Entity Engine, other components are based on it. In short, Entity Engine provides powerful encapsulation of database table creation, object-to-data table ing, and object query, you can define the database table structure in a simple XML file. Ofbiz will automatically help you create tables in the database and dynamically generate ing objects. You can only process objects in the program, ofbiz is automatically updated to the database through the transaction logic. One of the advantages Ofbiz claims is that it uses a small amount of Code to complete complex processing.

For more information about OFBiz, click here.
OFBiz: click here

2. Download and install Ofbiz

First install J2SDK1.4, download to the http://java.sun.com, set JAVA_HOME environment variable to J2SDK installation directory.

Visit the website http://www.ofbiz.org, there is a download connection, please select the Complete package, because this package already contains everything to run Ofbiz, download it and unlock it to run.

Unbind the Ofbiz package to a directory. Assume it is "C: \ ofbiz". There will be two directories catalina and ofbiz under this directory. The catalina directory is the Tomcat directory, ofbiz modified its configuration. The ofbiz directory is the program code directory of Ofbiz. In the command line status, go to the "c: \ ofbiz \ catalina \ bin" directory and run the "ofbiz run" command to start Ofbiz. After the command is started, you can access "http: // localhost: 8080/ecommerce ", which can access the e-commerce module of Ofbiz. You can access other modules through the connection on the page.


3. Create Ofbiz Schema

Ofbiz application entry:

For example, suppose we need to create a customer data table named StudyCustomer. Each segment is as follows:
StudyCustomer {
CustomerId Integer,
CustomerName String,
CustomerNote String,
}

Follow these steps to add, delete, modify, and query basic data operations:


1. Define the data Schema in the XML file:
Three files are required. One is the entitymodel_xxx.xml and entityengine. xml of the project to be created.
Entitygroup. xml,
Entitymodel_xxx.xml is created by ourselves. Assume that we name it entitymodel_study.xml and place it in the "c: \ ofbiz \ commonapp \ entitydef" directory,
Entityengine. xml is already available in Ofbiz and is stored in the "c: \ ofbiz \ commonapp \ etc" directory to include the entitymodel file we have defined.
Entitygroup. xml is also available in Ofbiz, which is in the same directory as engityengine. xml. We need to add our Schema definition to this file.

The Definition Format of entitymodel_study.xml is as follows:
<! -- ===================================================== ========================================================== = -->

<? 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-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>

<! -- ===================================================== ========================================================== = -->

The Tag in this XML file is basically clear, but the field type is predefined by Ofbiz to ensure inter-database migration.

Add the file we just defined in entityengine. xml and add a row to the appropriate location:
<Resource loader = "mainfile" location = "entitymodel_study.xml"/>
You can find the specific location by checking entityengine. xml, which contains other files.

Add our Schema definition to entitygroup. xml, and add a row to the end.
<Entity-group = "org. ofbiz. commonapp" entity = "StudyCustomer"/>

In this way, the Schema is defined. Now c: \ ofbiz \ commonapp \ etc \ entityengine. copy the xml file to the c: \ ofbiz \ catalina \ shared \ classes directory. Remember that I didn't copy the file before, and I couldn't create the Schema at last.

Restart Ofbiz, access URL: http: // localhost: 8080/webtools, click the "Login" link in the upper-right corner, and log on with admin/ofbiz, after logging on to the system, click "Check/Update Database". The Check Form will appear. This Form can only Check whether the Schema is changed. The default GroupName is "org. ofbiz. commonapp ", this does not need to be changed. Click the" Check Only "button. Ofbiz will Check the changes and display a complete list, you can check whether the "StudyCustomer" We just created exists. If not, it may be some of the problems we have previously defined. Check the problem and try again.

After checking, you can select "Check and Add Missing". This is a powerful function of Ofbiz. You have added tables in XML or segments in a table, it will be automatically mapped to the database to avoid direct operations on the database.

StudyCustomer Schema has been created. to check whether a table is created, Open c: \ ofbiz \ data \ ofbiz in the editor. script to query the words "create table StudyCustomer" in it. If there is no problem, we can find it.

 

4. How to Use the defined Schema

How to use a defined Schema

Ofbiz follows the MVC design pattern. On the View end, that is, the JSP end mainly uses the Tag defined by Ofbiz to display or extract data. Control is a Controller Servlet, in the URI mapping configuration file of Controller Servlet, we define the program to which each URL points. In this way, through this mapping configuration file, we can ensure the independence between each page and specific processing program, for example, you can modify the configuration file to change the URL of a Form Post Action without modifying the actual HTML or JSP code.

Ofbiz defines the Regions concept, which divides an HTML page into several areas, such as Top, Left, Right, and Main. Through these Regions, we can easily combine the UI interface, in addition, you can easily change the position of each part. For example, you can easily move the menu from the top to the bottom, just need to change a configuration file. Regions is similar to a Frame in HTML, But it combines interfaces through a page. frames are displayed in Different frames through several pages. The Frame control is complicated, and you need to change the relevant program.

In Ofbiz, we can directly operate the Schema-Defined Object in JSP, that is, the StudyCustomer we just defined,
Example:


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

<% @ Page import = "java. util. *" %>
<% @ Page import = "org. ofbiz. core. util. *, org. ofbiz. core. pseudo dotag. *" %>
<% @ 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) <% }%>


This program is easy to understand. First, an Object is created through delegator, and the Object will be automatically synchronized to the database by Ofbiz. Then, all saved objects are obtained through the findAll of the delegator, and finally displayed through an Iterator Object.

This program is named testofbiz. jsp. For simplicity, we put it in the directory of an existing Webapp of Ofbiz and put it in the directory of c: \ ofbiz \ partymgr \ webapp \ party. Then we need to modify two configuration files: controller. xml and regions. xml. These two files are the mapping and regions configuration files we mentioned above.

Both files are under: c: \ ofbiz \ partymgr \ webapp \ WEB-INF, add the following to controller. xml

<Request-map uri = "testofbiz">
<Description> Test Ofbiz </description>
<Security https = "false" auth = "false"/>
<Response name = "success" type = "view" value = "testofbiz"/>
</Request-map>
And
<View-map name = "testofbiz" type = "region"/>

See the configurations in controller. xml. Add the following to regions. xml:
<Define id = 'testofbiz' region = 'main _ region'>
<Put section = 'title'> Test Ofbiz </put>
<Put section = 'content' content = '/party/testofbiz. jsp'/>
</Define>
For more information, see the existing configuration.

After configuration, restart ofbiz and access URL: http: // localhost: 8080/partymgr/control/testofbiz.

Because we are in testofbiz. the Ofbiz security control mechanism is used in the jsp program. The system will prompt that there is no access permission now and you need to log on. Click "Login" on the right to log on with admin/ofbiz and we will see the program testofbiz. jsp running result. To add a new record, modify UtilMisc. toMap ("customerId", "1", "customerName", "Cust1", "customerNote", "Customer Note 1"); values of each segment, then access http: // localhost: 8080/partymgr/control/testofbiz. If you directly access the URL without modification, the system will prompt the Primary key conflict.

  • 1
  • 2
  • 3
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.