为什么有时会调用多个JSF生命周期?

时间:2012-06-05 13:39:58

标签: jsf facelets lifecycle

我将一个简单的jsf生命周期监听器连接到我的webbapp:

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>

其中topView是faces-config中配置为请求范围的简单bean:

public class TopView {

public TopView() {
}

public String showAddAdvert()
{
    return "addAdvert";
}

public String showMainPage()
{
    return "itemList";
}

}

让我想知道的是,如果我点击上面的链接,一切似乎都能正常工作,但根据LiceCycleListener的输出,每个阶段运行2次(昨天我算了4次,但这是因为线程问题)在页面加载之前。这是正常的行为吗?如果这表明我的代码中存在某种错误,我应该在哪找?

我在Glassfish 3.0.1上使用Mojarra 2.0.2

这是今天尝试的输出。只有两轮生命周期,但是昨天我有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/nowy17@mail.pl/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/nowy17@mail.pl/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|#]

1 个答案:

答案 0 :(得分:0)

我不知道这是假设的工作方式,但它与我对实际情况的观察相符。