JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

PrimeFaces AccordionPanel – Dynamic Loading + Cache Example

April 12, 2014 by Amr Mohammed Leave a Comment

If you’ve seen the primefaces accordion panel component, you are most probably asking about what’s the main important features that provided for the developer for making the component more useful. One of the feature is lazy loading of the content inside each tab.

AccordionPanel supports lazy loading of tab content, when dynamic attribute is set true, only active tab contents will be rendered to the client side and clicking an inactive tab header will do an ajax request to load the tab contents. This would increased the performance while first time rendering the page.

  • Read : PrimeFaces Tutorials

This feature is useful to reduce bandwidth and speed up page loading time. By default activating a previously loaded dynamic tab does not initiate to load the contents again as tab is cached. To control this behavior use cache option.

AccordionPanel tag has the attributes dynamic and cache for achieving the lazy loading and caching techniques. This tutorial highlights the benefits of those two attributes.

AccordionPanel Dynamic Loading

The below snapshots show you the impact of using dynamic attribute and the difference between considering it or not. Using dynamic attribute will load the activated panel data only. The remaining panels won’t be loaded any more until they are activated by the user. But if you provide no dynamic attribute the view does load the whole panels.

Running Without Dynamic Attribute

Running Demo without using dynamic attribute

  • Without using dynamic attribute, the all Accordion Tabs have activated.

Running With Dynamic Attribute

Running Demo using dynamic attribute

  • Using the dynamic attribute, wil cause the only first AccordionPanel Tab has been activated.
  • Considering that the other tabs will be activated once the user has clicked on.

AccordionPanel Data Cache

As mentioned previously, activating a previous loaded dynamic Tab doesn’t initiate a request to load the contents again as tab is cached. Disabling of cache attribute will solve this issue.

Running in Dynamic & Cached 

Accordion Dynamic & Cached

Running in Dynamic & Non Cached 

Accordion Dynamic & Non Cached

1. Managed Bean

AccordionPanel.java

[code lang=”java”]
package net.javabeat.primefaces;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class AccordionPanel {
private String messageOne = "Hello JavaBeat One";
private String messageTwo = "Hello JavaBeat Two";
private String messageThree = "Hello JavaBeat Three";

public String getMessageOne() {
System.out.println("Message One Loaded");
return messageOne;
}
public void setMessageOne(String messageOne) {
this.messageOne = messageOne;
}
public String getMessageTwo() {
System.out.println("Message Two Loaded");
return messageTwo;
}
public void setMessageTwo(String messageTwo) {
this.messageTwo = messageTwo;
}
public String getMessageThree() {
System.out.println("Message Three Loaded");
return messageThree;
}
public void setMessageThree(String messageThree) {
this.messageThree = messageThree;
}
}
[/code]

2. index.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<f:view>
<h:form prependId="false">
<h1>JavaBeat Primefaces Example</h1>
<h2>Primefaces AccordionPanel</h2>
<br/>
<p:accordionPanel dynamic="true" cache="false">
<p:tab title="First Tab Title">
<h:outputText value="#{accordionPanel.messageOne}"></h:outputText>
</p:tab>
<p:tab title="Second Tab Title">
<h:outputText value="#{accordionPanel.messageTwo}"></h:outputText>
</p:tab>
<p:tab title="Third Tab Title">
<h:outputText value="#{accordionPanel.messageThree}"></h:outputText>
</p:tab>
</p:accordionPanel>
</h:form>
</f:view>
</html>
[/code]

3. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
[/code]

4. Pom.xml File

pom.xml

[code lang=”xml”]
<!–Pom entries–>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.4</version>
</dependency>

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.4</version>
</dependency>

<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<!–Pom entries–>
[/code]

[wpdm_file id=59]

Filed Under: JSF Tagged With: PrimeFaces

Primefaces AccordionPanel Example

April 11, 2014 by Amr Mohammed Leave a Comment

AccordionPanel is a container component that displays content in stacked format. That component has associated with a type of org.primefaces.component.AccordionPanel Java Class. This tutorial explains how to use the Primefaces AccordionPanel with simple example. If you have any questions, please write it in the comments section.

1. AccordionPanel Tag Info

Primefaces Accordion Panel General Info

2. AccordionPanel Attributes

Primefaces Accordion Attributes Part I

Primefaces Accordion Attributes Part II

3. Managed Bean

AccordionPanel.java

[code lang=”java”]
package net.javabeat.primefaces;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class AccordionPanel {
private String messageOne = "Hello JavaBeat One";
private String messageTwo = "Hello JavaBeat Two";
private String messageThree = "Hello JavaBeat Three";

public String getMessageOne() {
return messageOne;
}
public void setMessageOne(String messageOne) {
this.messageOne = messageOne;
}
public String getMessageTwo() {
return messageTwo;
}
public void setMessageTwo(String messageTwo) {
this.messageTwo = messageTwo;
}
public String getMessageThree() {
return messageThree;
}
public void setMessageThree(String messageThree) {
this.messageThree = messageThree;
}
}
[/code]

4. The View

index.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<f:view>
<h:form prependId="false">
<h1>JavaBeat Primefaces Example</h1>
<h2>Primefaces AccordionPanel</h2>
<br/>
<p:accordionPanel>
<p:tab title="First Tab Title">
<h:outputText value="#{accordionPanel.messageOne}"></h:outputText>
</p:tab>
<p:tab title="Second Tab Title">
<h:outputText value="#{accordionPanel.messageTwo}"></h:outputText>
</p:tab>
<p:tab title="Third Tab Title">
<h:outputText value="#{accordionPanel.messageThree}"></h:outputText>
</p:tab>
</p:accordionPanel>
</h:form>
</f:view>
</html>
[/code]

