Total Pageviews

2011/06/02

利用enumeration type來做下拉選單

我們在開發的時候,會遇到某些下拉選單不是從資料庫抓出來的,寫死在jsp好像不太恰當,寫在property file再抓出來又有點麻煩,如下圖所示,輸出格式、法務科別、下授類別等,下拉單內容很固定、又不需要放在資料庫:


這邊我建議用Java的enumeration type來建立下拉選單。步驟如下(以法務科別為例):

1. 建立enumeration class (若下選選單有增減,只要修改enumeration即可)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   package gov.fdc.nig.enumeration;
   
   /**
    * 法務科別 enumeration
    *
    * @author albert
    *
    */
   public enum LegalSectionEnum {
   
       SECTION1("1.法務一科", "1"), SECTION2("2.法務二科", "2"), ALL("3.全部", "3");
   
       private final String label;
       private final String value;
   
       public String getLabel() {
           return label;
       }
   
       public String getValue() {
           return value;
       }
   
       private LegalSectionEnum(String label, String value) {
           this.label = label;
           this.value = value;
       }
   
       public static void main(String args[]) {
           LegalSectionEnum[] enumArr = LegalSectionEnum.values();
           for (LegalSectionEnum enumeration: enumArr) {
               System.out.println("label=" + enumeration.getLabel() + ", value=" +
                   enumeration.getValue());
           }
       }
   }

2. 在NIGUtilsConroller增加一個method來抓取enumeration中所定義的values
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   /**
    * 取得法務科別下拉單
    * 
    * @param res loadDelegationUnitList
    * @param session HttpSession
    * @throws IOException if exception happens
    */
   @RequestMapping(value = "/loadLegalSectionList", method = RequestMethod.POST)
   public @ResponseBody
   void loadLegalSectionList(final HttpServletResponse res, final HttpSession session)
   throws IOException {
       final JSONArray jsonArray = new JSONArray();
   
       try {
           final LegalSectionEnum[] enumArr = LegalSectionEnum.values();
   
           for (LegalSectionEnum enumeration: enumArr) {
               Map map = new HashMap();
               map.put("value", enumeration.getValue());
               map.put("name", enumeration.getLabel());
   
               jsonArray.add(map);
           }
       } catch (FDCDataAccessException e) {
           LoggerFactory.getLogger(this.getClass()).debug(
               "loadExportFormatList exception: " + e.getMessage());
       }
   
       res.setContentType(NigAccessKey.JKEY_CONTENT_TYPE);
       res.getWriter().write(jsonArray.toString());
   }


3. 在nig.utils.js增加一個function來取得法務科別下拉單


4. 在功能相對應的js,呼叫nig.utils.js所定義的JavaScript function


JavaScript function所定義的id,與下拉選單的id需要對應

2011/05/31

eclipse-fonts

Easily control Eclipse fonts size. Update site: http://eclipse-fonts.googlecode.com/svn/trunk/FontsUpdate/

Shortcuts -

CTRL+= : Increase fonts

CTRL+- : Decrease fonts

2011/05/28

My Travel Brain

Travel Map
I've been to 38 cities in 11 countries
Asia
China: Shenzhen
Hong Kong (SAR): Hong Kong Island
Japan: Asahikawa
Japan: Fukuoka
Japan: Hakodate
Japan: Hokkaido
Japan: Kyoto
Japan: Nagano
Japan: Nagoya
Japan: Onuma
Japan: Sapporo
Japan: Sendai
Japan: Tokyo
Malaysia: Kuala Lumpur
Singapore: Singapore
South Korea: Seoul
Taiwan: T'ai-nan
Taiwan: T'ai-pei
Taiwan: T'ao-yuan
Taiwan: Yilan City
Europe
France: Arles
France: Avignon
France: Carcassonne
France: Entrevaux
France: Gordes
France: Le Fugeret
France: Nice
France: Nimes
France: Paris
France: Roussillon
France: Saint-Paul
France: Sorgues
Italy: Milano
Monaco: Monte-Carlo
North America
Canada: Toronto

Utilize Commons BeansUtils to do copy object


2011/05/27

2011/5 France

摩納哥

尼斯

蔚藍海岸

St. Paul

Aix(Aix-en-Provence的首都)

Roussillon一隅

Gordes

Fontaine-de--Vaucluse(沃克呂姿碧泉村)

Pont St. Benezet(亞維農斷橋)

嘉德水道橋(Pont du Gard)

凱旋門

Carcasonne

Eiffel Tower

羅浮宮

聖母院

Eiffel Tower夜景


2011/04/07

