struts2 Action里面的getter方法會(huì)在Action結(jié)束后被自動(dòng)調(diào)用,例如有此情況:
<package extends="struts-default">
<action name="MyAction">
<action name="AnotherAction" method="getSomething">
...
在Action中
execute
geta
getaA
getaa
getB
也就是說,即使你沒有調(diào)用AnotherAction,getSomething仍然會(huì)執(zhí)行。
---------------------------------------------------------------------------
090520更新
這個(gè)問題確實(shí)很惱人,這兩天又被此事折騰的崩潰。
再次提醒,在struts2的Action中慎起get打頭的函數(shù)!
出錯(cuò)的整個(gè)過程是這樣的(錯(cuò)的花了..):
在測(cè)試程序的過程中,發(fā)現(xiàn)后臺(tái)Hibernate不時(shí)的發(fā)生讀取臟數(shù)據(jù)的情況。
為此專門編寫了測(cè)試文件進(jìn)行測(cè)試,并沒有發(fā)現(xiàn)讀取臟數(shù)據(jù)的現(xiàn)象。眾所周知Session不是線程安全的,可系統(tǒng)是用ThreadLocal來管理Session的,按說不應(yīng)該出現(xiàn)這個(gè)問題。
我也曾懷疑過是sf緩存出的錯(cuò),可去掉緩存后還是有問題。
在打開了Session創(chuàng)建和銷毀的Log后,發(fā)現(xiàn)程序在Action返回后,又創(chuàng)建了新的Session(沒有關(guān)閉)。
注:系統(tǒng)在Action層對(duì)Service層做了IOC,在Service層對(duì)DAO層做了IOC。
結(jié)果就是,Action結(jié)束時(shí)調(diào)用了getXXXService函數(shù),而又神奇的調(diào)到了當(dāng)前Service所有g(shù)et打頭的函數(shù)。
最后的解決辦法是,此處的IOC并不需要getter,把getter刪除就行了。
<package extends="struts-default">
<action name="MyAction">
<action name="AnotherAction" method="getSomething">
...
在Action中
public String execute(){
System.out.println("execute");
return SUCCESS;
}
public String getA() {
System.out.println("getA");
return SUCCESS;
}
public String geta() {
System.out.println("geta");
return SUCCESS;
}
public String getaa() {
System.out.println("getaa");
return SUCCESS;
}
public String getaA() {
System.out.println("getaA");
return SUCCESS;
}
public String getB() {
System.out.println("getB");
return SUCCESS;
}
public String gotA() {
System.out.println("gotA");
return SUCCESS;
}
當(dāng)調(diào)用MyAction的時(shí)候,輸出結(jié)果為:System.out.println("execute");
return SUCCESS;
}
public String getA() {
System.out.println("getA");
return SUCCESS;
}
public String geta() {
System.out.println("geta");
return SUCCESS;
}
public String getaa() {
System.out.println("getaa");
return SUCCESS;
}
public String getaA() {
System.out.println("getaA");
return SUCCESS;
}
public String getB() {
System.out.println("getB");
return SUCCESS;
}
public String gotA() {
System.out.println("gotA");
return SUCCESS;
}
execute
geta
getaA
getaa
getB
也就是說,即使你沒有調(diào)用AnotherAction,getSomething仍然會(huì)執(zhí)行。
---------------------------------------------------------------------------
090520更新
這個(gè)問題確實(shí)很惱人,這兩天又被此事折騰的崩潰。
再次提醒,在struts2的Action中慎起get打頭的函數(shù)!
出錯(cuò)的整個(gè)過程是這樣的(錯(cuò)的花了..):
在測(cè)試程序的過程中,發(fā)現(xiàn)后臺(tái)Hibernate不時(shí)的發(fā)生讀取臟數(shù)據(jù)的情況。
為此專門編寫了測(cè)試文件進(jìn)行測(cè)試,并沒有發(fā)現(xiàn)讀取臟數(shù)據(jù)的現(xiàn)象。眾所周知Session不是線程安全的,可系統(tǒng)是用ThreadLocal來管理Session的,按說不應(yīng)該出現(xiàn)這個(gè)問題。
我也曾懷疑過是sf緩存出的錯(cuò),可去掉緩存后還是有問題。
在打開了Session創(chuàng)建和銷毀的Log后,發(fā)現(xiàn)程序在Action返回后,又創(chuàng)建了新的Session(沒有關(guān)閉)。
注:系統(tǒng)在Action層對(duì)Service層做了IOC,在Service層對(duì)DAO層做了IOC。
結(jié)果就是,Action結(jié)束時(shí)調(diào)用了getXXXService函數(shù),而又神奇的調(diào)到了當(dāng)前Service所有g(shù)et打頭的函數(shù)。
最后的解決辦法是,此處的IOC并不需要getter,把getter刪除就行了。