5. Required Dependencies

Add the following dependencies in your Maven’s pom.xml file.

pom.xml

[code lang=”xml”]
<!– Pom entries–>
<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.4</version>
</dependency>

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.4</version>
</dependency>

<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>

</dependencies>
<!– Pom entries–>
[/code]

7. The Deployment Descriptor

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" metadata-complete="true" version="2.5">
<error-page>
<error-code>404</error-code>
<location>/faces/error.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/error.xhtml</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/faces/error.xhtml</location>
</error-page>
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/code]

8. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
[/code]

9. Primefaces AccordionPanel Demo

Primefaces Demo

[wpdm_file id=58]

Filed Under: JSF Tagged With: PrimeFaces

JSF Custom Error Pages

April 10, 2014 by Amr Mohammed Leave a Comment

When you run an application in the development project stage and you encounter an error, you get an error message in an undesirable form. You probably don’t want your users to see that message in such that ugly way.

To substitute a better error page, use error-page tag in the web.xml file, in that you can specify either a Java Exception or an HTTP error code. So in case the type of thrown exception has matched that type mentioned in the web.xml exception-type  or the error code that generated by the server has matched error-code that mentioned in the web.xml, the JSF framework will handle it by forwarding the user into the desired view that you’ve defined for such those errors or exceptions.

1. The Deployment Descriptor

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">
<error-page>
<error-code>404</error-code>
<location>/faces/error.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/error.xhtml</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/faces/error.xhtml</location>
</error-page>
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/code]

  • The error codes that being handled in that defined web.xml are 500 and 400.
  • The exceptions that being handled in that defined web.xml is the root of exceptions that could be thrown java.lang.Exception.
  • All of the defined error codes and exceptions must be handled in a compelling error page called error.xhtml.

2. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
[/code]

  • No changes are made on the faces configuration to support the error page concept

3. The Error Page

error.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 – Error Handling</h2>
<br />
<h:outputText value="Error Code: #{errorHandler.statusCode}"></h:outputText>
<br />
<h:outputText value="Error Desscription: #{errorHandler.message}"></h:outputText>
<br />
<h:outputText value="Exception Type: #{errorHandler.exceptionType}"></h:outputText>
<br />
<h:outputText value="Exception Calss: #{errorHandler.exception}"></h:outputText>
<br />
<h:outputText value="Request URI : #{errorHandler.requestURI}"></h:outputText>
</html>
[/code]

4. ErrorHandler RequestScoped Bean

ErrorHandler.java

[code lang=”java”]
package net.javabeat.jsf.error;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class ErrorHandler {

public String getStatusCode(){
String val = String.valueOf((Integer)FacesContext.getCurrentInstance().getExternalContext().
getRequestMap().get("javax.servlet.error.status_code"));
return val;
}

public String getMessage(){
String val = (String)FacesContext.getCurrentInstance().getExternalContext().
getRequestMap().get("javax.servlet.error.message");
return val;
}

public String getExceptionType(){
String val = FacesContext.getCurrentInstance().getExternalContext().
getRequestMap().get("javax.servlet.error.exception_type").toString();
return val;
}

public String getException(){
String val = (String)((Exception)FacesContext.getCurrentInstance().getExternalContext().
getRequestMap().get("javax.servlet.error.exception")).toString();
return val;
}

public String getRequestURI(){
return (String)FacesContext.getCurrentInstance().getExternalContext().
getRequestMap().get("javax.servlet.error.request_uri");
}

public String getServletName(){
return (String)FacesContext.getCurrentInstance().getExternalContext().
getRequestMap().get("javax.servlet.error.servlet_name");
}

}
[/code]

  • The error handler bean is defined as RequestScoped
  • Several objects related to the error are placed in the Request Map and they are considered as Servlet Exception Attributes

5. Error Prone Page

index.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form prependId="false">
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 – Error Handling</h2>
<br/>
<h:outputText value="#{indexBean.message}"></h:outputText>

<h:commandButton value="Throws Exception" action="#{indexBean.navigate}"/>
</h:form>
</f:view>
</html>
[/code]

6. Error Prone Managed Bean

IndexBean.java

[code lang=”java”]
package net.javabeat.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class IndexBean {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String navigate(){
// Assume an exception has been thrown by some business logic
System.out.println(10/0);
return "anonymousView";
}
}
[/code]

7. JSF Error Handling Demo

The below snapshots show you how could a thrown exception being handled in a compelling view.

JSF 2 Error Page View Example 1

JSF 2 Error Page View Example 2

8. JSF Without Custom Error Page

The below snapshot shows you the ugly page that might be displayed for the users while they are navigating your site.

JSF 2 Error Page Example 3

[wpdm_file id=57]

Filed Under: JSF Tagged With: JSF 2

JSF + Scala Integration

April 10, 2014 by Amr Mohammed Leave a Comment

Scala is a popular programming language for the Java Virtual Machine (JVM). Like Java, it is strongly typed and object-oriented, but it also supports functional programming. Many developers are attracted to Scala because it requires less boilerplate for common constructs, such as properties. Scala does support calling a code in the Java Library from it, so it’s possible to integrate the JavaServer Faces (JSF Framework) with Scala. This tutorial you’re going to install and configure the created jsf maven project to support using of Scala.

Tools Required For JSF And Scala Integration

  1. Eclipse IDE 4.3 (Kepler)
  2. Tomcat Servlet Runner (7.0.35)
  3. Scala 2.10.4
  4. JSF 2.0

1. Install Scala IDE for Eclipse

