ABOUT
TAGS
JSF
JSF Life Cycle
JavaServer Faces was introduced as Java Specification Request (JSR) 127 by Sun in May 2001; the final version of the specification, JSF 1.0, was released on March 3, 2004, and JSF 1.1 (a maintenance release) arrived on May 27th, 2004.

JavaServer Faces is a “framework” for developing web-based UIs in Java.

JSF constitutes the following;
1. UI Components - which interacts with users actions
2. Managed Bean - Also called as Backing bean where the user actions are translated into appropriate results
3. faces-config.xml - where all the actions are mapped to the respective jsf pages, this also has the list of managed beans

In Detail -

UIComponents : For example inputtext, outputtext , outputlabel, etc.. these are all the typical ui components which you see in any web application, so also in jsf you will encounter all these components.
To use any of the standard jsf components you need to included these tag libs into your jsf page
1) <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
2) <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
so you can use any of the components with the prefix ( h or f ) inaddition to this, there are lot of available component like Apache MyFaces which you can use.

Managed Bean : A managed bean is nothing but a java class with all the variables defined which are directly used in the jsf pages,hence set and get methods and business logic methods.

faces-config.xml : This is where all the mappings are defined.
for ex:
<faces-config>
<navigation-rule>
       <navigation-case>
             <description>you can add any description, describing the action</description>
             <from-outcome>cancelClicked</from-outcome>
               <!-- This is the action user click and cancelClicked in the output string-->
             <to-view-id>/home.faces</to-view-id>
               <!-- this is the response page showed to the user -->
           </navigation-case>
</navigation-rule>
<managed-bean>
    <managed-bean-name>testBean</managed-bean-name> <!-- this is the bean instance -->
    <managed-bean-class>com.darla.TestBean</managed-bean-class> <!-- Bean class -->
    <managed-bean-scope>application</managed-bean-scope> <!-- bean scope -->
</managed-bean>

</faces-config>

The Bean scope can be request, session and application
Tips in JSF

#1 To use redirect in jsf: This is used when you want to show the url in browser ( http://localhost:7001/test.jsp )
<navigation-rule>
    <navigation-case>
      <from-outcome>test</from-outcome>
      <to-view-id>/test.jsp</to-view-id>
      <redirect></redirect>
    </navigation-case>
</navigation-rule>

#2 To use Tiles in JSF:
<application>
    <view-handler>org.apache.myfaces.application.jsp.JspTilesViewHandlerImpl</view-handler>
    <locale-config>
      <default-locale>en</default-locale>
     </locale-config>
    <message-bundle>ApplicationMessages</message-bundle> <!-- this is resource bundle -- >
</application>

#3 To get instance of another bean in the container:
TestBean testBean =  (TestBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("beanId");
        if (null == testBean) // if instance is null then use this
        {
            FacesContext context = FacesContext.getCurrentInstance();
            myBean2 = (TestBean)context.getApplication().createValueBinding("#{"beanId"}").getValue(context);
        }

#4 To add messages to the faces context:
FacesContext facesContext = FacesContext.getCurrentInstance();
String text = FacesMessages.getMessageResourceString(FacesContext.getCurrentInstance().getApplication().getMessageBundle(),
                                                                              errorNumber,null,FacesContext.getCurrentInstance().getViewRoot().getLocale());
facesContext.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,null,text));

#5 To display error messages:
<h:messages errorClass="error" infoClass="info" warnClass="warn"
            fatalClass="fatal" showDetail="true" warnStyle="color: red" infoStyle="color: blue" errorStyle="color: red"/>

#6 To display error message for a particular component:
<h:message for="username"/>

#7 To create a custom validator:

Add the validator attribute on the inputText tag:

<h:inputText value="#{userBean.userName}" validator="#{userBean.validateUser} id="userName" required="true"/>

public void checkUser(FacesContext context, UIComponent component, Object value) throws ValidatorException {
       // make sure to call component.setValid(false)
}

#8 To display each error messages in new line.
<h:messages styleClass="messages" id="messageId" layout="table" /> , the other value for layout is "list".

#9





Narendra Kumar Dantala
A J2EE Developer
Having a masters degree in Computational Science, working in Java, developed applications on different domains such as Finanical Services, B2B, Banking, Utility...