JAVA SERVER FACES (JSF)

JSF PROJECTS

JSF PROJECT 1

JSF Examples

JSF EXAMPLE

adplus-dvertising
PhaseListener Application
Previous Home Next

Code for phaseListener Example: For run this example following step is required.

Step 1: Welcome page of Example

<%--
Name= welcomeJSF.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%--
This file is an entry point for JavaServer Faces application.
--%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>r4r.co.in-phaseListener</title>
</head>
<body>
<h1><h:outputText value="Tag phaseListener Example"/></h1>
<h:form id="form">
<h:messages globalOnly="true" showDetail="true" 
errorStyle="color:red" infoStyle="color:green" />
<h:panelGrid columns="3" cellpadding="2" cellspacing="2">
<h:outputLabel value="Enter username"/>
<h:inputText id="user" value="#{listenerBean.user}" size="20" 
maxlength="10" required="true" requiredMessage="*" />
<h:message for="user" errorStyle="color:red" />
<h:outputLabel value="Enter Password" />
<h:inputText id="pass" value="#{listenerBean.pass}" size="20" 
maxlength="10" required="true" requiredMessage="*"/>
<h:message for="pass" errorStyle="color:red" />
<h:commandButton value=" Submit " action="#{listenerBean.checkLogin()}" />
</h:panelGrid>
<p>Username== user || password== pass</p>
</h:form>
</body>
</html>
</f:view>

Step 2: faces-config.xml file

<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ======= -->
<faces-config version="2.0"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 <lifecycle>
  <phase-listener>r4r.jsf.listenerClass</phase-listener> 
 </lifecycle>
<managed-bean>
  <managed-bean-name>listenerBean</managed-bean-name>
  <managed-bean-class>r4r.jsf.listenerBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/success.jsp</to-view-id>
  </navigation-case> 
  <navigation-case>
<from-outcome>login</from-outcome> 
<to-view-id>/welcomeJSF.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>
</faces-config>

step 3: ManagedBean class for provide logic in program.

/*
 * Save as a listenerBean.java
 */
package r4r.jsf;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
public class listenerBean {
private String user;
private String pass;
public String checkLogin() {
if (user.equals("user") && pass.equals("pass")) {
return "success";
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage
(FacesMessage.SEVERITY_ERROR, "Login Failure! ",
"Enter Username [" + user + "] && Password [" + pass + "] doesn't match"));
return null;
}
}
//------------ getter/setter
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}

step 4: Client Class for provide PhaseListener logic

/*
 * Save as a listenerClass.java
 */
package r4r.jsf;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
public class listenerClass implements PhaseListener {
private final String LOGIN_OUTCOME = "login";
@Override
public void afterPhase(PhaseEvent pe) {
System.out.println("After phase" + getPhaseId()); // Print to console
FacesContext context = pe.getFacesContext();
if (requestPage(context)) {
context.renderResponse();
context.getApplication().getNavigationHandler()
.handleNavigation(context, null, LOGIN_OUTCOME);
}
}
@Override
public void beforePhase(PhaseEvent pe) {
System.out.println("Before phase" + getPhaseId());
}
@Override
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
private boolean requestPage(FacesContext context) {
ExternalContext ec = context.getExternalContext();
String path = ec.getRequestPathInfo();
return (!"/welcomeJSF.jsp".equals(path) && !"/success.jsp".equals(path));
}
}

Output :

Previous Home Next