We are going to install the Scala in the installed Eclipse IDE. By using the scala download link you can install the Scala IDE for Eclipse for enabling the Scala tools, compiler and perspective. The steps required for installing the Scala are as following:

  • Open Eclipse IDE
  • From Help menu choose Install New Software.
  • Inside Work with type then scala download link mentioned above.
  • From the fetched list, select Scala IDE for Eclipse.

Install Scala IDE for Eclipse

2. Add Scala Nature

If you’ve tried to create any type of Scala files (Scala class, objects, etc ..) you’re surely about facing a complexity for doing that for being the created JSF maven project that imported into eclipse doesn’t support Scala nature.

For adding the Scala nature you have to follow the below steps:

  • Right click on the created JSF project that imported into your eclipse IDE.
  • From Configure choose the option Add Scala Nature.
  • From Project menu select clean.
  • For more familiarity using of scala, you have a new scala perspective. From Window menu, select Open perspective and choose Scala. If you cannot show such that perspective choose Other and select Scala again.

By doing all of the above steps, you have the ability to create scala assets easily.

JSF 2 Scale Adding Scale Nature

JSF 2 Scala Perspective

3. Scala Library

Make sure that once you’ve added a scala nature to your project the scala library will be added for it. Just look at your project structure and beside libraries such Maven Dependencies and JRE System Library you should see Scala Library [Version].

If you’ve not seen it, add it by the following steps:

  • Right click on your project.
  • From Scala Menu, select Add Scala Library To Build Path. (Someone may ask, if you are using a maven why not add those libraries into maven dependencies).
  • For those interesting in the maven and only maven add the scala dependency to your pom.xml.

Scala Maven Dependency

[code lang=”xml”]
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.4</version>
</dependency>
[/code]

Either you are using the library added by the eclipse or use the library added by the maven, you are getting the right way and your application should running smoothly.

JSF 2 Scala Adding Library

JSF 2 Scala Libraries

4. Create Scala Class (Managed Bean)

To create a scala class that would be considering later as a managed bean, you have to follow the below steps:

  • Right click on the already created package and select New.
  • From the sub menu of new select Scala Class.
  • Name your newly created scala class as IndexBean.
  • Staring to write a scala class.

Your class should look like the below one:
IndexBean.scala

[code lang=”java”]
package net.javabeat.jsf

import javax.faces.bean.ManagedBean
import scala.beans.BeanProperty

@ManagedBean
class IndexBean {
@BeanProperty
var name = "JavaBeat !";
@BeanProperty
var message = "Hello";

def navigate(): String ={
return "anonymousView";
}
}
[/code]

  • Note using of javax.faces.bean.ManagedBean annotation to annotate a scala class
  • Scala provides you another type of annotations called @BeanProperty that annotate the scala class properties.
  • IndexBean scala class defines two properties and one method named navigate that doesn’t consider any parameters and it will return a string.

JSF 2 Adding Scala Class

5. The View

index.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form prependId="false">
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 &amp;amp; Scala</h2>
<br/>
<h:outputText value="#{indexBean.message}"></h:outputText>
<h:outputText value="#{indexBean.name}"></h:outputText>
<br/>
<h:commandButton value="Navigate Using Scala" action="#{indexBean.navigate}"/>
</h:form>
</f:view>
</html>
[/code]

anonymousView.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:form prependId="false">
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 &amp;amp; Scala</h2>
<br/>
<h3>Anonymous View</h3>
</h:form>
</html>
[/code]

6. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
[/code]

7. The Deployment Descriptor

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/code]

8. JSF + Scala Integration Demo

The below snapshots show you a running demonstration for a jsf application does use a scala programming language.

JSF 2 Scala Example 1

JSF 2 Scala Example 2

  • You’ve achieved the rendering of Scala class (Managed Bean) properties.
  • You’ve navigated into another view using the Scala class (Managed Bean) action method.

[wpdm_file id=56]

Filed Under: JSF Tagged With: JSF 2, JSF Integration, scala

JSF + Groovy Integration

April 10, 2014 by Amr Mohammed Leave a Comment

Groovy is another popular programming language for the Java Virtual Machine. Groovy is dynamically typed, and groovy code can be more concise than the equivalent Java because you can omit types of variables and parameters.

Almost any Java code is legal groovy, that’s make it simple and easier for getting started using it. Ultimately, using the Groovy within JSF applications isn’t more complex for one reason the using of groovy compiler to compile your groovy code does generate a .class file. In JSF, it doesn’t know, or care that you’re using groovy to generate them.

The JSF reference implementation  supports hot deployment of your Groovy code. When you change the groovy source files, the JSF implementation automatically recompile it and deploys the class files to the web application.

This tutorial you are going to integrate the Groovy with the JavaServer Faces using support of Eclipse IDE. This tutorial you will use the already created jsf maven project.

Tools Required For JSF + Groovy Integration

  • Eclipse IDE 4.3 (Kepler)
  • Tomcat Servlet Runner (7.0.35)
  • Groovy 2.0.x
  • JSF 2.0

1. Install Groovy Plugin for Eclipse

By using the Groovy download link you can choose the proper plugin for your Eclipse, but in our case you a have consider the link that provided for Kepler version. The steps required for installing the Groovy is as following:

  • Open Eclipse IDE
  • From Help menu choose Install New Software.
  • Inside Work with type the Groovy download link mentioned above.
  • From the fetched list, select Groovy-Eclipse.

Groovy Eclipse IDE - Download Groovy Plugin

2. Add Groovy Nature

If you’ve tried to create any type of Groovy files (groovy class, project, etc ..) you’re surely about getting complexity, cause your imported maven project doesn’t support Groovy naturally.

