新手写OFBiz的HelloWorld

本人没有学过web2.0,但按照http://www.opensourcestrategies.com/ofbiz/hello_world1.php的这篇文章,也使用OFBiz跑起一个简单的Helloworld页面了,看来rp不错!下面写一下我搭建的过程。

后面发现https://cwiki.apache.org/OFBIZ/ofbiz-tutorial-a-beginners-development-guide.html这个教程更官方和详细,强烈推荐这个!!

1、我们需要在ofbiz主目录下的hot-deploy目录下建立一个hello1文件夹,


文章对hot-deploy的说明如下:

The advantage of the hot-deploy/ directory is that components here are loaded automatically when OFBiz starts

我的理解是hot-deploy就是用来放我们写的网站/网页的地方

在我们建的hello文件夹中建立一个ofbiz-component.xml的文件,如图:


向里面写入这些内容:

<?xml version="1.0" encoding="UTF-8"?>

<ofbiz-component name="hello1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/ofbiz-component.xsd">
<resource-loader name="main" type="component"/>
<webapp name="hello1"
title="My First OFBiz Application"
server="default-server"
location="webapp/hello1"
mount-point="/hello1"
app-bar-display="false"/>
</ofbiz-component>

文章中对这个文件的作用做了如下的描述:

ofbiz-component.xml file to tell OFBiz about your applicaton component: its data model (tables), business logic (services) and workflow, UI layer (web or GUI applications), any seed data it may have, and what other resources it requires.
我们来分析一下这段代码说明了什么:

l 我们的组件名叫做"hello1“

这个hello1的组件名在我们最后访问网址的时候表现如下:

