星期一, 7月 16, 2007

sturts 2 validation and action property

今天又犯了個豬頭錯誤,我有兩個Struts Action,程式碼類似這樣:
public class ParentAction {
protected long property;
...
public long getProperty(){
return property;
}
}

public class ChildAction extends ParentAction{
public void setProperty(int property ){
this.property = property;
}
}

當我對ChildAction 加上required validation時,問題就來了,不論我輸入的是什麼值,validation interceptor總是告訴會回報property is null,找了半天,一路trace到Ognl層,才發現問題出在,透過reflection API取得property 的method descriptor時,因為setProperty的參數型態和getProperty的參數型態不符,而getProperty又在parent,所以會只認getProperty,而把setProperty(int) 當作是一個overloading method,以致於無法設值進去,只要把setProperty 由setProperty(int)改成setProperty(long),就又能夠正常運作了。

這樣的錯誤,只能說是自己不小心造成的,但是,這種錯,應該會常常發生吧,下次,得把這類的檢查也加到test case中。

沒有留言: