OFBiz 快速入門——續二

2.5 創建一個文件,取名爲(controller.xml),被OFBiz webapp控制器使用的。在沒有額外增加功能時,這個文件內容非常的小與簡單,如下:

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

<site-conf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/site-conf.xsd">

       <include

        location="component://common/webcommon/WEB-INF/common-controller.xml"/>

       <description>Practice Component Site Configuration File</description>

       <owner>Copyright 2001-2009 The Apache Software Foundation</owner>

       <handler name="screen" type="view" 

class="org.ofbiz.widget.screen.ScreenWidgetViewHandler"/>

       <!-- Request Mappings -->

       <request-map uri="main">

           <security https="false" auth="false"/>

           <response name="success" type="view" value="main"/>

       </request-map>

       <!-- end of request mappings -->

       <!-- View Mappings -->

       <view-map name="main" type="screen" 

page="component://practice/widget/PracticeScreens.xml#main"/>

       <!-- end of view mappings -->

</site-conf>

2.6 回到上一級目錄,創建一個(error)目錄,

   完整路徑是(hot-deploy/practice/webapp/practice/error

2.6.1 在(error)目錄中創建一個(error.jsp)文件。文件的內容,可以從 example或其他的組件拷貝過來。

這個目錄結構,就是你在上一步驟中在controller.xml文件中的<errorpage>/error/error.jsp</errorpage>的位置。你會需要用到這個目錄去顯示一個錯誤信息給用戶。內容如下(省去一些HTML代碼,只貼出與OFBiz有關的,詳細的請自己瀏覽):

<%@ page import="org.ofbiz.base.util.*" %>

先獲得request中的錯誤信息,並保存在一個errorMsg的變量中

<% String errorMsg = (String) request.getAttribute("_ERROR_MESSAGE_"); %>

最後,在HTML頁面中某處合適的位置,輸出其錯誤信息:

<%=UtilFormatOut.replaceString(errorMsg, "\n", "<br/>")%>

2.7 在組件目錄(practice)中創建目錄,命名爲(widget),完整路徑是,(hot-deploy/practice/widget)。這個目錄的作用,包含你應用將要創建的UI,有formsmenusscreens等。

2.8 在(widget)目錄中創建一個命名爲(PracticeScreens.xml)的文件。類似地,其文件內容也可以從Example組件中獲取並拷貝到這文件中。

在創建Views前,推薦你先去閱讀以下有關內容(注意,這個是告訴我自己要去閱讀的)

² HTML and CSS Best Practice

² Managing Your Source Differences

² Methodology Recommendations

² User Interface Layout Best Practices

閱讀以上文檔後,會對你產生莫大的幫助,但在這裏,可以先使用下面的代碼:

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

<screens xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation

="http://ofbiz.apache.org/dtds/widget-screen.xsd">

    <screen name="main">

        <section>

            <widgets>

                <label text="This is first practice"/>

            </widgets>

        </section>

    </screen>

</screens>

2.9 完成了一個基本應用(practice)。首先,客戶端的一個request會去查看一個特定的資源,舉個例子:localhost:8080/practice/cntrol/main

OFBiz接收到這個請求,就會查看/practice這個項。爲什麼呢?因爲我們在ofbiz-component.xml已經聲明webapps加載點是/practice。現在OFBiz知道處理(practice)組件請求的剩餘部分了。

經歷了OFBizofbiz-component.xml查找到掛載點(/practice)後,OFBiz這時將會查看controller.xml文件。這個文件裏,我們已經定義了request-mapsview-maps。如果找到request-map是路徑後面的(main),就會使用相關聯的view-map,其他也如此。在request-map中,能或者指定一個view或者是一個事件或一個服務這都將會在後面所見到的。如果指定的是一個view,就會進一步查看request-map元素中查找到指定的名稱並返回。看如下xml內容:

<request-map uri="main">

<security https="false" auth="false"/>

<response name="success" type="view" value="main"/>

</request-map>

<view-map name="main" type="screen" 

page="component://practice/widget/PracticeScreens.xml#main"/>

在這裏就可很清楚的知道,如果urimain的時候,就會在request-map標籤中進行下一步的查找,如果是success時,就會得到一個類型爲view值爲main的返回。那麼,OFBiz就會索引到view-map中與其(main)值相等的標籤。跟着,就會在PracticeScreens.xml文件中讀取元素名爲main的內容(#

最後,就可以啓動OFBiz,在瀏覽器中輸入以下的URL

 http://localhost:8080/practice/control/main

2.11 webapppractice)目錄創建一個名爲(index.jsp)的文件。當然,類似地,其文件內容也可以從Example組件中拷貝過來。這個文件的作用是爲了響應如下的URLhttp://localhost:8080/practice/。如果你給了一個錯誤的URL,如http://localhost:8080/practice/unknown/request將會重定向到在web.xml定義好的redirectPath中的文件路徑。

In the case, ContextFilter will filter out the request and use the redirect path to redirect the request.

第二部分

3.做進一步的一些操作

待續待續ing

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章