Utilize Recursive Java Decompiler to Decompile Jar File



2. Unzip jd-gui-0.3.3.windows.zip and execute jd-gui.exe

3. assign jar file which you want to do decompile



4. File --> Save All Sources




6. Install Doxygen

7. Assign project name, source code directory, destination directory. And check "Scan
recursively"


8. Change to "Mode", and do some configuration

9. Change to "Output", and do some configuration

10. Change to "Diagram", and do some configuration

11. Click "Run" tab, and click "Run doxygen"

12. Check result

2011/04/01

Extract Method Refactoring Example

Extract Method is a refactoring operation that provides an easy way to create a new method from a code fragment in an existing member.

Using Extract Method, you can create a new method by extracting a selection of code from inside the code block of an existing member. The new, extracted method contains the selected code, and the selected code in the existing member is replaced with a call to the new method. Turning a fragment of code into its own method lets you quickly and accurately reorganize code for better reuse and readability.

Extract Method has the following benefits:
1. Encourages best coding practices by emphasizing discrete, reusable methods.
2. Encourages self-documenting code through good organization.
3. When descriptive names are used, high-level methods can read more like a series of comments.
4. Encourages the creation of finer-grained methods to simplify overriding.
5. Reduces code duplication.

實例說明
我們在Controller中,會撰寫新增、修改、刪除等method,其中在新增與修改的部份,會將前端form bean的value轉成entity class,然後再呼叫insert or update method,其code snippets如下:

Extract Method Refactoring三部曲
1. 選取要進行refactor的部分

2. 按右鍵-->Refactor-->Extract Method

3. 給予新的method name --> OK


檢查Refactor後的結果

Eclipse將原本set的動作抽離成一個獨立的method


insert與update的method中,原本重複的動作只要用covertBeanToEntity此method取代即可,避免code duplication

2011/03/25

Implementing a simple toString() with Apache Commons

我們在Web開發的時候,JSP頁面參數是透過form bean傳遞,有時候需要印出form bean裡頭的valuedebug,大家可以在form bean中,override toString(),如以下寫法

public String toString() {

return ToStringBuilder.reflectionToString(this);

}

實作如下

public static void main(String[] args) {

// set value to value object

Nigw002PK vo = new Nigw002PK();

vo.setCarryDate("1");

vo.setCarrySeqNo("2");

vo.setCaseStus("3");

vo.setMtgTp("4");

// print value object by toString()

System.out.println(vo.toString());

}

印出結果入下:gov.fdc.nig.domain.Nigw002PK@63396339[mtgTp=4,caseStus=3,taxCd=,carryDate=1,carrySeqNo=2,vioNo=>]

Reference: http://blogs.bytecode.com.au/glen/2007/10/05/implementing-a-simple-tostring---with-apache-commons.html

2011/03/16

WTRN0037W: The transaction service encountered an error on an xa_recover operation


As I start up WAS(WebSphere Application Server) V7, it show error messages as bellows:
[2011/3/16 17:30:48:784 CST] 0000000b XARminst E WTRN0037W: The transaction service encountered an error on an xa_recover operation. The resource was com.ibm.ws.rsadapter.spi.WSRdbXaResourceImpl@702d702d. The error code was XAER_RMERR. The exception stack trace follows: javax.transaction.xa.XAException
at oracle.jdbc.xa.OracleXAResource.recover(OracleXAResource.java:709)
at com.ibm.ws.rsadapter.spi.WSRdbXaResourceImpl.recover(WSRdbXaResourceImpl.java:1044)
at com.ibm.ws.Transaction.JTA.XARminst.recover(XARminst.java:140)
at com.ibm.ws.Transaction.JTA.XARecoveryData.recover(XARecoveryData.java:571)
at com.ibm.tx.jta.PartnerLogTable.recover(PartnerLogTable.java:389)
at com.ibm.tx.jta.RecoveryManager.resync(RecoveryManager.java:1523)
at com.ibm.ws.tx.jta.RecoveryManager.performResync(RecoveryManager.java:126)
at com.ibm.tx.jta.RecoveryManager.run(RecoveryManager.java:2200)
at java.lang.Thread.run(Thread.java:735)

Solution
1. Shut down WAS(WebSphere Application Server) V7

2. Change directory to transaction logs foldler: [WebSphere Application Server_install_root]\profiles\[PROFILE_NAME]\tranlog\[CELL_NAME]\[NODE_NAME]\[SERVER_NAME]\transaction


3. Delete all folder and files

4. Start up server again

5. Do not have error message any more

6. And log into admin console successfully.