Thursday 15 November 2012

How to get Value from FacesContext in JSF?

If your managed bean is testbean of class TestBean then U can get the bean from the FacesContex based on scope as follows

For session scope---

TestBean testbeanObject=(TestBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("testbean");

For Request scope---

TestBean testbeanObject=(TestBean)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("testbean");

 For Application scope---

TestBean testbeanObject=(TestBean)FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("testbean");



Saturday 10 November 2012

JSF consists of 
Java classes
• “Faces” components: server-side equivalents of HTML
components (text,buttons,check boxes, etc.)
• FacesServlet (web.xml)
• Helper classes (converters, validations, etc) 
Tag libraries
• Sun Microsystems tag libraries
• Used in JSPs and Facelets 
Configuration file (faces-config.xml)
• JSF project configuration: navigation between pages,JavaBeans used by JSF



JSF Architecture

JSF is a web application framework that implements the MVC design pattern, and a clean separation of concerns:
Model: Contains UI data and handles database interactions 

View: Defines the user interface with a hierarchy of components using a declarative markup language(eg xhtml)

Controller: Handles user interactions and navigation between views



Managed Bean Scopes
• The JSF managed bean facility provides three scopes in which managed-
beans may exist:
request:
• Very short lifespan –created when the request begins, and
scheduled for garbage collection when the request completes
• For efficiency, try to place managed beans in request scope
session:
• The HttpSession can be used to store application state, such as
the contents of a shopping cart
• The lifespan of an HttpSession is determined by a timeout or by
invalidation (when a user logs out)
application:
• Lifespan continues as long as the web application is deployed
• Useful for sharing data between users

Wednesday 7 November 2012


JSF Core Tags
Tag                                           Description
view                                  Creates the top-level view
subview                            Creates a subview of a view
facet                                 Adds a facet to a component
attribute                           Adds an attribute (key/value) to a component
param                              Adds a parameter to a component
actionListener                 Adds an action listener to a component
valueChangeListener      Adds a valuechange listener to a component
convertDateTime            Adds a datetime converter to a component
convertNumber              Adds a number converter to a component
validator                         Adds a validator to a component
validateDoubleRange     Validates a double range for a component’s value
validateLength                Validates the length of a component’s value
validateLongRange         Validates a long range for a component’s value
loadBundle                      Loads a resource bundle, stores properties as a Map
selectitems                      Specifies items for a select one or select many
                                        component
selectitem                        Specifies an item for a select one or select many
                                         component
verbatim                         Adds markup to a JSF page


JSF   HTML Tags
Tag                                              Description
form                                       HTML form
inputText                               Single-line text input control
inputTextarea                       Multiline text input control
inputSecret                           Password input control
inputHidden                          Hidden field
outputLabel                          Label for another component for accessibility
outputLink                            HTML anchor
outputFormat                       Like outputText, but formats compound messages
outputText                            Single-line text output
commandButton                  Button: submit, reset, or pushbutton
commandLink                      Link that acts like a pushbutton
message                               Displays the most recent message for a component
messages                             Displays all messages
graphicImage                      Displays an image
selectOneListbox                 Single-select listbox
selectOneMenu                    Single-select menu
selectOneRadio                    Set of radio buttons
selectBooleanCheckbox      Checkbox
selectManyCheckbox          Set of checkboxes
selectManyListbox              Multiselect listbox
selectManyMenu                 Multiselect menu
panelGrid                            HTML table
panelGroup                         Two or more components that are laid out as one
dataTable                            A feature-rich table control
column                                Column in a dataTable


How to define navigation rule in JSF  faces-config.xml?

<navigation-rule>
 <from-view-id>welcome.xhtml</from-view-id>
 <navigation-case>
  <from-outcome>success</from-outcome>
  <to-view-id>page1.xhtml</to-view-id>
 </navigation-case> 
        <navigation-case>
  <from-outcome>failure</from-outcome>
  <to-view-id>page2.xhtml</to-view-id>
 </navigation-case>  
 </navigation-rule>
 
 
 
In JSF 2, we can also put conditional checking like .....
 
<navigation-rule>
 <from-view-id>welcome.xhtml</from-view-id>
 <navigation-case>
  <from-outcome>success</from-outcome>
                <if>#{testBean.count &gt 0}</if>
  <to-view-id>page1.xhtml</to-view-id>
 </navigation-case>          
         <navigation-case>
  <from-outcome>failure</from-outcome>
  <to-view-id>page2.xhtml</to-view-id>
 </navigation-case>  
 </navigation-rule>  

 

Monday 27 August 2012

Advantage of Using JSF ? Why JSF ?

1)Allowing resusable UI components to create new UI
2)Easy event handling and validation.
3)Moving application data to and from UI is easy.
4)Follow MVC architecture.
5)Third party faces support likes Richfaces,Icefaces etc...

Simple Example on JSF with Validation--

 ValidateEmail.java

package com.nik.testValidation
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class ValidatEmail  implements Validator

{
  public ValidatEmail()
  {
  }
  //Override the validate method 
   public void validate(FacesContext context, UIComponent component, Object   val)
      throws ValidatorException {
    if (null != val) {
      if (!(val instanceof String)) {
        throw new IllegalArgumentException(
            "The value must be a String");
      }
      String email = (String) val;
     
     
       if (email.indexOf("@") > 0 || email.) {
        throw new ValidatorException(
            new FacesMessage("Invalid Email Address"));
   
      }
    }
  }
   
}

TestBean.java

package com.nik.testValidation 
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

public class TestBean
{
String email;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
}

JSF Code

<%@ page contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
  <html>
    <head>
      <title>A Simple Validation Example</title>
    </head>
    <body>
      <h:form>

<h:inputText value="#{bean.email}" required="true"
                             validator="#{bean.validateEmail}" id="email"/>
   <h:message for="email"/>
</body>
</h:form>
</html>
</f:view>

Part of faces config

<managed-bean>
    <managed-bean-name>bean</managed-bean-name>
    <managed-bean-class>com.nik.testValidation.TestBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>

Jars Required

servlet-api.jar
jsp-api.jar
jstl API jar file
jsf-api.jar
commons-beanutils.jar
commons-collections.jar
commons-digester.jar
commons-logging.jar
JSTL standard.jar
jsf-impl.jar
Most of the developers and experties says primefaces...
http://primefaces.org/whyprimefaces.html