For adding Groovy you have to follow the below steps:

  • Right click on the created JSF project that imported into your eclipse IDE.
  • From Configure choose the option Convert To Groovy Project.
  • From Project menu select clean.
  • There is no specific perspective for Groovy, cause it’s considered as Java Project.

JSF 2 Groovy Add Nature

3. Groovy Library

Make sure that once you’ve added a groovy nature to your project,  the groovy library in it’s turn being added for it. Just look at your project structure and beside libraries such Maven Dependencies and JRE System Library you should see Groovy Libraries & Groovy DSL Support.

If you’ve not seen it, add it by the following steps:

  • Right click on your project.
  • From Grrovy Menu, select Add Groovy Library To Build Path.

But for those developers that focusing on using of maven you can consider the following dependency as mandatory.

Groovy Maven Dependency

[code lang=”xml”]
&lt;dependency&gt;
&lt;groupId&gt;org.codehaus.groovy&lt;/groupId&gt;
&lt;artifactId&gt;groovy-all&lt;/artifactId&gt;
&lt;version&gt;2.2.2&lt;/version&gt;
&lt;/dependency&gt;
[/code]

Either you are using the library added by the eclipse or use the library added by the maven, you are getting the right way and your application should running smoothly.

JSF 2 Groovy Add Library

JSF 2 Groovy Added Libraries

4. Groovy Class (Managed Bean)

To create a Groovy class that would be considering later as a managed bean, you have to follow the below steps:

  • Right click on the already created package and select New.
  • From the sub menu of new select Groovy Class.
  • Name your newly created Groovy class as IndexBean.
  • Staring to write a groovy class.

Your class should look like the below one:

IndexBean.groovy

[code lang=”java”]
package net.javabeat.jsf

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
class IndexBean implements GroovyObject{
private message = "Hello Mr. "; // Within groovy, you can omit the types
private userInput; // Within groovy, you can omit the types

public void setMessage(String message){
this.message = message;
}

public String getMessage(){
return this.message;
}

public void setUserInput(String userInput){
this.userInput = userInput;
}

public String getUserInput(){
return this.userInput;
}

public String navigate(){
return "anonymousView";
}
}
[/code]

  • Note using of properties without typos.
  • Note using of jsf2 @ManagedBean annotation
  • Note using of jsf2 @SessionScoped annotation
  • IndexBean groovy class defines two properties and one method named navigate that doesn’t consider any parameters and it will return a string.

JSF Groovy Adding Groovy Class

5. The View

index.xhtml

[code lang=”xml”]
&lt;html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"&gt;
&lt;f:view&gt;
&lt;h:form prependId="false"&gt;
&lt;h1&gt;JavaBeat JSF 2.2 Examples&lt;/h1&gt;
&lt;h2&gt;JSF2 &amp;amp; Groovy&lt;/h2&gt;
&lt;br/&gt;
&lt;h:outputText value="#{indexBean.message}"&gt;&lt;/h:outputText&gt;
&lt;h:inputText value="#{indexBean.userInput}"&gt;&lt;/h:inputText&gt;
&lt;br/&gt;
&lt;h:commandButton value="Navigate Using Groovy Action" action="#{indexBean.navigate}"/&gt;
&lt;/h:form&gt;
&lt;/f:view&gt;
&lt;/html&gt;
[/code]

anonymousView.xhtml

[code lang=”xml”]
&lt;html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"&gt;
&lt;h:form prependId="false"&gt;
&lt;h1&gt;JavaBeat JSF 2.2 Examples&lt;/h1&gt;
&lt;h2&gt;JSF2 &amp;amp; Groovy&lt;/h2&gt;
&lt;h3&gt;Anonymous View&lt;/h3&gt;
&lt;h:outputText value="#{indexBean.message}"&gt;&lt;/h:outputText&gt;
#{‘ ‘}
&lt;h:outputText value="#{indexBean.userInput}"&gt;&lt;/h:outputText&gt;
&lt;/h:form&gt;
&lt;/html&gt;
[/code]

6. Faces Configuration File

faces-config.xml

[code lang=”xml”]
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2"&gt;
&lt;application&gt;
&lt;resource-bundle&gt;
&lt;base-name&gt;net.javabeat.jsf.application&lt;/base-name&gt;
&lt;var&gt;msg&lt;/var&gt;
&lt;/resource-bundle&gt;
&lt;/application&gt;
&lt;/faces-config&gt;
[/code]

7. The Deployment Descriptor

web.xml

[code lang=”xml”]
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true"&gt;
&lt;context-param&gt;
&lt;description&gt;State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
&lt;/description&gt;
&lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
&lt;param-value&gt;server&lt;/param-value&gt;
&lt;/context-param&gt;
&lt;context-param&gt;
&lt;param-name&gt;javax.faces.application.CONFIG_FILES&lt;/param-name&gt;
&lt;param-value&gt;/WEB-INF/faces-config.xml&lt;/param-value&gt;
&lt;/context-param&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;listener&gt;
&lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt;
&lt;/listener&gt;
&lt;/web-app&gt;
[/code]

8. JSF & Groovy Integration Demo

The below snapshots show you a running demonstration for a jsf application does use a Groovy programming language.

JSF 2 Groovy Example 1

JSF 2 Groovy Example 2

9. java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

If you’ve followed the previous steps carefully, and your application doesn’t functional properly. Make sure that your Tomcat 7 has loaded the Groovy library in it’s classpath even your application has been compiled successfully. Also, you might be familiar with an error message that shown on the Eclipse console says

[code lang=”xml”]
SEVERE: Unable to load annotated class: net.javabeat.jsf.IndexBean, reason: java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
[/code]

Even that your application has been running, but your groovy managed bean doesn’t initialized and try to navigate for seeing a big exception telling you that your managed bean is resolved to null.

