下面是实现的学期管理查询界面:
具体实现步骤如下:
1. 定义实体
创建文件hot-deploy\booking\entitydef\entitymodel.xml
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entitymodel.xsd">
<!-- ========================================================= -->
<!-- ======================== Defaults ======================= -->
<!-- ========================================================= -->
<title>Entity of an Apache OFBiz Component</title>
<description>None</description>
<version>1.0</version>
<default-resource-name>BookingEntityLabels</default-resource-name>
<entity entity-name="Term" package-name="org.apache.ofbiz.booking.term" title="Term Entity">
<field name="termName" type="id-ne"><description>primary sequenced ID</description></field>
<field name="beginTime" type="date"></field>
<field name="endTime" type="date"></field>
<field name="used" type="name"></field>
<prim-key field="termName"/>
</entity>
</entitymodel>
加入下面配置:
<entity-resource type="model" reader-name="main" loader="main" location="entitydef/entitymodel.xml"/>
最终ofbiz-component.xml文件
<ofbiz-component name="booking" enabled="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd">
<!-- define resource loaders; most common is to use the component resource loader -->
<resource-loader name="main" type="component"/>
<!-- place the config directory on the classpath to access configuration files -->
<classpath type="dir" location="config"/>
<entity-resource type="model" reader-name="main" loader="main" location="entitydef/entitymodel.xml"/>
<entity-resource type="data" reader-name="demo" loader="main" location="data/BookingDemoData.xml"/>
<!-- web applications; will be mounted when using the embedded container -->
<webapp name="booking"
title="Booking"
menu-name="secondary"
server="default-server"
location="webapp/booking"
base-permission="OFBTOOLS,BOOKING"
mount-point="/booking"/>
</ofbiz-component>
3. 配置controller.xml文件
controller.xml配置中加入如下配置:
<request-map uri="FindTerm">
<security https="true" auth="true"/>
<response name="success" type="view" value="FindTerm"/>
</request-map>
<view-map name="FindTerm" type="screen" page="component://booking/widget/term/TermScreens.xml#FindTerm"/>
4. 配置TermScreens.xml#FindTerm创建文件component://booking/widget/term/TermScreens.xml#FindTerm
<screens xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://ofbiz.apache.org/Widget-Screen" xsi:schemaLocation="http://ofbiz.apache.org/Widget-Screen http://ofbiz.apache.org/dtds/widget-screen.xsd">
<screen name="FindTerm">
<section>
<actions>
<set field="titleProperty" value="TermManange"/>
<set field="headerItem" value="TermManange"/>
<set field="viewIndex" from-field="parameters.VIEW_INDEX" type="Integer"/>
<property-to-field resource="widget" property="widget.form.defaultViewSize" field="viewSizeDefaultValue"/>
<set field="viewSize" from-field="parameters.VIEW_SIZE" type="Integer" default-value="${viewSizeDefaultValue}"/>
</actions>
<widgets>
<decorator-screen name="CommonBookingDecorator" location="component://booking/widget/booking/CommonScreens.xml">
<decorator-section name="body">
<section>
<widgets>
<decorator-screen name="FindScreenDecorator" location="component://common/widget/CommonScreens.xml">
<decorator-section name="menu-bar">
<container style="button-bar">
<link target="CreateTerm" text="${uiLabelMap.CreateTerm}" style="buttontext create"/>
</container>
</decorator-section>
<decorator-section name="search-options">
<include-form name="FindTerm" location="component://booking/widget/term/TermForms.xml"/>
</decorator-section>
<decorator-section name="search-results">
<include-form name="ListFindTerm" location="component://booking/widget/term/TermForms.xml"/>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
</screens>
并且这里必须配置好headerItem参数,这样才能正确的显示三级菜单。该参数是菜单name属性值。也就是对应BookingMenus.xml里<menu-item name="TermManange" title="${uiLabelMap.TermManange}">的name属性。
容器button-bar中配置了该界面的操作栏。里面配置了一个【新增学期】的操作按钮
这里使用了CommonBookingDecorator和FindScreenDecorator装饰器。需要在CommonScreens.xml中定义。
还使用了TermForms.xml#FindTerm与TermForms.xml#ListFindTerm表单。需要在TermForms.xml中定义。
5. 定义CommonBookingDecorator和FindScreenDecorator装饰器
打开component://booking/widget/booking/CommonScreens.xml配置CommonBookingDecorator和FindScreenDecorator装饰器
<screen name="CommonBookingDecorator">
<section>
<widgets>
<decorator-screen name="main-decorator">
<decorator-section name="body">
<section>
<condition>
<if-has-permission permission="BOOKING" action="_VIEW"/>
</condition>
<widgets>
<decorator-section-include name="body"/>
</widgets>
<fail-widgets>
<label style="h3">${uiLabelMap.BookingViewPermissionError}</label>
</fail-widgets>
</section>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
<screen name="FindScreenDecorator">
<section>
<widgets>
<section>
<condition>
<if-empty field="titleProperty"/>
</condition>
<widgets>
<container style="page-title"><label text="${title}"></label></container>
</widgets>
<fail-widgets>
<container style="page-title"><label text="${uiLabelMap[titleProperty]}"></label></container>
</fail-widgets>
</section>
<decorator-section-include name="menu-bar"/>
<container style="clear"/>
<screenlet id="searchOptions" name="findScreenlet" collapsible="true" title="${uiLabelMap.CommonSearchOptions}">
<container id="search-options">
<decorator-section-include name="search-options" />
</container>
</screenlet>
<screenlet padded="false">
<label style="h3" text="${uiLabelMap.CommonSearchResults}"/>
<container id="search-results">
<decorator-section-include name="search-results"/>
</container>
</screenlet>
</widgets>
</section>
</screen>
从上面的配置中可以看到配置titleProperty属性时,value值是UILable的key值,而不是最终显示的value。所以TermScreens.xml#FindTerm中正确的配置是
<set field="titleProperty" value="TermManange"/>
下面这么配置是错误的
<set field="titleProperty" value="${uiLabelMap.TermManange}"/>
6. 配置TermForms.xml
创建文件component://booking/widget/term/TermForms.xml
<forms xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://ofbiz.apache.org/Widget-Form" xsi:schemaLocation="http://ofbiz.apache.org/Widget-Form http://ofbiz.apache.org/dtds/widget-form.xsd">
<form name="FindTerm" target="FindTerm" title="" type="single"
header-row-style="header-row" default-table-style="basic-table">
<field name="termName" title="${uiLabelMap.TermTermName}"><text-find/></field>
<field name="submitButton" title="${uiLabelMap.CommonFind}"><submit/></field>
</form>
<form name="ListFindTerm" list-name="listIt" title="" type="list" paginate-target="FindTerm"
odd-row-style="alternate-row" default-table-style="basic-table hover-bar">
<actions>
<service service-name="performFind" result-map="result" result-map-list="listIt">
<field-map field-name="inputFields" from-field="requestParameters"/>
<field-map field-name="entityName" value="Term"/>
<field-map field-name="viewIndex" from-field="viewIndex"/>
<field-map field-name="viewSize" from-field="viewSize"/>
<field-map field-name="orderBy" value="beginTime"/>
<field-map field-name="noConditionFind" value="Y"/>
</service>
</actions>
<field name="termName" title="${uiLabelMap.TermTermName}"><display/></field>
<field name="beginTime" title="${uiLabelMap.TermBeginTime}"><display/></field>
<field name="endTime" title="${uiLabelMap.TermEndTime}"><display/></field>
<field name="used" title="${uiLabelMap.TermUsed}"><display/></field>
</form>
</forms>
找了很久才找出原因,因为之前没有配置noConditionFind属性,所以导致查询条件为空时总查不到记录,加上下面配置后就能正常查询了。
<field-map field-name="noConditionFind" value="Y"/>