+[http://localhost:8080/+hello1{+}/control/main|http://localhost:8080/hello1/control/main]+


l resource-loader的名字叫做main

l 只有一个web应用,名字也叫“hello1”

l 这个叫hello1的web应用使用的是“default-server”这个服务器

l 这个应用Component的位置在webapp/hello1目录下

l 这个应用Component在使用url访问的时候应该用“/hello1”来访问

2、在这个hello1目录中,我们建立一个webapp目录,如图所示


以后我们的代码就放在里面.进入到webapp目录中,我们还要再新建一个hello1目录,这里hello的名字是要对应之前写的

<webapp name="hello1"

这一句代码

一个Component里面可以包含很多个app, A component can have multiple webapps attached to it. e.g. In the "marketing" component there are two webapps "marketing" and "sfa".

3、进入到这个hello1目录中,我们建立一个WEB-INF文件夹以及一个叫做main.ftl的文件,如图所示:


这个main.ftl的作用我的理解是它定义了显示给用户看的页面是什么样子的。他

是一个Freemarker的template file。Freemarker是什么可以查看这个网址

http://www.zzbaike.com/wiki/FreeMarker

简单来说它是用来设计HTML web页面的,特别适用于基于MVC模式的应用程序。

我们在这个文件中输入以下内容:

<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>HELLO</h1>
<p>Hello world\!</p>
</body>
</html>

对于目前我们的程序来说,main.ftl就是一个简单的html文件。

https://cwiki.apache.org/OFBIZ/ofbiz-tutorial-a-beginners-development-guide.html的part2中,我们可以看到,可以在.ftl中进行判断,来决定显示什么内容。

4、我们进入到WEB-INF这个目录中,创建两个xml文件,一个是web.xml,另一个是controller.xml,如图所示:


需要知道的是,每一个OFBiz的web应用,都至少需要这两个文件。

l controller.xml告诉OFBiz如何处理访问者不同的请求,针对不同的请求采取什么样的动作,返回什么样的界面。

l web.xml则告诉OFBiz对于这个web应用,可以使用那些资源,如数据库之类的。

我们在controller.xml中写入如下内容:

<?xml version="1.0" encoding="UTF-8" ?>
<site-conf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/site-conf.xsd">
    <description>First Hello World Site Configuration File</description>
     <owner>Open For Business Project (c) 2005 </owner>
     <errorpage>/error/error.jsp</errorpage>

     <handler name="java" type="request"/>
     <handler name="soap" type="request"/>
     <handler name="service" type="request"/>
     <handler name="service-multi" type="request"/>
     <handler name="simple" type="request"/>

     <handler name="ftl" type="view"/>
     <handler name="jsp" type="view"/>
     <handler name="screen" type="view"/>

    <handler name="http" type="view"/>

     <preprocessor>
         <\!-\- Events to run on every request before security (chains exempt) \-->
         <\!-\- <event type="java" path="org.ofbiz.webapp.event.TestEvent" invoke="test"/> \-->
         <event type="java" path="org.ofbiz.securityext.login.LoginEvents" invoke="checkExternalLoginKey"/>
     </preprocessor>
     <postprocessor>
         <\!-\- Events to run on every request after all other processing (chains exempt) \-->
         <\!-\- <event type="java" path="org.ofbiz.webapp.event.TestEvent" invoke="test"/> \-->
     </postprocessor>

     <\!-\- Request Mappings \-->
     <request-map uri="main">
         <response name="success" type="view" value="main"/>
     </request-map>

     <\!-\- end of request mappings \-->

     <\!-\- View Mappings \-->
     <view-map name="error" page="/error/error.jsp"/>
     <view-map name="main" type="ftl" page="main.ftl"/>
     <\!-\- end of view mappings \-->
 </site-conf>


可以对此做一下简单的分析:

l 可以看出有9个handler。这些hanlder用于处理请求或者提供界面。他们都是Java的类来着。

<handler name="java" type="request"/>

这个对应servlet的请求
<handler name="service" type="request"/>

这个对应service engine的请求

至于那些type为view的,用于针对browser based on Freemarker templates, velocity, JSP, or the OFBiz screen widget.提供不同的界面

l pre-和post-processors。他们是servlet,将会在每一个web请求操作的之前和之后运行。对于我们写的这个web应用,只有pre-processor,

<event type="java" path="org.ofbiz.securityext.login.LoginEvents" invoke="checkExternalLoginKey"/>

至于它做了什么我现在不是很清楚。

l request mappings。用于将URI(统一资源标识符,是一个用于标识某一互联网资源名称的字符串。 该种标识允许用户对网络中(一般指万维网)的资源通过特定的协议进行交互操作。URI由包括确定语法和相关协议的方案所定义。)请求映射到具体的 request handler或者view handler。

对于我们这个web应用,只有一个请求,就是”/mian”

l view mappings。我的理解是用于将某一个变量映射到具体的页面文件。如:

<view-map name="main" type="ftl" page="main.ftl"/>

我们就吧main映射到了之前写的main.ftl文件。

 

对于web.xml,我们写入如下的内容:

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <display-name>Hello 1</display-name>
    <description>The First Hello World Application</description>

    <context-param>
        <param-name>entityDelegatorName</param-name>
        <param-value>default</param-value>
        <description>The Name of the Entity Delegator to use, defined in entityengine.xml</description>
    </context-param>
    <context-param>
        <param-name>localDispatcherName</param-name>
        <param-value>hello1</param-value>
        <description>A unique name used to identify/recognize the local dispatcher for the Service Engine</description>
    </context-param>
    <context-param>
        <param-name>serviceReaderUrls</param-name>
        <param-value>/WEB-INF/services.xml</param-value>
        <description>Configuration File(s) For The Service Dispatcher</description>
    </context-param>

    <filter>
        <filter-name>ContextFilter</filter-name>
        <display-name>ContextFilter</display-name>
        <filter-class>org.ofbiz.webapp.control.ContextFilter</filter-class>
        <init-param>
            <param-name>disableContextSecurity</param-name>
            <param-value>N</param-value>
        </init-param>
        <init-param>
            <param-name>allowedPaths</param-name>
            <param-value>/control:/select:/index.html:/index.jsp:/default.html:
                               /default.jsp:/images:/includes/maincss.css</param-value>
        </init-param>
        <init-param>
            <param-name>errorCode</param-name>
            <param-value>403</param-value>
        </init-param>
        <init-param>
            <param-name>redirectPath</param-name>
            <param-value>/control/main</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>ContextFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener><listener-class>
              org.ofbiz.webapp.control.ControlEventListener</listener-class></listener>
    <listener><listener-class>
              org.ofbiz.webapp.control.LoginEventListener</listener-class></listener>
    <!-- NOTE: not all app servers support mounting implementations of the HttpSessionActivationListener interface -->
    <!-- <listener><listener-class>
          org.ofbiz.webapp.control.ControlActivationEventListener</listener-class></listener> -->

    <servlet>
        <servlet-name>ControlServlet</servlet-name>
        <display-name>ControlServlet</display-name>
        <description>Main Control Servlet</description>
        <servlet-class>org.ofbiz.webapp.control.ControlServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ControlServlet</servlet-name>
        <url-pattern>/control/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>60</session-timeout> <!-- in minutes -->
    </session-config>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>

至于做了什么,我现在还无法分析。

5、最后,就是启动startofbiz.bat这个脚本,然后访问

http://localhost:8080/hello1/control/main

当你看到如下的显示:


那么恭喜你,成功地写了第一个OFBiz的helloworld程序啦!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值