Don’t worry, all things that you have to do is to assemble the groovy library into your server by following the below steps:

  • Right click on the project.
  • Choose properties, and point on Deployment Assembly.
  • Click Add button within the Web Deployment Assembly dialog.
  • Choose the Java Build Path Entries and click next.
  • Choose those libraries that mentioned for Groovy. and click finish.
  • Enjoy running your application.

JSF 2 Groovy Example 4

JSF 2 Groovy Example 5

JSF 2 Groovy Example 6

JSF 2 Groovy Example 7

[wpdm_file id=55]

Filed Under: JSF Tagged With: Eclipse, Groovy, JSF 2, JSF Integration

JSF SystemEventListener – UIComponent Events Example

April 9, 2014 by Amr Mohammed Leave a Comment

You’ve already examined the events that could be fired from both of Application and UIViewRoot. But those aren’t only the events that might be handled. The UIComponent – UIComponent is the base class for all user interface components in JavaServer Faces. The set of UIComponent instances associated with a particular request and response are organized into a component tree under a UIViewRoot that represents the entire content of the request or response – is also have a set of predefined system events could be listened.

The following events are fired by the UIComponent:

  • PostAddToViewEvent: After a component has been added to the view root.
  • PreRemoveFromViewEvent: Before a component is about to be removed.
  • PostRestoreStateEvent: After the state of a component has been restored.
  • PreValidateEvent: Before a component is validated.
  • PostValidateEvent: After a component is validated.
  • PreRenderComponentEvent: Before the component is about to be rendered.

At this tutorial, you are going to look at all mentioned events.

1. Managed Bean

IndexBean.java

[code lang=”java”]
package net.javabeat.jsf;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.PostAddToViewEvent;
import javax.faces.event.PostRestoreStateEvent;
import javax.faces.event.PostValidateEvent;
import javax.faces.event.PreRemoveFromViewEvent;
import javax.faces.event.PreRenderComponentEvent;
import javax.faces.event.PreValidateEvent;

@ManagedBean
@ViewScoped
public class IndexBean {
private UIComponentListener listener = new UIComponentListener();
private UIInput messageInput;

public IndexBean(){
// For PreRemove
FacesContext.getCurrentInstance().getApplication().
subscribeToEvent(PreRemoveFromViewEvent.class, listener);
// For PostRestore
FacesContext.getCurrentInstance().getApplication().
subscribeToEvent(PostRestoreStateEvent.class, listener);
}

private String message = "JavaBeat Message";

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public UIComponentListener getListener() {
return listener;
}

public void setListener(UIComponentListener listener) {
this.listener = listener;
}

public UIInput getMessageInput() {
return messageInput;
}

public void setMessageInput(UIInput messageInput) {
this.messageInput = messageInput;
}

public void postAddToViewHandler(PostAddToViewEvent e){
System.out.println("Event :: "+e.toString()+" :: Being Processed ::");
}

public void preRenderComponent(PreRenderComponentEvent e){
System.out.println("Event :: "+e.toString()+" :: Being Processed ::");
}

public void preValidate(PreValidateEvent e){
System.out.println("Event :: "+e.toString()+" :: Being Processed ::");
}

public void postValidate(PostValidateEvent e){
System.out.println("Event :: "+e.toString()+" :: Being Processed ::");
}

public String delete(){
List<UIComponent> components =
FacesContext.getCurrentInstance().getViewRoot().getChildren();
for(UIComponent component : components){
if(component.getId().equals(messageInput.getId())){
FacesContext.getCurrentInstance().getViewRoot().getChildren().remove(component);
}
}
return "";
}
}
[/code]

2. The View

index.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form prependId="false">
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 System Events – UIComponent Events Example</h2>
<br/>

<h:panelGrid columns="3">
<h:outputText value="Enter Your Message:">
<f:event listener="#{indexBean.preRenderComponent}" type="preRenderComponent"></f:event>
</h:outputText>
<h:inputText value="#{indexBean.message}"
binding="#{indexBean.messageInput}">
<f:event listener="#{indexBean.postAddToViewHandler}" type="postAddToView"></f:event>
<f:event listener="#{indexBean.preValidate}" type="preValidate"></f:event>
<f:event listener="#{indexBean.postValidate}" type="postValidate"></f:event>
</h:inputText>
<h:commandButton value="Fire Remove From View Event" action="#{indexBean.delete}"></h:commandButton>
</h:panelGrid>
</h:form>
</f:view>
</html>
[/code]

3. SystemEventListener

UIComponentListener

[code lang=”java”]
package net.javabeat.jsf;

import javax.faces.component.UIComponent;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

public class UIComponentListener implements SystemEventListener {

@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
System.out.println("Event :: "+event.toString()+" :: Being Handled");
}

@Override
public boolean isListenerForSource(Object source) {
if(source instanceof UIComponent){
return true;
}
return false;
}
}
[/code]

4. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
[/code]

5. The Deployment Descriptor

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/code]

6. JSF SystemEventListener – UIComponent Events Demo

The below snapshot shows you the events propagated by the different type of UIComponent and the different ways that used for handle the events.

JSF SystemEventListener - UIComponent Events Demo

[wpdm_file id=54]

Filed Under: JSF Tagged With: JSF 2

JSF 2 SystemEventListener – UIViewRoot Events Example

April 8, 2014 by Amr Mohammed Leave a Comment

As mentioned previously in the introduction of the JSF2 System Events – Application Events, there are many kinds of System events that could be listening for. UIViewRoot component – is the UIComponent that represents the root of the UIComponent tree – also does considered one of the component that has fired a system event.

