跳到主要內容

Struts2 - 如何將相關的操作放置於同一個Action中?

Problem

先前做的國軍登出倒數計時器中,有個正在受苦受難弟兄的排行榜,可以根據入伍日期、退伍日期、%數與剩餘天數去排序。除了排序外,還包含要取得不同種類資料的動作。我們可以選擇把這些動作寫成不同的Actions,但如果每一個都寫成一個Action,會讓Action類別很龐大。Struts提供了DispatchAction讓你可以封裝多個動作到一個Action中,以方便管理減少維護困難。

How to?

Java Action

我定義BillBoardAction做為DispatchAction的Controller負責與排行榜功能相關的邏輯流程控制。底下的程式碼主要讓大家知道大概是長怎樣,省略了很多實作細節。基本上就是一個操作對應一個method。
public class BillBoardAction extends ActionSupport 
			implements ServletRequestAware,ServletResponseAware {
	public String getPercentageBillboradInfo(){
		return Action.NONE;
	}
	public String getJoinDateBillboradInfo(){
		return Action.NONE;
	}
	public String getLeftDaysBillboradInfo(){
		return Action.NONE;
	}
	public String getEndDateBillboradInfo(){
		return Action.NONE;
	}
	public String getLogoutInformation(){
		return Action.NONE;
	}

Struts Config

struts config的定義,需要關注的就是name、method與class:

  • name: ui呼叫的Action名稱。
  • class: DispatchAction的類別名稱。
  • method: 對應功能的method名稱。
由於我是透過ajax方式去呼叫Action,因此我沒定義結果導向的設定。另外這個範例有使用到interceptor,用來確認session與認證相關的流程,這裡非本篇重點我不多做說明。
<action name="getLogoutInformation" method="getLogoutInformation"
	class="org.tonylin.logoutarmycd.web.action.BillBoardAction">
	<interceptor-ref name="basic-stack" />
</action>
<action name="getPercentageBillboard" method="getPercentageBillboradInfo"
	class="org.tonylin.logoutarmycd.web.action.BillBoardAction">
	<interceptor-ref name="basic-stack" />
</action>
<action name="getJoinDateBillboard" method="getJoinDateBillboradInfo"
	class="org.tonylin.logoutarmycd.web.action.BillBoardAction">
	<interceptor-ref name="basic-stack" />
</action>
<action name="getEndDateBillboard" method="getEndDateBillboradInfo"
	class="org.tonylin.logoutarmycd.web.action.BillBoardAction">
	<interceptor-ref name="basic-stack" />
</action>
<action name="getLeftDaysBillboard" method="getLeftDaysBillboradInfo"
	class="org.tonylin.logoutarmycd.web.action.BillBoardAction">
	<interceptor-ref name="basic-stack" />
</action>

UI HTML

html的部分相當單純,就只是顯示表格,你注意排序圖片的id即可:
<table id="BillboardTable" cellspacing="0" align="Center" border="1"
	class="table-stats"
	style="background-color: #CECFCE; border-width: 1px; border-style: solid; width: 97%; border-collapse: collapse;">
	<tr bgcolor="#cccccc" style="text-align: center; font-weight: bold;">
		<td width="30">排名</td>
		<td width="40%" align="left">使用者&nbsp;<label>
			<a href="#" style="text-decoration:none;" id="showAllLabel">顯示所有</a>
		</label></td>
		<td width="13%">入伍日期 <img id="jd-image" src="images/common/refresh.gif"
			class="Image-Moveon" title="照入伍日期排行"/></td>
		<td width="13%">退伍日期<img id="ld-image" src="images/common/refresh.gif"
			class="Image-Moveon" title="照退伍日期排行"/></td>
		<td width="13%">%數<img id="pbb-image" src="images/common/sort_desc.gif"
			class="Image-Moveon" title="照%數排行"/></td>
		<td width="13%">剩餘天數<img id="lds-image" src="images/common/refresh.gif"
			class="Image-Moveon" title="照剩餘天數排行"/></td>
	</tr>
</table>

UI Javascript

我在Javascript中定義了BillBoard的物件,我只取出部分重要程式碼做說明。BillBoard物件的宣告定義了各個元素id的綁定,最後會呼叫init的method:
function BillBoard(obj){
	var table = obj;
	var pbbImage = $('#pbb-image');
	var ldsImage = $('#lds-image');
	var ldImage = $('#ld-image');
	var jdImage = $('#jd-image');
 
	this.getBillBoard = function(){
		return table;
	};
	this.getPBBImage = function(){
		return pbbImage;
	};
	this.getLDSImage = function(){
		return ldsImage;
	};
	this.getLDImage = function(){
		return ldImage;
	};
	this.getJDImage = function(){
		return jdImage;
	};
	this.init();
}
在BillBoard的init中,我會將圖片綁定點擊事件,當User點擊圖片後會去呼叫sortBillboard的method,但會根據不同圖片有不同的參數值,以達到不同的排序效果:
BillBoard.prototype.init = function(){
	var thisObj = this;
	// init image event
	this.getPBBImage().click(function(){
			thisObj.sortBillboard('getPercentageBillboard.action','percentage');
	});
	this.getLDSImage().click( function(){
			thisObj.sortBillboard('getLeftDaysBillboard.action','leftDays');
	});
	this.getLDImage().click(function(){
			thisObj.sortBillboard('getEndDateBillboard.action','endDate');
	});
	this.getJDImage().click(function(){
			thisObj.sortBillboard('getJoinDateBillboard.action','joinDate');
 
	});
};
sortBillboard的method很純粹的會透過ajax呼叫一個action的url,最後再將結果顯示於頁面中:
BillBoard.prototype.sortBillboard = function(actionUrl, sortType){
	var thisObj = this;
	// Get the sorting result.
	$.ajax({
		  url: actionUrl,
		  type: "POST",
		  dataType: 'xml',
			 success: function(data) {
					//Show sorting result.
			 }, error: function(data) {
			   	window.alert("Link server failure.");
			}
	});
};

Summary

網路上最常看到的範例就是新增修改刪除查詢操作,而我則是使用以前的使用方式與大家做說明。善用DispatchAction可以透過集中管理方式減少大量的Action,也使得程式碼的維護較容易。

留言

這個網誌中的熱門文章

解決RobotFramework從3.1.2升級到3.2.2之後,Choose File突然會整個Hand住的問題

考慮到自動測試環境的維護,我們很久以前就使用java去執行robot framework。前陣子開始處理從3.1.2升級到3.2.2的事情,主要先把明確的runtime語法錯誤與deprecate item處理好,這部分內容可以參考: link 。 直到最近才發現,透過SeleniumLibrary執行Choose File去上傳檔案的動作,會導致測試案例timeout。本篇文章主要分享心路歷程與解決方法,我也送了一條issue給robot framework: link 。 我的環境如下: RobotFramework: 3.2.2 Selenium: 3.141.0 SeleniumLibrary: 3.3.1 Remote Selenium Version: selenium-server-standalone-3.141.59 首先並非所有Choose File的動作都會hang住,有些測試案例是可以執行的,但是上傳一個作業系統ISO檔案一定會發生問題。後來我透過wireshark去比對新舊版本的上傳動作,因為我使用 Remote Selenium ,所以Selenium會先把檔案透過REST API發送到Remote Selenium Server上。從下圖我們可以發現,在3.2.2的最後一個TCP封包,比3.1.2大概少了500個bytes。 於是就開始了我trace code之路。包含SeleniumLibrary產生要送給Remote Selenium Server的request內容,還有HTTP Content-Length的計算,我都確認過沒有問題。 最後發現問題是出在socket API的使用上,就是下圖的這支code: 最後發現可能因為開始使用nio的方式送資料,但沒處理到尚未送完的資料內容,而導致發生問題。加一個loop去做計算就可以解決了。 最後我有把解法提供給robot framework官方,在他們出新的版本之前,我是將改完的_socket.py放在我們自己的Lib底下,好讓我們測試可以正常進行。(shutil.py應該也是為了解某個bug而產生的樣子..)

第一次寫MIB就上手

SNMP(Simple Network Management Protocol)是用來管理網路設備的一種Protocol,我對它的認識也是從工作接觸開始。雖說是管理網路設備,但是主機、電源供應器、RAID等也都可以透過它來做管理。如果你做了一個應用程式,當然所有的操作也都可以透過SNMP來完成,不過可能會很痛苦。前陣子遇到一個學弟,它告訴我說:「我可能不會想寫程式。」為什麼? 因為這是他痛苦的根源。 在這篇文章中,不是要告訴你SNMP是什麼,會看這篇文章的大哥們,應該已經對SNMP有些認識了。 是的!主題是MIB(Management information base)! 對於一個3th-party的SNMP oid,有MIB可以幫助你去了解它所提供的資訊是什麼,且可以對它做什麼操作。最近我運氣很好剛好做到關於修改MIB的工作,也讓我順便了解一下它的語法,接下來我要交給大家MIB的基礎認識。 smidump 我並非使用什麼高強的Editor去編寫MIB,我僅透過Nodepad++編輯和smidump編譯而已。smidump是Kay教我使用的一個將MIB module轉成樹狀結構或oid列表的工具,唯一的缺點是不會告訴你哪一行打錯。當然有錢直接買編輯樹狀結構的工具就可以不需要了解語法了! 安裝 在Ubuntu上可先輸入smidump確認是否安裝,如果沒安裝可透過apt-get install libsmi2ldbl安裝。(CentOS可以透過yum install libsmi) root@tonylin:~/multi-boot-server# smidump The program 'smidump' is currently not installed. You can install it by typing: apt-get install libsmi2ldbl 使用 透過下面兩行指令,就可以將mib file產生出對應的tree與oid列表的檔案。也可以透過這個結果確認MIB是不是你想要的。 smidump -f tree example1.mib > xtree.txt smidump -f identifiers example1.mib > xiden.txt 如果有參考其它檔案要加上p的參數: smidum...

Robot Framework升級到4.1.2之後,為何Jenkins的Report突然不會報錯了?

最近我們在把Robot升級到3.2.2之後,我想說試試看4.1.2,於是就直接升了上去。沒想到daily build的測試沒發生任何異常。 但不幸的是Jenkins的報表怪怪的: 發生錯誤確Build Success! 趁著颱風假,比較了一下發現新版本的Critical tests測試項目皆為0: 查了一下 官方文件 發現是因為4.0之後,已經把Critical tests概念刪除了,詳細可以參考: Migrating from criticality to SKIP。 解決方法有3種, 自己定義Critical tests,不過看起來5.0就移掉了。 可能可以更新robot framework plugin。我們版本很舊,是1.6.4。因為目前還沒有更新jenkins的計畫,所以我使用了第三個方法。 Build成功與否的Threshold別用Critical tests。 最後測試結果: