めもめも

このブログに記載の内容は個人の見解であり、必ずしも所属組織の立場、戦略、意見を代表するものではありません。

JSF2.0のLifeCycle

JSF2.0のLifeCycleをPhaseListenerを使って確認しました。

ソース

index.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
	<title>LifeCycleCheck</title>
	<f:phaseListener type="lib.PhaseTracker" />
</h:head>
<h:body>
	<h:form>
		Output: <h:outputText id="output" value="#{mybean.text}" />
		<br />
		Ajax Input:
		<h:inputText value="#{mybean.text}">
			<f:ajax event="blur" execute="@this" render="output" />
		</h:inputText>
		<br />
		Normal Input:
		<h:inputText value="#{mybean.text}">
			<f:valueChangeListener type="lib.MyValueChangeListener" />
		</h:inputText>
		<br />
		<h:commandButton action="index.xhtml" value="Submit">
			<f:actionListener type="lib.MyActionListener" />
		</h:commandButton>
		<h:commandButton action="index.xhtml" value="Immediate"
			immediate="true">
			<f:actionListener type="lib.MyActionListener" />
		</h:commandButton>
	</h:form>
</h:body>
</html>

lib/PhaseTracker.java

package lib;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

public class PhaseTracker implements PhaseListener {
	private static final long serialVersionUID = -2917841296997807166L;

	@Override
	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}

	@Override
	public void beforePhase(PhaseEvent event) {
		System.out.println( "Before: " + event.getPhaseId().toString());
	}

	@Override
	public void afterPhase(PhaseEvent event) {
		System.out.println("After: " + event.getPhaseId().toString());
	}
}

lib/MyActionListener.java

package lib;

import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

public class MyActionListener implements ActionListener {
	@Override
	public void processAction(ActionEvent event) throws AbortProcessingException {
		System.out.println("ActionListener: " + event.getPhaseId().toString());
	}
}

lib/MyValueChangeListener.java

package lib;

import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;

public class MyValueChangeListener implements ValueChangeListener {
	@Override
	public void processValueChange(ValueChangeEvent event)
			throws AbortProcessingException {
		System.out.println("ValueChangeListener: " + event.getPhaseId().toString());
	}
}

実行結果

初回画面表示

10:30:26,024 INFO  [stdout] (http--127.0.0.1-8080-1) Before: RENDER_RESPONSE 6
10:30:26,058 INFO  [stdout] (http--127.0.0.1-8080-1) After: RENDER_RESPONSE 6

テキストを入力してSubmit

10:30:44,771 INFO  [stdout] (http--127.0.0.1-8080-1) Before: APPLY_REQUEST_VALUES 2
10:30:44,772 INFO  [stdout] (http--127.0.0.1-8080-1) After: APPLY_REQUEST_VALUES 2
10:30:44,772 INFO  [stdout] (http--127.0.0.1-8080-1) Before: PROCESS_VALIDATIONS 3
10:30:44,795 INFO  [stdout] (http--127.0.0.1-8080-1) ValueChangeListener: ANY 0
10:30:44,796 INFO  [stdout] (http--127.0.0.1-8080-1) After: PROCESS_VALIDATIONS 3
10:30:44,796 INFO  [stdout] (http--127.0.0.1-8080-1) Before: UPDATE_MODEL_VALUES 4
10:30:44,797 INFO  [stdout] (http--127.0.0.1-8080-1) After: UPDATE_MODEL_VALUES 4
10:30:44,797 INFO  [stdout] (http--127.0.0.1-8080-1) Before: INVOKE_APPLICATION 5
10:30:44,797 INFO  [stdout] (http--127.0.0.1-8080-1) ActionListener: INVOKE_APPLICATION 5
10:30:44,798 INFO  [stdout] (http--127.0.0.1-8080-1) After: INVOKE_APPLICATION 5
10:30:44,800 INFO  [stdout] (http--127.0.0.1-8080-1) Before: RENDER_RESPONSE 6
10:30:44,801 INFO  [stdout] (http--127.0.0.1-8080-1) After: RENDER_RESPONSE 6

テキストを変更せずに再度Submit

10:31:34,297 INFO  [stdout] (http--127.0.0.1-8080-1) Before: APPLY_REQUEST_VALUES 2
10:31:34,298 INFO  [stdout] (http--127.0.0.1-8080-1) After: APPLY_REQUEST_VALUES 2
10:31:34,298 INFO  [stdout] (http--127.0.0.1-8080-1) Before: PROCESS_VALIDATIONS 3
10:31:34,299 INFO  [stdout] (http--127.0.0.1-8080-1) After: PROCESS_VALIDATIONS 3
10:31:34,299 INFO  [stdout] (http--127.0.0.1-8080-1) Before: UPDATE_MODEL_VALUES 4
10:31:34,300 INFO  [stdout] (http--127.0.0.1-8080-1) After: UPDATE_MODEL_VALUES 4
10:31:34,300 INFO  [stdout] (http--127.0.0.1-8080-1) Before: INVOKE_APPLICATION 5
10:31:34,300 INFO  [stdout] (http--127.0.0.1-8080-1) ActionListener: INVOKE_APPLICATION 5
10:31:34,301 INFO  [stdout] (http--127.0.0.1-8080-1) After: INVOKE_APPLICATION 5
10:31:34,302 INFO  [stdout] (http--127.0.0.1-8080-1) Before: RENDER_RESPONSE 6
10:31:34,303 INFO  [stdout] (http--127.0.0.1-8080-1) After: RENDER_RESPONSE 6

テキストを変更してImmediate属性のSubmit

10:32:47,482 INFO  [stdout] (http--127.0.0.1-8080-1) Before: APPLY_REQUEST_VALUES 2
10:32:47,483 INFO  [stdout] (http--127.0.0.1-8080-1) ActionListener: APPLY_REQUEST_VALUES 2
10:32:47,483 INFO  [stdout] (http--127.0.0.1-8080-1) After: APPLY_REQUEST_VALUES 2
10:32:47,485 INFO  [stdout] (http--127.0.0.1-8080-1) Before: RENDER_RESPONSE 6
10:32:47,487 INFO  [stdout] (http--127.0.0.1-8080-1) After: RENDER_RESPONSE 6

※テキストの変更は反映されません。

Ajaxなテキストフィールドの変更

11:17:22,118 INFO  [stdout] (http--127.0.0.1-8080-1) Before: APPLY_REQUEST_VALUES 2
11:17:22,119 INFO  [stdout] (http--127.0.0.1-8080-1) After: APPLY_REQUEST_VALUES 2
11:17:22,119 INFO  [stdout] (http--127.0.0.1-8080-1) Before: PROCESS_VALIDATIONS 3
11:17:22,120 INFO  [stdout] (http--127.0.0.1-8080-1) After: PROCESS_VALIDATIONS 3
11:17:22,121 INFO  [stdout] (http--127.0.0.1-8080-1) Before: UPDATE_MODEL_VALUES 4
11:17:22,122 INFO  [stdout] (http--127.0.0.1-8080-1) After: UPDATE_MODEL_VALUES 4
11:17:22,123 INFO  [stdout] (http--127.0.0.1-8080-1) Before: INVOKE_APPLICATION 5
11:17:22,123 INFO  [stdout] (http--127.0.0.1-8080-1) After: INVOKE_APPLICATION 5
11:17:22,124 INFO  [stdout] (http--127.0.0.1-8080-1) Before: RENDER_RESPONSE 6
11:17:22,127 INFO  [stdout] (http--127.0.0.1-8080-1) After: RENDER_RESPONSE 6