UIViewRoot has fired three main types of events:

  • PreRenderComponentEvent: This event has fired before the view root is about to be rendered.
  • PostConstructiveMapEvent: This event has fired after the root component has constructed the view scope map
  • PreDestroyViewMapEvent: This event has fired when the view map is cleaned.

Let’s see how could such those events listened.

1. ViewScoped Managed Bean

IndexBean.java

[code lang=”java”]
package net.javabeat.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.PostConstructViewMapEvent;
import javax.faces.event.PreDestroyViewMapEvent;

@ManagedBean
@ViewScoped
public class IndexBean {
private UIViewRootListener listener = new UIViewRootListener();

public IndexBean(){
// Subscribe listener for PostConstructiveViewMapEvent
FacesContext.getCurrentInstance().getApplication().
subscribeToEvent(PostConstructViewMapEvent.class, listener);

// Subscribe listener for PreDestrotyViewMapEvent
FacesContext.getCurrentInstance().getApplication().
subscribeToEvent(PreDestroyViewMapEvent.class, listener);
}

private String message = "JavaBeat Message";

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public void preRenderView(ComponentSystemEvent e){
System.out.println("PreRenderView Event Being Processed");
}

public String emptyViewScope(){
return "index";
}
}
[/code]

2. UIViewRoot Event Listener

UIViewRootListener.java

[code lang=”java”]
package net.javabeat.jsf;

import javax.faces.component.UIViewRoot;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

public class UIViewRootListener implements SystemEventListener{

public void processEvent(SystemEvent event) throws AbortProcessingException {
System.out.println("System Event : "+event.toString() + " :: Being Processed");
}

public boolean isListenerForSource(Object source) {
if(source instanceof UIViewRoot){
return true;
}
return false;
}
}
[/code]

3. The Views

index.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 System Events – Application Source Example</h2>
<br/>
<h2>This is a JSF View</h2>
<h:commandButton value="Go To Anonymous View" action="anonymousView"></h:commandButton>
</h:form>
</f:view>
</html>
[/code]

anonymousView.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<f:event listener="#{indexBean.preRenderView}" type="preRenderView"></f:event>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 System Events – Application Source Example</h2>
<br/>
<h3>View Map Constructed</h3>
<br/>
<h:outputText value="#{indexBean.message}"/>
<br/>
<h:commandButton value="Go To Index" action="index"></h:commandButton>
</h:form>
</f:view>
</html>
[/code]

4. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
[/code]

5. The Deployment Descriptor

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/code]

6. JSF 2 UIViewRoot Event Demo

The below snapshot shows you the normal behavior for listening upon UIViewRoot component events.

JSF 2 UIViewRoot Events Listening Example

[wpdm_file id=53]

Filed Under: JSF Tagged With: JSF 2

JSF 2 SystemEventListener Example

April 8, 2014 by Amr Mohammed Leave a Comment

The System Events are normally specified in the JavaServer Faces Specification 2.2 under “System Events” Subject. This concept expands on the idea of lifecycle PhaseEvents. With the PhaseEvents, it is possible to have an application scoped PhaseListeners that are given the opportunity to act on the system before and after each phase in the lifecycle. System events provide a much more fine-grained insight into the system, allowing application or component scoped listeners to be notified of a verity of kinds of events.

There are several ways to declare interest in a particular kind of event.

  • Call Application.subscribeToEvent () to add an application scoped listener.
  • Call UIComponent.subscribeToEvent () to add a component scoped listener.
  • Use <f:event> tag to declare a component scoped listener.
  • Use @ListenerFor or @ListenersFor annotation. The scope of the listener is determined by the code that processes the annotation. (This mechanism can be useful for component developers).
  • Use <system-event-listener> element in an application configuration resource to add an application scoped listener.

At this tutorial, you are going to examine the using of <system-event-listener> in an application configuration to add application scoped listener.

Also Read:

  • JSF 2 Tutorials
  • JSF Tutorials
  • Introduction to JSF

1. Managed Bean

IndexBean.java

[code lang=”java”]
package net.javabeat.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class IndexBean {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
[/code]

2. The View

index.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 System Events – Application Source Example</h2>
<br/>
<h2>This is a JSF View</h2>
</h:form>
</f:view>
</html>
[/code]

3. SystemEventListener

SystemEventsListener.java

[code lang=”java”]
package net.javabeat.jsf.listener;

import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

public class SystemEventsListener implements SystemEventListener{

@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
// The handling of event
System.out.println("System Event Being Processed :: "+event.toString());

}

@Override
public boolean isListenerForSource(Object source) {
if(source instanceof Application){
// This listener for this kind of source
return true;
}
// This listener isn’t for this kind of source
return false;
}
}
[/code]

4. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
<system-event-listener>
<system-event-listener-class>net.javabeat.jsf.listener.SystemEventsListener</system-event-listener-class>
<system-event-class>javax.faces.event.PostConstructApplicationEvent</system-event-class>
</system-event-listener>
<system-event-listener>
<system-event-listener-class>net.javabeat.jsf.listener.SystemEventsListener</system-event-listener-class>
<system-event-class>javax.faces.event.PreDestroyApplicationEvent</system-event-class>
</system-event-listener>
</application>
</faces-config>
[/code]

5. JSF 2 SystemEventListener Demo

The below snapshots show you the way that used for listening the SystemEventListener.

JSF 2 System Event Example 1 SystemEventListener

JSF 2 System Event Example 2

  • By removing the application from the server, you are fired the PreDestroyApplicationEvent.

JSF 2 System Event Example 3

[wpdm_file id=52]

Filed Under: JSF Tagged With: JSF 2

JSF 2 DataTable Delete Row Example

