Ok, I not only survived a horrible breakdown. I need to be a bit patient and hopefully I’ll be fully restored. Time to talk about life cycle. Lifecycle of JSF. Not a full-fledged article yet, but a tutorial to find out by yourself.
Like before, I use NetBeans for explanation. Create a new web application an. Tag “Enable Context and Dependency Injection”. Choose JSF as framework with an extension of “*.xhtml” for URL pattern. This has been a very short explanation. For details and sreenshots refer to Part II of my JSF tutorial.
Complete the generated index.xhtlm:
<?xml version='1.0' encoding='UTF-8' ?>
<!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">
<f:metadata>
<f:phaseListener type="demo.LifeCycleListener"/>
</f:metadata>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Facelets
<h:form id="form">
<p>
<h:outputLabel for="name" value="Name:"/>
<h:inputText id="name" value="#{backingBean.name}"/>
</p>
<p>
<h:outputLabel for="age" value="Age:"/>
<h:inputText id="age" value="#{backingBean.age}"/>
</p>
<p>
<h:commandButton value="Ok"/>
</p>
</h:form>
</h:body>
</html>
Create a package and name it “demo”.
Create “JSF managed bean” and provide a name of “BackingBean”. Indeed, because you’ve enabled CDI, it will be a named bean. Or just create java class and extend it’s code to become a named bean.
package demo;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named
@RequestScoped
public class BackingBean {
private String _name;
private int _age;
public String getName() {
return _name;
}
public void setName(String name) {
this._name = name;
}
public int getAge() {
return _age;
}
public void setAge(int age) {
this._age = age;
}
}
Add a second bean with name of “LifeCycleListener”.
package demo;
import javax.enterprise.context.Dependent;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.inject.Named;
@Named
@Dependent
public class LifeCycleListener implements PhaseListener {
@Override
public void beforePhase(PhaseEvent event) {
System.out.println("Before phase: " + event.getPhaseId());
}
@Override
public void afterPhase(PhaseEvent event) {
System.out.println("After phase: " + event.getPhaseId());
}
@Override
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
}
Start the project and wath the output of the NetBeans output window, tab GlassFish Server. Provide a name and age and click onto [OK]. Use some letters instead of numeric for field age and click [Ok] again. Did you recognize the difference?
I’ll provide detailed explanation when I’ll get time again.
To web development content.