采用TinyDB組件方式開(kāi)發(fā)
Icon 前文介紹四則運(yùn)算的流程編程開(kāi)發(fā)時(shí),說(shuō)過(guò)流程編排在開(kāi)發(fā)重復(fù)功能時(shí),可以利用已有的組件庫(kù)快速開(kāi)發(fā)。對(duì)于開(kāi)發(fā)人員而言只需要簡(jiǎn)單配置流程就可以完成工作了。
開(kāi)發(fā)增刪改查的組件接口。本來(lái)這部分很花費(fèi)時(shí)間,如果采用組件復(fù)用的話(huà),就可以實(shí)現(xiàn)一次開(kāi)發(fā),終生受益。
- 配置curd.beans.xml和tinydb.xml。
- 使用流程編輯器定制組件流程curd.pageflow。
- 修改頁(yè)面文件:list.page和operate.page,使之符合流程方式調(diào)用。
- 修改布局文件default.layout。
完整的子工程目錄如下:
首先是開(kāi)發(fā)增刪改查的組件接口,如果是開(kāi)發(fā)人員自己實(shí)現(xiàn)的話(huà)就是實(shí)現(xiàn)ComponentInterface接口,然后在組件里實(shí)現(xiàn)具體的數(shù)據(jù)庫(kù)邏輯,其實(shí)Tiny框架提供基礎(chǔ)組件庫(kù),配合TinyDB進(jìn)行開(kāi)發(fā),可以實(shí)現(xiàn)無(wú)Pojo,無(wú)Dao層,在本示例中開(kāi)發(fā)人員不用寫(xiě)一行Java代碼,通過(guò)配置就可以完成示例需求。
基礎(chǔ)組件資源
Icon目前Tiny框架提供如下幾種組件庫(kù),用戶(hù)可以根據(jù)實(shí)際需要配合流程編輯器使用。
- org.tinygroup.flowbasiccomponent 提供了邏輯基本組件,如對(duì)象轉(zhuǎn)換組件、調(diào)用靜態(tài)方法和bean組件等。
- org.tinygroup.flowservicecomponent 提供了邏輯基本服務(wù),如調(diào)用服務(wù)、調(diào)用本地流程、調(diào)用頁(yè)面流程等。
- org.tinygroup.tinydbflowcomponent 提供了TinyDB數(shù)據(jù)庫(kù)組件,如插入組件、更新組件、刪除組件、查詢(xún)記錄組件等。
- org.tinygroup.pageflowbasiccomponent 提供了頁(yè)面基本組件,如頁(yè)面重定向、頁(yè)面轉(zhuǎn)發(fā)等。
開(kāi)發(fā)人員想要使用使用組件庫(kù)中的內(nèi)置組件,必須將具體的依賴(lài)加入到工程的pom.xml,這樣在使用圖形化工具,在右側(cè)的組件列表就能看到組件清單:
本示例用到了數(shù)據(jù)庫(kù)訪問(wèn)和頁(yè)面跳轉(zhuǎn),pom.xml里面需要有如下依賴(lài):
<dependency> <groupId>org.tinygroup</groupId> <artifactId>org.tinygroup.tinydbflowcomponent</artifactId> <version>${tiny_version}</version> </dependency> <dependency> <groupId>org.tinygroup</groupId> <artifactId>org.tinygroup.pageflowbasiccomponent</artifactId> <version>${tiny_version}</version> < /dependency> |
配置crud.beans.xml可以復(fù)用先前TinyDB采用服務(wù)方式的配置文件,只需要把如下內(nèi)容刪除:
<bean id="tinyDbCrudService" class="org.tinygroup.crud.service.impl.TinyDbCrudService" scope="singleton"> <property name="beanType" value="TUser" /> < /bean> |
因?yàn)楸臼纠](méi)有配置tinyDbCrudService,如果不刪除的話(huà),Web應(yīng)用啟動(dòng)時(shí)會(huì)報(bào)異常。至于tinydb.xml文件無(wú)需任何修改,可以直接復(fù)用先前例子。
接下來(lái)按“New”-“Other”-“Tiny框架”-“頁(yè)面流程”順序,創(chuàng)建crud.pageflow,然后按下圖拖曳組件:
接下來(lái)修改組件的基本信息:標(biāo)識(shí)、英文名和中文名。以插入組件為例,鼠標(biāo)選中畫(huà)板里的“插入組件”,在Eclipse的下方的“Properties”,就可以看到如下內(nèi)容:
五個(gè)組件修改完畢,界面如下圖展示:
然后是配置組件的擴(kuò)展屬性,每個(gè)組件的擴(kuò)展屬性是根據(jù)自身功能定制的,具體的組件參數(shù)請(qǐng)參考各組件的幫助文檔。這里還是以“新增用戶(hù)”為例:
說(shuō)明:這里類(lèi)型就是數(shù)據(jù)庫(kù)表的值對(duì)象類(lèi)型TUser,模式是指數(shù)據(jù)庫(kù)的schema,其他幾個(gè)組件也是類(lèi)似。
配置好五個(gè)組件的擴(kuò)展屬性,就是配置頁(yè)面組件的擴(kuò)展屬性。頁(yè)面組件的擴(kuò)展屬性就一個(gè):頁(yè)面路徑。
頁(yè)面重定向的配置如下:
查詢(xún)單用戶(hù)對(duì)應(yīng)的頁(yè)面轉(zhuǎn)發(fā)配置如下:
查詢(xún)用戶(hù)列表對(duì)應(yīng)的頁(yè)面轉(zhuǎn)發(fā)配置如下:
完整的curd.pageflow的內(nèi)容如下:
- <flow id="crud" enable="true" private-context="false">
- <parameters/>
- <nodes>
- <node id="start" name="start" title="開(kāi)始">
- <next-nodes>
- <next-node next-node-id="addUser"/>
- <next-node next-node-id="updateUser"/>
- <next-node next-node-id="deleteUserById"/>
- <next-node next-node-id="getUserById"/>
- <next-node next-node-id="redirectComponent"/>
- <next-node next-node-id="queryUsers"/>
- </next-nodes>
- </node>
- <node id="addUser" name="addUser" title="新增用戶(hù)">
- <component name="tinydbAddService" title="插入組件">
- <properties>
- <flow-property name="beanType" value="TUser"/>
- <flow-property name="schema" value="sample"/>
- </properties>
- </component>
- <next-nodes>
- <next-node next-node-id="redirectComponent"/>
- </next-nodes>
- </node>
- <node id="updateUser" name="updateUser" title="更新用戶(hù)">
- <component name="tinydbUpdateService" title="更新組件">
- <properties>
- <flow-property name="beanType" value="TUser"/>
- <flow-property name="schema" value="sample"/>
- </properties>
- </component>
- <next-nodes>
- <next-node next-node-id="redirectComponent"/>
- </next-nodes>
- </node>
- <node id="deleteUserById" name="deleteUserById" title="刪除用戶(hù)">
- <component name="tinydbDeleteService" title="刪除組件">
- <properties>
- <flow-property name="beanType" value="TUser"/>
- <flow-property name="schema" value="sample"/>
- </properties>
- </component>
- <next-nodes>
- <next-node next-node-id="redirectComponent"/>
- </next-nodes>
- </node>
- <node id="getUserById" name="getUserById" title="查詢(xún)單用戶(hù)">
- <component name="tinydbQueryServiceWithId" title="單記錄查詢(xún)組件">
- <properties>
- <flow-property name="beanType" value="TUser"/>
- <flow-property name="schema" value="sample"/>
- <flow-property name="primaryKey" value="${primaryKey}"/>
- <flow-property name="resultKey" value="user"/>
- </properties>
- </component>
- <next-nodes>
- <next-node next-node-id="forwardComponent"/>
- </next-nodes>
- </node>
- <node id="forwardComponent" name="forwardComponent" title="頁(yè)面轉(zhuǎn)發(fā)">
- <component name="forwardComponent" title="頁(yè)面轉(zhuǎn)發(fā)">
- <properties>
- <flow-property name="path" value="/crud/operate.page"/>
- </properties>
- </component>
- <next-nodes>
- <next-node next-node-id="end"/>
- </next-nodes>
- </node>
- <node id="redirectComponent" name="redirectComponent" title="頁(yè)面重定向">
- <component name="redirectComponent" title="頁(yè)面重定向">
- <properties>
- <flow-property name="path" value="crud.pageflow?tiny_flow_id=queryUsers"/>
- </properties>
- </component>
- <next-nodes>
- <next-node next-node-id="end"/>
- </next-nodes>
- </node>
- <node id="queryUsers" name="queryUsers" title="查詢(xún)用戶(hù)列表">
- <component name="tinydbQueryService" title="查詢(xún)組件">
- <properties>
- <flow-property name="beanType" value="TUser"/>
- <flow-property name="schema" value="sample"/>
- <flow-property name="resultKey" value="users"/>
- </properties>
- </component>
- <next-nodes>
- <next-node next-node-id="forwardComponent_1"/>
- </next-nodes>
- </node>
- <node id="forwardComponent_1" name="forwardComponent" title="頁(yè)面轉(zhuǎn)發(fā)">
- <component name="forwardComponent" title="頁(yè)面轉(zhuǎn)發(fā)">
- <properties>
- <flow-property name="path" value="/crud/list.page"/>
- </properties>
- </component>
- <next-nodes>
- <next-node next-node-id="end"/>
- </next-nodes>
- </node>
- <node id="end" name="end" title="結(jié)束">
- <next-nodes/>
- </node>
- </nodes>
- < /flow>
操作頁(yè)面operate.page代碼如下:
- <form action="${TINY_CONTEXT_PATH}/crud.pageflow">
- 姓名:<input type="text" name="name" value="${user?.name}" /><br/>
- 年齡:<input type="text" name="age" value="${user?.age}" /><br/>
- <input type="hidden" name="id" value="${user?.id}"/>
- #if($user)
- <input type="hidden" name="tiny_flow_id" value="updateUser"/>
- #else
- <input type="hidden" name="tiny_flow_id" value="addUser"/>
- #end
- < input type="submit" value="提交">
- < /form>
列表頁(yè)面list.page代碼如下:
- 用戶(hù)管理界面:
- <form>
- < div>
- <p>
- <input type="button" id="add" value="添加"/>
- <input type="button" id="update" value="修改"/>
- <input type="button" id="delete" value="刪除"/>
- </p>
- <table cellpadding="0" cellspacing="1" border="0" bgcolor="#ebebeb" width="500px">
- <tbody>
- <tr bgcolor="#ffffff">
- <th width="35"><input type="checkbox" id="selectAll"/></th>
- <th width="100px" height="35">名稱(chēng)</th>
- <th width="365px" >年齡</th>
- </tr>
- #foreach($user in $users)
- <tr bgcolor="#ffffff">
- <td align="center"><input type="checkbox" name="id" value="$user.id"/></td>
- <td align="center" height="30">$user.name</td>
- <td align="center">$user.age</td>
- </tr>
- #end
- </tbody>
- </table>
- < /div>
- < form>
- < script>
- $(document).ready(function(){
- $("#selectAll").click(function(){
- var checked=$(this).get(0).checked;
- $(":checkbox:not('#selectAll')").each(function(){
- $(this).get(0).checked=checked;
- });
- });
- $("#add").click(function(){
- location.href="${TINY_CONTEXT_PATH}/crud/operate.page";
- }
- );
- $("#update").click(function(){
- var checkboxs=$(":checked:not('#selectAll')");
- var size=checkboxs.size();
- if(size==0){
- alert("修改前請(qǐng)先選擇記錄");
- }else if(size>1){
- alert("只能選擇一條記錄進(jìn)行修改");
- }else{
- var checkbox=checkboxs.get(0);
- location.href="${TINY_CONTEXT_PATH}/crud.pageflow?primaryKey="+checkbox.value+"&tiny_flow_id=getUserById";
- }
- }
- );
- $("#delete").click(function(){
- if(confirm("確定要?jiǎng)h除選擇的記錄嗎?")){
- var checkboxs=$(":checked:not('#selectAll')");
- var size=checkboxs.size();
- if(size==0){
- alert("刪除前請(qǐng)先選擇記錄");
- }else if(size>1){
- alert("只能選擇一條記錄進(jìn)行刪除");
- }else{
- var checkbox=checkboxs.get(0);
- location.href="${TINY_CONTEXT_PATH}/crud.pageflow?id="+checkbox.value+"&tiny_flow_id=deleteUserById";
- }
- }
- }
- );
- });
- < /script>
默認(rèn)布局文件default.layout的配置如下:
- <table border="1" width="100%">
- <tr>
- <td colspan="2">
- <a href="${TINY_CONTEXT_PATH}/crud.pageflow?tiny_flow_id=queryUsers">用戶(hù)管理</a><br/>
- </td>
- </tr>
- <tr>
- <td width="20%">tinydb流程方式</td>
- <td>
- ${pageContent}
- </td>
- </tr>
- < /table>
到這一步,流程編排的例子算是開(kāi)發(fā)完畢。
演示效果
具體的增刪改查效果,用戶(hù)可以根據(jù)教程自行嘗試。
歡迎訪問(wèn)開(kāi)源技術(shù)社區(qū):http://bbs.tinygroup.org。本例涉及的代碼和框架資料,將會(huì)在社區(qū)分享。《自己動(dòng)手寫(xiě)框架》成員QQ群:228977971,一起動(dòng)手,了解開(kāi)源框架的奧秘!或點(diǎn)擊加入QQ群:http://jq.qq.com/?_wv=1027&k=d0myfX