April 8, 2014 by Amr Mohammed Leave a Comment

In JSF 2.2 the manipulation of Data Table has been never easier and specifically when it comes to finding out which row the user selected for removal. In JSF 2.2 the removing happened without using of passed parameters or binding the DataTable component into its corresponding Java Object – Like HTMLDataTable – for being manipulated from there. Simply what you have to do is to identify your Plain Old Java Object (POJO) by overriding the equals and pass the object being removed into the remove method. This tutorial highlights the simple example for how to delete a row in datatable.

1. Student POJO

Student.java

[code lang=”java”]
package net.javabeat.jsf.data;

public class Student {
private String name;
private String major;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}

public boolean equals(Object obj){
// The identification here is through of using the name of the student
if(obj instanceof Student){
if(this.name.equals(((Student)obj).getName())){
return true;
}
}
return false;
}
}
[/code]

2. Managed Bean

RegisterBean.java

[code lang=”java”]
package net.javabeat.jsf;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.javabeat.jsf.data.Student;

@ManagedBean
@SessionScoped
public class RegisterBean {
private Student student = new Student();
private List<Student> students = new ArrayList<Student>();
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}

public String addStudent(){
this.students.add(student);
this.student = new Student();
return "";
}

public String delete(Student std){
this.students.remove(std);
return "";
}
}
[/code]

3. The View

index.xhtml

[code lang=”xml”]
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 DataTable – Deleting Rows Example</h2>
<h:panelGrid columns="2" border="1">
<h:outputText value="Enter Student Name:"/>
<h:inputText value="#{registerBean.student.name}"/>
<h:outputText value="Enter Student Major:"/>
<h:inputText value="#{registerBean.student.major}"/>
</h:panelGrid>
<br/>
<h:commandButton value="Add Student" action="#{registerBean.addStudent}"/>
<br/>
<br/>
<h:dataTable value="#{registerBean.students}" var="student" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Student Name"/>
</f:facet>
<h:outputText value="#{student.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Student Major"/>
</f:facet>
<h:outputText value="#{student.major}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Delete Student"/>
</f:facet>
<h:commandLink value="Delete" action="#{registerBean.delete(student)}"/>
</h:column>
</h:dataTable>
</h:form>
</f:view>
</html>
[/code]

3. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
[/code]

4. The Deployment Descriptor

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/code]

5. JSF DataTable Delete Row Demo

The below snapshot, shows you the using of the newly feature added for removing the rows from a data table using the passing pojo java object directly into the method that responsible of removal operation.

JSF 2 Deleting Rows Example 2

  • The admins can add whatever students they want.

JSF 2 Deleting Rows Example 2

  • The admins can remove whatever students they want.
  • The deletion process has assumed that the uniqueness is covered through the name of the student. However, you have ability to consider whatever kind of identification. But consider the needs to override the equals method in the Student.

[wpdm_file id=51]

Filed Under: JSF Tagged With: JSF 2

JSF 2 @FlowScoped Example

April 8, 2014 by Amr Mohammed Leave a Comment

With JSF 2.2, the JavaServer Faces technology allows you to create a set of pages with the new scope FlowScoped, that scope is greater than request scope and less than session scope. The FlowScoped scope depends on the concept of procedure where is declared a well defined entry point and a return value. The entry point should be used for starting the flow whereas the return is used for finishing the flow. In between the managed beans are defined for that scope, so once the flow has entered by its entry point the all managed beans that are defined as FlowScoped for that flow have created and once the flow has finished the FlowScoped managed beans are destroyed.

In other words, the flow has a scope, allowing information to be available only during the invocation of the flow. Such information is not available outside the scope of the flow and doesn’t consume any resources once the flow returns.

You can configure the flow using three types of configuration:

  • Using the faces-config.xml as introduced in this tutorial.
  • Using the file named as same name of the flow but it ends with .xml.
  • Using created class and annotated using @FlowDefinition.

The using of FlowScoped isn’t possible without using of CDI (Context Dependency Injection), so in order to define a FlowScoped managed beans, you have to include any of the CDI implementation. For that purpose we’ve used a JBoss Weld Implementation.

Also Read:

  • JSF 2 Tutorials
  • JSF Tutorials
  • Introduction to JSF

1. Managed Bean

RegisterBean.java

[code lang=”java”]
package net.javabeat.jsf;

import java.util.ArrayList;
import java.util.List;

import javax.faces.flow.FlowScoped;
import javax.inject.Named;

import net.javabeat.jsf.data.Student;

@Named(value="register")
@FlowScoped(value="register")
public class RegisterBean {
private int index = 0;
private String name;
private String age;
private String address;

private String major;
private String graduateYear;

private List<Student> students = new ArrayList<Student>();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getMajor() {
return major;
}

public void setMajor(String major) {
this.major = major;
}

public String getGraduateYear() {
return graduateYear;
}

public void setGraduateYear(String graduateYear) {
this.graduateYear = graduateYear;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}

public List<Student> getStudents() {
return students;
}

public void setStudents(List<Student> students) {
this.students = students;
}

public String fillBasicInfo (){
index++;
return "registera";
}

public String fillAcademicInfo(){
index++;
return "registerb";
}

public String backIntoBasicInfo(){
index–;
return "registera";
}

public String save(){
Student student = new Student();
student.setName(this.name);
student.setAddress(this.address);
student.setAge(this.age);
student.setMajor(this.major);
student.setGraduateYear(this.graduateYear);
this.students.add(student);
this.name = "";
this.major = "";
this.graduateYear = "";
this.address = "";
this.age = "";
this.index = 0;
return "";
}
}
[/code]

Student.java

[code lang=”java”]
package net.javabeat.jsf.data;

