0
我連接一個簡單的JSF生命週期偵聽到我webbapp:爲什麼有時會調用多個JSF生命週期?
public class LifeCycleListener implements PhaseListener
{
@Override
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
@Override
public void beforePhase(PhaseEvent event) {
System.out.println("START PHASE " + event.getPhaseId());
}
@Override
public void afterPhase(PhaseEvent event) {
System.out.println("END PHASE " + event.getPhaseId());
}
}
在網頁中的一個予有不便這樣的:
<h:form>
<h:commandLink id="avdertAddedToMainPage" action="#{topView.showMainPage}">
Main Page
</h:commandLink>
</h:form>
其中冠捷在faces-配置配置爲請求簡單豆-scoped:
public class TopView {
public TopView() {
}
public String showAddAdvert()
{
return "addAdvert";
}
public String showMainPage()
{
return "itemList";
}
}
令我奇怪的是,如果我點擊上面的鏈接,似乎一切都正常工作,但根據LiceCycleLi在頁面加載之前,stener的輸出每個階段運行2次(昨天我計數4次,但這是因爲線程問題)。這是正常的行爲嗎?如果這表明我的代碼中存在某種錯誤,我應該在哪裏查找它?
我用鑽嘴魚科2.0.2在Glassfish 3.0.1
這是從今天的嘗試我的輸出。只有兩輪的生命週期,但昨天我有4個。當我看着yestarday的日誌時,發現我有多個線程,所以我猜想它在服務器運行時修改我的應用程序並不合適。但我仍然不知道2輪生命週期是否是件好事。另外請注意,幾乎沒有關於設置字符編碼的警告,但我不知道這是否重要。
FINE: [Web-Security] Policy Context ID was: Powypadkowe/Powypadkowe
FINE: [Web-Security] hasUserDataPermission perm: (javax.security.jacc.WebUserDataPermission /faces/AdvertAddedCl.xhtml POST)
FINE: [Web-Security] hasUserDataPermission isGranted: true
FINE: [Web-Security] Policy Context ID was: Powypadkowe/Powypadkowe
FINE: [Web-Security] hasResource isGranted: true
FINE: [Web-Security] hasResource perm: (javax.security.jacc.WebResourcePermission /faces/AdvertAddedCl.xhtml POST)
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /powypadkowe, because request parameters have already been read, or ServletRequest.getReader() has already been called
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /powypadkowe, because request parameters have already been read, or ServletRequest.getReader() has already been called
INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: ------- some hibernate sql selects -------
INFO: END PHASE RENDER_RESPONSE 6
FINE: SecurityContext: setCurrentSecurityContext method called
FINE: [Web-Security] Policy Context ID was: Powypadkowe/Powypadkowe
FINE: [Web-Security] hasUserDataPermission perm: (javax.security.jacc.WebUserDataPermission /faces/photos/[email protected]/Atojest/item_1/1m.jpg GET)
FINE: [Web-Security] hasUserDataPermission isGranted: true
FINE: [Web-Security] Policy Context ID was: Powypadkowe/Powypadkowe
FINE: [Web-Security] hasResource isGranted: true
FINE: [Web-Security] hasResource perm: (javax.security.jacc.WebResourcePermission /faces/photos/[email protected]/Atojest/item_1/1m.jpg GET)
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /powypadkowe, because request parameters have already been read, or ServletRequest.getReader() has already been called
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /powypadkowe, because request parameters have already been read, or ServletRequest.getReader() has already been called
INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: END PHASE RENDER_RESPONSE 6
FINE: SecurityContext: setCurrentSecurityContext method called
而且yestarday日誌的片段:
[#|2012-06-04T22:05:49.158+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=30;_ThreadName=Thread-1;|END PHASE UPDATE_MODEL_VALUES 4|#]
[#|2012-06-04T22:05:49.158+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=33;_ThreadName=Thread-1;|START PHASE RESTORE_VIEW 1|#]
[#|2012-06-04T22:05:49.161+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=31;_ThreadName=Thread-1;|START PHASE RENDER_RESPONSE 6|#]
[#|2012-06-04T22:05:49.162+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=33;_ThreadName=Thread-1;|END PHASE RESTORE_VIEW 1|#]
[#|2012-06-04T22:05:49.163+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=33;_ThreadName=Thread-1;|START PHASE APPLY_REQUEST_VALUES 2|#]
[#|2012-06-04T22:05:49.261+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=32;_ThreadName=Thread-1;|START PHASE APPLY_REQUEST_VALUES 2|#]
[#|2012-06-04T22:05:49.261+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=33;_ThreadName=Thread-1;|END PHASE APPLY_REQUEST_VALUES 2|#]
對於「頁面加載之前」,你究竟是什麼意思?在webapp的啓動過程中只有一次?或者*每次*當你點擊鏈接? – BalusC
每次點擊鏈接 – GrzesiekC
根據目前提供的代碼,這絕對不是正常行爲。你在一個完全空白的JSF項目項目中測試過嗎?或者它是一個現有的Web應用程序?如果是這樣,其他事情可能會造成這種情況。可能由於某種原因,響應已被轉發/重定向數次。只有一次點擊鏈接時,您看到的輸出是什麼? – BalusC