public class Student {
private String name;
private String age;
private String address;

private String major;
private String graduateYear;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getGraduateYear() {
return graduateYear;
}
public void setGraduateYear(String graduateYear) {
this.graduateYear = graduateYear;
}
}
[/code]

2. Faces Configuration File

faces-config.xhtml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
<flow-definition id="register">
<flow-return id="endFlow">
<from-outcome>/index</from-outcome>
</flow-return>
</flow-definition>
</faces-config>
[/code]

  • Based on the faces-config.xml you’ve a Flow named register
  • Based on the name of the flow, the entry page must be named as the name of the flow (Start Node)
  • The consequences pages of the flow can have any name
  • The return page must be located outside of the flow directory (See index.xhtml). The return is used in order to end the flow, the provided flow-return determines the token that cause the flow to be ended and the view to be displayed. 
  • The flow doesn’t call any another flow
  • The flow doesn’t pass parameters to another flow

3. The Views

index.xhtml

[code lang=”xml”]
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 FlowScoped Example</h2>
<h:commandButton value="Start Flow" action="register"></h:commandButton>
</h:form>
</f:view>
</html>
[/code]

register.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 FlowScoped Example</h2>
<br/>
<br/>
<h:outputText value="Page # #{register.index}" style="font-size:15px;"/>
<br/>
<br/>
<h:outputText value="JavaBeat Training Center"/>
<br/>
<br/>
<h:outputText value="Start"/>
#{‘ ‘}
<h:commandButton value="Enter Registration Info" action="#{register.fillBasicInfo}"/>
</h:form>
</f:view>
</html>
[/code]

registera.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 FlowScoped Example</h2>
<br/>
<h:outputText value="Page # #{register.index}" style="font-size:15px;"/>
<br/>
<h:outputText value="Basic Information:"/>
<br/>
<h:outputText value="Enter Name:"/>#{‘ ‘}<h:inputText value="#{register.name}"></h:inputText>
<br/>
<h:outputText value="Enter Address:"/>#{‘ ‘}<h:inputText value="#{register.address}"></h:inputText>
<br/>
<h:outputText value="Enter Age:"/>#{‘ ‘}<h:inputText value="#{register.age}"></h:inputText>
<br/>
<h:commandButton value="Enter Academic Info" action="registerb"></h:commandButton>
</h:form>
</f:view>
</html>
[/code]

registerb.xhtml

[code lang=”xml”]
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 FlowScoped Example</h2>
<br/>
<h:outputText value="Page # #{register.index}" style="font-size:15px;"/>
<br/>
<h:outputText value="Academic Information:"/>
<br/>
<h:outputText value="Enter Major:"/>#{‘ ‘}<h:inputText value="#{register.major}"></h:inputText>
<br/>
<h:outputText value="Enter Graduate Year:"/>#{‘ ‘}<h:inputText value="#{register.graduateYear}"></h:inputText>
<br/>
<h:commandButton value="Back" action="registera"></h:commandButton>
<h:commandButton value="Save" action="#{register.save}"></h:commandButton>
<h:commandButton value="New Student" action="register"></h:commandButton>
<h:commandButton value="End Flow" action="/index"></h:commandButton>
<br/>
<h:dataTable value="#{register.students}" var="student" border="1">
<f:facet name="header">
<h:outputText value="Registerd Students"></h:outputText>
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{student.name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Major"/>
</f:facet>
<h:outputText value="#{student.major}"></h:outputText>
</h:column>
</h:dataTable>
</h:form>
</f:view>
</html>
[/code]

4. The Deployment Descriptor

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<resource-env-ref>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>
javax.enterprise.inject.spi.BeanManager
</resource-env-ref-type>
</resource-env-ref>
</web-app>
[/code]

5. Weld Dependency

[code lang=”xml”]
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>1.1.18.Final</version>
</dependency>
[/code]

6. webapp/META-INF/context.xml

[code lang=”xml”]
<Context>
<Resource name="BeanManager"
auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"/>
</Context>
[/code]

7. webapp/WEB-INF/beans.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>
[/code]

8. Directory Structure

JSF 2 FlowScope Example 1

9. JSF 2 FlowScoped Demo

  • The below snapshots show you the using of FlowScoped for implementing a registration module. In that module the admin entry of a flow in order to register a student, but meanwhile the admin has a chance to cancel the flow or save the student being registered.
  • Save the student means the admin still in the flow, and it can also register another one, but once the admin has cancelled the flow, the all registered students are removed together with their managed beans that are held them.

JSF 2 FlowScope Example 2

  • The user has entered the flow by navigating into register.xhtml that represents the entry point for the defined register flow.
  • All of those managed beans associated with the flow are created.

JSF 2 FlowScope Example 3

  • The user has navigated into the first node of the flow.
  • By entering the academic information, the user has navigated into second node.

JSF 2 FlowScope Example 3

  • If the user has activated the Back, the previous node of the flow should be displayed.
  • If the user has activated the Save, the student will be saved and the current node of the flow will remain.
  • If the user has activated the End Flow, the flow will be ended and the beans associated will be destroyed. Index view has displayed.
  • If the user has activated the New Student, the same flow is continued and it will be remaining for holding the data. Register view has displayed.

JSF 2 FlowScope Example 4

  • Result of Save activation.

JSF 2 FlowScope Example 5

  • The return view that should be displayed, once the Flow has returned (ended).
  • By returning from the Flow, all of these associated managed beans are destroyed.
  • If the user has been entering again into the flow, the associated managed beans are initialized again for serving the user request.

[wpdm_file id=50]

Filed Under: JSF Tagged With: JSF 2

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • …
  • 17
  • Next Page »

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved